bzoj 1065(NOI 2008 奧運物流)
Description
自己看吧= =
Solution
這個題感覺很蛋疼啊= =因為它不僅僅是一棵樹,在1節點處還有一個環。我們考慮一個環上距離節點1距離為dep對答案所做的貢獻
假設環的長度為l,則貢獻為c1?kdep?(1+kl+k2l+....),然後我們驚訝的發現這原來是個等比數列求和啊。。。
於是對答案的貢獻是c1?kdep1?kl,顯然,修改m次的話我們最好把每個節點都指向1,這樣dep=1,收益最大
之前一直沒想明白環怎麼辦,看別人題解發現直接一個等比數列求和就搞定了= =too young。。
然後用f[i][j][k]表示節點i為根的子樹,深度為j,修改k次獲得最大值,先枚舉1處的環長度再樹dp即可,詳情看代碼
Code
#include
using namespace std;
typedef long long LL;
#define pb push_back
#define mp make_pair
#define F first
#define S second
inline void read(int &t) {
int f = 1;char c;
while (c = getchar(), c < '0' || c > '9') if (c == '-') f = -1;
t = c - '0';
while (c = getchar(), c >= '0' && c <= '9') t = t * 10 + c - '0';
t *= f;
}
const int N = 65;
const double inf = 1e10;
vector g[N];
int tot, n, m, fa[N], cir[N], num[N];
double k, p[N], c[N], f[N][N][N];
void gao(int u, int dep, int len) {
for (int i = 0, v; i < g[u].size(); ++i) {
v = g[u][i];
gao(v, dep + 1, len);
}
for (int i = 0; i <= dep; ++i) {
if (num[u] == len && i != 1) continue;
if (u != 1 && num[u] && num[u] <= len && i != (len - (tot - dep))) continue;
f[u][i][0] = c[u] * p[i] / (1.0 - p[len]);
for (int j = 0, v; j < g[u].size(); ++j) {
v = g[u][j];
for (int k = m; k >= 0; --k) {
if (!k) f[u][i][k] += f[v][i + 1][k];
else {
double t = -inf;
for (int l = 0; l <= k; ++l)
f[u][i][k] = max(f[u][i][k], f[u][i][k - l] + max(f[v][i + 1][l], l ? f[v][1][l - 1] : -inf));
f[u][i][k] = t;
}
}
}
}
}
int main() {
read(n), read(m);
scanf("%lf", &k);
p[0] = 1.0;
for (int i = 1; i <= n; ++i) p[i] = p[i - 1] * k;
for (int i = 1; i <= n; ++i) {
read(fa[i]);
if (i != 1) g[fa[i]].pb(i);
}
for (int i = 1; i <= n; ++i) scanf("%lf", &c[i]);
cir[++tot] = 1, num[1] = 1;
int now = fa[1];
while (now != 1) {
cir[++tot] = now;
num[now] = tot;
now = fa[now];
}
double ans = 0.0;www.Bkjia.com
for (int i = tot; i >= 2; --i) {//enum circle length
for (int j = 1; j <= n; ++j)
for (int k = 0; k <= n; ++k)
for (int l = 0; l <= n; ++l)
f[j][k][l] = -inf;
gao(1, 0, i);
ans = max(ans, f[1][0][m]);
}
printf("%.2lf\n", ans);
return 0;
}