The Ghost Blows Light
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1909 Accepted Submission(s): 585
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.
Input
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)
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)
Output
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!".
Sample Input
5 10 1 2 2 2 3 2 2 5 3 3 4 3 1 2 3 4 5
Sample Output
11
给定一棵n个节点的树,每个节点都有权值,从1点出发,在时间T内到达n,且获得最大的权值和。
由于是树,每两个点之间都有唯一的路径,从1到n之间的必经之路唯一,因此那总时间减去这条路上的权值和,这条路上的边权全部清零,就相当于缩点,
每条边要么访问两次,要么不访问,然后进行一次树形背包得到答案。
代码:
/* ***********************************************
Author :xianxingwuguan
Created Time :2014-2-6 22:31:52
File Name :1.cpp
************************************************ */
#pragma comment(linker, "/STACK:102400000,102400000")
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <string>
#include <time.h>
#include <math.h>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define eps 1e-8
#define pi acos(-1.0)
typedef long long ll;
const int maxn=200;
int head[maxn],tol,dp[maxn][5*maxn],weight[maxn],T,t,n;
struct node{
int next,to,val;
node(){};
node(int _next,int _to,int _val):next(_next),to(_to),val(_val){}
}edge[5*maxn];
void add(int u,int v,int val){
edge[tol]=node(head[u],v,val);
head[u]=tol++;
}
bool dfs(int u,int pre){
if(u==n)return 1;
for(int i=head[u];i!=-1;i=edge[i].next){
int v=edge[i].to;
if(v==pre)continue;
if(dfs(v,u)){
t+=edge[i].val;
edge[i].val=0;
return 1;
}
}
return 0;
}
void dfs1(int u,int fa){
for(int i=0;i<=T;i++)dp[u][i]=weight[u];
for(int i=head[u];i!=-1;i=edge[i].next){
int v=edge[i].to;
if(v==fa)continue;
dfs1(v,u);
int pp=2*edge[i].val;
for(int j=T;j>=pp;j--)
for(int k=0;k<=j-pp;k++)
dp[u][j]=max(dp[u][j],dp[v][k]+dp[u][j-pp-k]);
}
}
int main()
{
//freopen("data.in","r",stdin);
//freopen("data.out","w",stdout);
int i,j,k,p;
while(~scanf("%d%d",&n,&T)){
memset(head,-1,sizeof(head));tol=0;
for(i=1;i<n;i++){
scanf("%d%d%d",&j,&k,&p);
add(j,k,p);
add(k,j,p);
}
for(i=1;i<=n;i++)scanf("%d",&weight[i]);
t=0;
dfs(1,1);
if(t>T){
puts("Human beings die in pursuit of wealth, and birds die in pursuit of food!");
continue;
}
memset(dp,0,sizeof(dp));
T-=t;
dfs1(1,-1);
cout<<dp[1][T]<<endl;
}
return 0;
}