The Ghost Blows light
Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 2240 accepted submission (s): 688
Problem description
My name is Hu Bayi, robing an existing ent tomb in Tibet. the tomb consists of N rooms (numbered from 1 to n) which are connected by some roads (pass each road shocould 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 shocould 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.
Inputthere 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,! = B, 0 <= T <= 100)
The last line contains N integers, which AI indicating the number of treasure in the ith room. (0 <= AI <= 100)
Outputfor 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! ".
Sample Input
5 101 2 2 2 3 22 5 33 4 31 2 3 4 5
Sample output
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 the Ghost Blows light (Dp-tree DP)