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 98 99 100 101
| include <cstdio> include <cstring> include <iostream> using namespace std;
const int maxn = 200; int head[maxn], tol, dpmaxn, weight[maxn], T, t, n;
struct node { int next, to, time; node(){}; node(int next, int _to, int _time) : next(next), to(to), time(time){} }edge[5*maxn];
void add(int u, int v, int time) { edge[tol] = node(head[u], v, time); head[u] = tol++; }
bool dfs(int u, int pre) { if(u == n) return 1;
for(int i = head[u]; i != -1; i = edge[i].next) { int v = edge[i].to; if( v == pre ) continue;
if(dfs(v, u)) { t += edge[i].time; edge[i].time = 0; return 1; } } return 0; }
void dfs1(int u, int pre) { for(int i = 0; i <= T; i++) dpu = weight[u];
for(int i = head[u]; i != -1; i = edge[i].next) { int v = edge[i].to; if( v == pre ) continue; dfs1(v, u); int pp = 2*edge[i].time; for(int j = T; j >= pp; j–) for(int k = 0; k <= j-pp; k++) dpu = max(dpu, dpv+dpu); } }
int main() { int i, j, k, p; while(~scanf(“\%d\%d”, &n, &T)) { memset(head, -1, sizeof(head)); tol = 0; for(i = 1; i < n; i++) { scanf(“\%d\%d\%d”, &j, &k, &p); add(j, k, p); add(k, j, p); } for(i = 1; i <= n; i++) scanf(“\%d”, &weight[i]); t = 0; dfs(1, 1); if(t > T) { puts(“Human beings die in pursuit of wealth, and birds die in pursuit of food!”); continue; } memset(dp, 0, sizeof(dp)); T -= t; dfs1(1, -1); cout<< dp1 <<endl; } return 0; }
|