标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3345 Accepted Submission(s): 1040
1 # include <bits/stdc++.h> 2 using namespace std; 3 4 const int MAX = 120; 5 struct node 6 { 7 int to; 8 int next; 9 int t; 10 }tree[MAX]; 11 int head[MAX]; 12 int tol = 0; 13 int w[MAX]; 14 int n, t; 15 int Mint; 16 int dp[MAX][600]; 17 18 void add(int a, int b, int val) 19 { 20 tree[tol].to = b; 21 tree[tol].next = head[a]; 22 tree[tol].t = val; 23 head[a] = tol++; 24 } 25 26 bool dfs1(int root, int f) 27 { 28 if(root == n) 29 return true;//找到了 30 for(int i = head[root]; i != -1; i = tree[i].next) 31 { 32 int son = tree[i].to; 33 if(son == f) 34 continue; 35 if(dfs1(son, root)) 36 { 37 Mint += tree[i].t; 38 tree[i].t = 0; 39 return true; 40 } 41 } 42 return false; 43 } 44 45 void dfs2(int root, int f) 46 { 47 for(int i = 0; i <= t; i++) 48 dp[root][i] = w[root]; 49 for(int i = head[root]; i != -1; i = tree[i].next) 50 { 51 int son = tree[i].to; 52 if(son == f) 53 continue; 54 dfs2(son, root); 55 int cost = tree[i].t * 2; 56 for(int j = t; j >= cost; j--) 57 { 58 for(int k = 0; k <= j - cost; k++) 59 dp[root][j] = max(dp[root][j], dp[root][j - k - cost] + dp[son][k]); 60 } 61 } 62 } 63 int main() 64 { 65 while(scanf("%d%d", &n, &t) != EOF) 66 { 67 memset(head, -1, sizeof(head)); 68 memset(dp, 0, sizeof(dp)); 69 tol = 0; 70 Mint = 0; 71 int a, b, val; 72 for(int i = 1; i < n; i++) 73 { 74 scanf("%d%d%d", &a, &b, &val); 75 add(a, b, val); 76 add(b, a, val); 77 } 78 for(int i = 1; i <= n; i++) 79 { 80 scanf("%d", &w[i]); 81 } 82 dfs1(1, -1);// 看看直接走去的最短时间是多少, 83 if(Mint > t) 84 { 85 printf("Human beings die in pursuit of wealth, and birds die in pursuit of food!\n"); 86 continue; 87 } 88 t -= Mint; 89 dfs2(1, -1); 90 printf("%d\n", dp[1][t]); 91 } 92 return 0; 93 }
HDU 4276 - The Ghost Blows Light
标签:
原文地址:http://www.cnblogs.com/lyf-acm/p/5815854.html