The Ghost Blows Light
Problem Description
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.
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, 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)
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 Input5 10 1 2 2 2 3 2 2 5 3 3 4 3 1 2 3 4 5
Sample Output11
Source
【題意】php
一個有 N 個節點的樹形的地圖,知道了每條邊通過所須要的時間,如今給出時間T,問能不能在T時間內從 1號節點到 N 節點。每一個節點都有相對應的價值,並且每一個價值只能被取一次,問若是能夠從1 號節點走到 n 號節點的話,最多能夠取到的最大價值爲多少。node
【分析】ios
這題跟poj2486相似,不過這題的問題是不知道是否回到n。
規定回到n的話,咱們畫一下圖會發現,咱們走的路徑是1到n的路徑只走一遍,其餘路徑必定是去和回的兩遍。因此咱們能夠先求出1~n的路徑,而後把這些邊權置爲0,而後就是依賴揹包問題,f[i][j]表示在i這棵子樹上走j分鐘的最大價值,咱們走一條邊的費用是*2的,因去和回是兩遍,最後要加上1~n的路徑的代價。
(網上打的都是樹形0-1揹包是n^3,依賴揹包能夠打成n^2)ide
代碼以下:ui
1 #include<cstdio> 2 #include<cstdlib> 3 #include<cstring> 4 #include<iostream> 5 #include<algorithm> 6 using namespace std; 7 #define Maxn 110 8 #define Maxm 510 9 10 struct node 11 { 12 int x,y,c,next; 13 }t[Maxn*2];int len; 14 int first[Maxn],w[Maxn]; 15 16 int mymax(int x,int y) {return x>y?x:y;} 17 18 void ins(int x,int y,int c) 19 { 20 t[++len].x=x;t[len].y=y;t[len].c=c; 21 t[len].next=first[x];first[x]=len; 22 } 23 24 int n,v; 25 int dis[Maxn],sum; 26 27 bool dfs(int x,int fa) 28 { 29 if(x==n) return 1; 30 for(int i=first[x];i;i=t[i].next) if(t[i].y!=fa) 31 { 32 int y=t[i].y; 33 dis[y]=dis[x]+t[i].c; 34 if(dfs(y,x)) {sum+=t[i].c;t[i].c=0;return 1;} 35 } 36 return 0; 37 } 38 39 int f[Maxn][Maxm]; 40 void ffind(int x,int fa) 41 { 42 for(int i=first[x];i;i=t[i].next) if(t[i].y!=fa) 43 { 44 int y=t[i].y; 45 for(int j=0;j<=v-2*t[i].c;j++) if(f[x][j]!=-1) 46 { 47 f[y][j+2*t[i].c]=mymax(f[y][j+2*t[i].c],f[x][j])+w[y]; 48 } 49 ffind(y,x); 50 for(int j=0;j<=v;j++) if(f[y][j]!=-1) 51 { 52 f[x][j]=mymax(f[x][j],f[y][j]); 53 } 54 } 55 } 56 57 int main() 58 { 59 while(scanf("%d%d",&n,&v)!=EOF) 60 { 61 len=0; 62 memset(first,0,sizeof(first)); 63 for(int i=1;i<n;i++) 64 { 65 int x,y,c; 66 scanf("%d%d%d",&x,&y,&c); 67 ins(x,y,c);ins(y,x,c); 68 } 69 for(int i=1;i<=n;i++) scanf("%d",&w[i]); 70 sum=0; 71 dfs(1,0); 72 if(sum>v) 73 { 74 printf("Human beings die in pursuit of wealth, and birds die in pursuit of food!\n"); 75 } 76 else 77 { 78 memset(f,-1,sizeof(f)); 79 f[1][0]=w[1]; 80 ffind(1,0); 81 int ans=0; 82 for(int i=0;i<=v-sum;i++) ans=mymax(ans,f[1][i]); 83 printf("%d\n",ans); 84 } 85 86 } 87 return 0; 88 }
2016-10-18 10:51:20spa