The Ghost blows Light Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 2549 accepted Submission (s): 795
Problem Description
My name is Hu Bayi, robing a 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 Tim e). There is exactly one route between any two rooms, and each room contains some. Now I am located at the 1st room and the ' exit is located ' at the nth Room.
Suddenly, alert occurred! The tomb would topple down into 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.
Input There are multiple test cases.
The contains two integer n and T. (1 <= n <= 0 <= T <= 500)
Each of the next N-1 lines contains three integers a, b, and t indicating there are 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. (0 <= Ai <= 100)
Output for each test case, output a 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 10 1 2 2 2 3 2 2 5 3 3 4 3 1 2 3 4 5
Sample Output
11
Source ACM/ICPC Asia Regional Changchun Online
Recommend Liuyiding | We have carefully selected several similar problems for you:4272 4277 4274 4269 4275 topic: A tree, from 1 to n points, goes in T time, obtains the biggest Value idea: First use SPFA to find the shortest road, if >t, then can not, T minus the shortest way, the shortest way of the Benquan to 0, if not go the most short-circuit, back and forth for twice times the Benquan, that is twice times the consumption of Benquan AC code
#include <stdio.h> #include <string.h> #include <queue> #include <string> #include <iostream
> #define MAX (a,b) (a>b?a:b) #define INF 0xfffffff using namespace std;
int n,t;
int a[110],head[110],vis[110],cnt,pre[110],dis[110],d[220],dp[110][10100]; struct S {int u,v,w,next;}
EDGE[220];
void Add (int u,int v,int W) {edge[cnt].u=u;
Edge[cnt].v=v;
Edge[cnt].w=w;
Edge[cnt].next=head[u];
head[u]=cnt++;
} void Spfa (int u) {int i;
for (i=1;i<=n;i++) {dis[i]=inf;
Pre[i]=i;
d[i]=0;
} memset (Vis) (vis,0,sizeof);
Vis[u]=1;
dis[u]=0;
queue<int>q;
Q.push (U);
while (!q.empty ()) {int U=q.front ();
Q.pop ();
vis[u]=0;
for (i=head[u];i!=-1;i=edge[i].next) {int v=edge[i].v;
int W=EDGE[I].W;
if (dis[v]>dis[u]+w) {dis[v]=dis[u]+w;
Pre[v]=u;
D[v]=i;
if (!vis[v]) {vis[v]=1;
Q.push (v);
}}} for (I=n;i!=1;i=pre[i]) {edge[d[i]].w=0;
edge[d[i]^1].w=0; } void Dfs (int u,int pre) {
int i,j,k;
for (i=0;i<=t;i++) {dp[u][i]=a[u];
for (i=head[u];i!=-1;i=edge[i].next) {int v=edge[i].v;
if (v==pre) continue;
DFS (V,U);
int cost=edge[i].w*2;
for (j=t;j>=cost;j--) {for (k=0;k+cost<=j;k++) {Dp[u][j]=max (dp[u][j],dp[v][k]+dp[u][j-k-cost]);
int main () {while (scanf ("%d%d", &n,&t)!=eof) {int i;
Memset (head) (head,-1,sizeof);
Memset (Dp,0,sizeof (DP));
cnt=0;
for (i=0;i<n-1;i++) {int u,v,w;
scanf ("%d%d%d", &u,&v,&w);
Add (U,V,W);
Add (V,U,W);
for (i=1;i<=n;i++) {scanf ("%d", &a[i]);
} SPFA (1);
if (dis[n]>t) {printf ("Human beings die in pursuit of wealth, and birds die in pursuit of food!\n");
Continue
} T-=dis[n];
DFS (1,-1);
printf ("%d\n", dp[1][t]);
}
}