hdu 4276 The Ghost Blows Light(DP-树形DP)
2014-08-29 21:01
260 查看
The Ghost Blows Light
[b]Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2240 Accepted Submission(s): 688
[/b]
[align=left]Problem Description[/align]
My name is Hu Bayi, robing an ancient tomb in Tibet. The tomb consists of N rooms (numbered from 1 to N) which are connected by some roads (pass each road should cost some time). There is exactly one route between any two rooms, and each room contains some
treasures. Now I am located at the 1st room and the exit is located at the Nth room.
Suddenly, alert occurred! The tomb will topple down in T minutes, and I should reach exit room in T minutes. Human beings die in pursuit of wealth, and birds die in pursuit of food! Although it is life-threatening time, I also want to get treasure out as much
as possible. Now I wonder the maximum number of treasures I can take out in T minutes.
[align=left]Input[/align]
There are multiple test cases.
The first line contains two integer N and T. (1 <= n <= 100, 0 <= T <= 500)
Each of the next N - 1 lines contains three integers a, b, and t indicating there is a road between a and b which costs t minutes. (1<=a<=n, 1<=b<=n, a!=b, 0 <= t <= 100)
The last line contains N integers, which Ai indicating the number of treasure in the ith room. (0 <= Ai <= 100)
[align=left]Output[/align]
For each test case, output an integer indicating the maximum number of treasures I can take out in T minutes; if I cannot get out of the tomb, please output "Human beings die in pursuit of wealth, and birds die in pursuit
of food!".
[align=left]Sample Input[/align]
5 10 1 2 2 2 3 2 2 5 3 3 4 3 1 2 3 4 5
[align=left]Sample Output[/align]
11
#include <iostream> #include <cstdio> #include <vector> #include <cstring> #include <algorithm> using namespace std; const int maxn = 110; const int maxt = 510; int dp[maxn][maxt] , A[maxn] , vis[maxn] , ans[maxt]; int way[maxn]; int N , T , cnt , sumT , route; struct edge{ int u , v , t , next; edge(int a = 0 , int b = 0, int c = 0 , int d = 0){ u = a , v = b , t = c , next = d; } }e[4*maxn]; int head[maxn]; void add(int u , int v , int t){ e[cnt] = edge(u , v , t , head[u]); head[u] = cnt++; } void initial(){ for(int i = 0; i < maxn; i++){ head[i] = -1; A[i] = 0; vis[i] = 0; for(int j = 0; j < maxt; j++) dp[i][j] = 0; } for(int i = 0; i < maxt; i++) ans[i] = 0; cnt = 0; sumT = 0; route = 0; } void readcase(){ int u , v , t; for(int i = 0; i < N-1; i++){ scanf("%d%d%d" , &u , &v , &t); add(u , v , t); add(v , u , t); } for(int i = 1; i <= N; i++){ scanf("%d" , &A[i]); } } void DP(int k , int f){ dp[k][0] = A[k]; for(int i = head[k]; i != -1; i = e[i].next){ int son = e[i].v; if(son != f){ DP(son , k); if(vis[son]) continue; for(int t = T; t >= 0; t--){ if(dp[k][t] == 0 && t != 0) continue; for(int j = T; j >= 0; j--){ if(dp[son][j] != 0 && t+j+2*e[i].t <= T && dp[k][t+j+2*e[i].t] < dp[k][t]+dp[son][j]) dp[k][t+j+2*e[i].t] = dp[k][t]+dp[son][j]; } } } } } bool dfs(int k , int index , int f){ way[index] = k; route = index; if(k == N) return true; for(int i = head[k]; i != -1; i = e[i].next){ int son = e[i].v; if(f != son){ sumT += e[i].t; if(dfs(son , index+1 , k)) return true; sumT -= e[i].t; } } return false; } void computing(){ dfs(1 , 0 , 1); for(int i = 0; i <= route; i++) vis[way[i]] = 1; DP(1 , 1); if(sumT > T){ printf("Human beings die in pursuit of wealth, and birds die in pursuit of food!\n"); return; } T -= sumT; for(int i = 0; i <= route; i++){ for(int t = T; t >= 0; t--){ if(ans[t] == 0 && t != 0) continue; for(int j = T; j >= 0; j--){ if(t+j <= T && ans[t+j] < ans[t]+dp[way[i]][j]) ans[t+j] = ans[t]+dp[way[i]][j]; } } } int Max = 0; for(int i = 0; i <= T; i++) if(Max < ans[i]) Max = ans[i]; printf("%d\n" , Max); } int main(){ while(~scanf("%d%d" , &N , &T)){ initial(); readcase(); computing(); } return 0; }
相关文章推荐
- HDU 4276 树形DP The Ghost Blows Light
- HDU 4276 The Ghost Blows Light (树形DP)
- HDU 4276 The Ghost Blows Light [树形背包DP]
- HDU 4276 The Ghost Blows Light(12年长春 树形DP+spfa)
- hdu 4276 The Ghost Blows Light(树形DP+最短路+分组背包)好题。。。
- hdu 4276 The Ghost Blows Light(Spfa+树形dp)
- HDU 4276 The Ghost Blows Light(树形DP)
- 【HDU 4276】The Ghost Blows Light(树形DP,依赖背包)
- hdu 4276 The Ghost Blows Light - 树形dp
- HDU 4276 The Ghost Blows Light 树形dp
- **HDU 4276 - The Ghost Blows Light(树形DP)
- hdu 4276 The Ghost Blows Light (树形dp)
- HDU 4276 The Ghost Blows Light【树形DP】
- 【树形DP】【HDU 4276】The Ghost Blows Light
- HDOJ 4276 The Ghost Blows Light(树形DP)
- *(中等) 树形dp+分组背包 HDU 4276 The Ghost Blows Light
- HDOJ 4276 The Ghost Blows Light(树形DP)
- hdoj 4276 The Ghost Blows Light 【树形dp】
- HDU 4276 The Ghost Blows Light(树形)
- HDU-4276 The Ghost Blows Light (树形DP+背包)