# 【BZOJ 1065】【Vijos 1826】【NOI 2008】奥运物流

http://www.lydsy.com/JudgeOnline/problem.php?id=1065
https://vijos.org/p/1826

∈我这辈子也想不出来系列~

$$f(i,j,d)=max\{g(s_1,j_1,d)+g(s_2,j_2,d)+\dots +g(s_t,j_t,d)\}+c(i)*k^d$$
$$s_1,s_2\dots s_t$$为i的t个儿子。

4的后继边指向了1。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int N = 63;

struct node {int nxt, to;} E[N];

int n, m, T[N], point[N], cnt, cir[N], ctot = 0, a[N], atot, deep[N];
double k, f[N][N][N], g[N][N][N], FF[N][N], C[N], ipow[N];
bool vis[N];

void ins(int u, int v) {E[++cnt] = (node) {point[u], v}; point[u] = cnt;}

void pre_dfs(int x) {
for (int i = point[x]; i; i = E[i].nxt)
if (!vis[E[i].to])
deep[E[i].to] = deep[x] + 1, pre_dfs(E[i].to);
}

void dp(int x) {
for (int i = point[x]; i; i = E[i].nxt)
if (!vis[E[i].to])
dp(E[i].to);

atot = 0;
for (int i = point[x]; i; i = E[i].nxt)
if (!vis[E[i].to]) a[++atot] = E[i].to;

for (int d = deep[x]; d >= 1; --d) {
for (int i = 1; i <= atot; ++i)
for (int l = 0; l <= m; ++l)
FF[i][l] = 0;
for (int i = 1; i <= atot; ++i)
for (int l = 0; l <= m; ++l)
for (int nowl = 0; nowl <= l; ++nowl)
FF[i][l] = max(FF[i][l], FF[i - 1][l - nowl] + g[a[i]][nowl][d]);
for (int j = 0; j <= m; ++j)
f[x][j][d] = FF[atot][j] + C[x] * ipow[d];
}

if (x == 1) {
for (int i = 1; i <= atot; ++i)
for (int l = 0; l <= m; ++l)
FF[i][l] = 0;
for (int i = 1; i <= atot; ++i)
for (int l = 0; l <= m; ++l)
for (int nowl = 0; nowl <= l; ++nowl)
FF[i][l] = max(FF[i][l], FF[i - 1][l - nowl] + g[a[i]][nowl][0]);
for (int j = 0; j <= m; ++j)
f[x][j][0] = FF[atot][j] + C[x];
} else {
for (int j = 1; j <= m; ++j)
f[x][j][1] = max(f[x][j][1], FF[atot][j - 1] + C[x] * k);
}

for (int d = deep[x] - 1; d >= 0; --d) {
g[x][0][d] = f[x][0][d + 1];
for (int j = 1; j <= m; ++j)
g[x][j][d] = max(f[x][j][d + 1], f[x][j - 1][1]);
}
}

int main() {
scanf("%d%d%lf", &n, &m, &k);
for (int i = 1; i <= n; ++i) {
scanf("%d", T + i);
ins(T[i], i);
}
for (int i = 1; i <= n; ++i)
scanf("%lf", C + i);
ipow[0] = 1;
for (int i = 1; i <= n; ++i)
ipow[i] = ipow[i - 1] * k;

int tmp = T[1]; cir[++ctot] = 1;
while (tmp != 1) {
cir[++ctot] = tmp;
tmp = T[tmp];
}

double ans = 0, ret;
vis[1] = true;
for (int len = 2; len <= ctot; ++len) {
vis[cir[len]] = true;
pre_dfs(1);
cnt = len;
for (int i = 2; i <= len; ++i)
deep[cir[i]] = --cnt, pre_dfs(cir[i]);
memset(f, 0, sizeof(f));
memset(g, 0, sizeof(g));

for (int i = 1; i <= len; ++i)
dp(cir[i]);

cnt = len;
memset(FF, 0, sizeof(FF));
for (int i = 0; i <= m; ++i)
FF[1][i] = f[1][i][0];
for (int i = 2; i <= len; ++i) {
--cnt;
for (int j = 0; j <= m; ++j)
for (int nowl = 0; nowl <= j; ++nowl)
FF[i][j] = max(FF[i][j], FF[i - 1][j - nowl] + f[cir[i]][nowl][cnt]);
}
if (len < ctot) ans = max(ans, FF[len][m - 1] / (1.0 - ipow[len]));
else ans = max(ans, FF[len][m] / (1.0 - ipow[len]));
//		printf("%.2lf\n", f[1][1][0]);
}

printf("%.2lf\n", ans);

//	for (int i = 1; i <= ctot; ++i)
//		printf("%d\n", cir[i]);

return 0;
}

posted @ 2016-10-27 20:15  abclzr  阅读(269)  评论(0编辑  收藏  举报