1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
| #include <iostream> #include <algorithm> #include <cstring> #include <cstdio>
using namespace std;
const int maxn = 305;
struct edge { int to, nxt, v; }e[maxn << 1]; int f[maxn][maxn], g[maxn][maxn], h[maxn][maxn]; int n, lnk[maxn], ptr, T, ans; int t[maxn], w[maxn];
inline void add(int bgn, int end, int val) { e[++ptr] = (edge){end, lnk[bgn], val}; lnk[bgn] = ptr; } void dfs(int i, int fa) { int cf[maxn], cg[maxn], ch[maxn]; for(int j = t[i]; j <= T; ++j) f[i][j] = w[i], g[i][j] = w[i], h[i][j] = w[i]; for(int p = lnk[i]; p; p = e[p].nxt) { int y = e[p].to; if(y == fa) continue; dfs(y, i); for(int j = 0; j <= T; ++j) cf[j] = f[i][j], cg[j] = g[i][j], ch[j] = h[i][j]; for(int j = 0; j <= T; ++j) { for(int k = 0; k <= T; ++k) { if(j - k - 2 * e[p].v >= 0) h[i][j] = max(h[i][j], ch[j - k - 2 * e[p].v] + f[y][k]), h[i][j] = max(h[i][j], cf[j - k - 2 * e[p].v] + h[y][k]); if(j - k - e[p].v >= 0) h[i][j] = max(h[i][j], cg[j - k - e[p].v] + g[y][k]); } } for(int j = T; j >= 0; --j) { for(int k = 0; k <= T; ++k) { if(j - k - 2 * e[p].v >= 0) f[i][j] = max(f[i][j], cf[j - k - 2 * e[p].v] + f[y][k]); } } for(int j = T; j >= 0; --j) { for(int k = 0; k <= T; ++k) { if(j - k - 2 * e[p].v >= 0) g[i][j] = max(g[i][j], cg[j - k - 2 * e[p].v] + f[y][k]); } } for(int j = T; j >= 0; --j) { for(int k = 0; k <= T; ++k) { if(j - k - e[p].v >= 0) g[i][j] = max(g[i][j], cf[j - k - e[p].v] + g[y][k]); } } } ans = max(max(ans, f[i][T]), max(g[i][T], h[i][T])); }
int main() { freopen("toyuq.in", "r", stdin); freopen("toyuq.out", "w", stdout); scanf("%d%d", &n, &T); for(int i = 1; i <= n; ++i) scanf("%d", &w[i]); for(int i = 1; i <= n; ++i) scanf("%d", &t[i]); register int x, y, z; for(int i = 1; i < n; ++i) { scanf("%d%d%d", &x, &y, &z); add(x, y, z); add(y, x, z); } memset(f, 0xcf, sizeof f); memset(g, 0xcf, sizeof g); memset(h, 0xcf, sizeof h); dfs(1, 0); printf("%d\n", ans); fclose(stdin); fclose(stdout); return 0; }
|