【HDU 4276】The Ghost Blows Light(树形DP,依赖背包)

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.
 
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)
 
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
 
Source
 

【题意】

  一个有 N 个节点的树形的地图,知道了每条边经过所需要的时间,现在给出时间T,问能不能在T时间内从 1号节点到 N 节点。每个节点都有相对应的价值,而且每个价值只能被取一次,问如果可以从1 号节点走到 n 号节点的话,最多可以取到的最大价值为多少。

【分析】

  这题跟poj2486类似,不过这题的问题是不知道是否回到n。
  规定回到n的话,我们画一下图会发现,我们走的路径是1到n的路径只走一遍,其他路径一定是去和回的两遍。所以我们可以先求出1~n的路径,然后把这些边权置为0,然后就是依赖背包问题,f[i][j]表示在i这棵子树上走j分钟的最大价值,我们走一条边的费用是*2的,因去和回是两遍,最后要加上1~n的路径的代价。
  (网上打的都是树形0-1背包是n^3,依赖背包可以打成n^2)

代码如下:

 #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define Maxn 110
#define Maxm 510 struct node
{
int x,y,c,next;
}t[Maxn*];int len;
int first[Maxn],w[Maxn]; int mymax(int x,int y) {return x>y?x:y;} void ins(int x,int y,int c)
{
t[++len].x=x;t[len].y=y;t[len].c=c;
t[len].next=first[x];first[x]=len;
} int n,v;
int dis[Maxn],sum; bool dfs(int x,int fa)
{
if(x==n) return ;
for(int i=first[x];i;i=t[i].next) if(t[i].y!=fa)
{
int y=t[i].y;
dis[y]=dis[x]+t[i].c;
if(dfs(y,x)) {sum+=t[i].c;t[i].c=;return ;}
}
return ;
} int f[Maxn][Maxm];
void ffind(int x,int fa)
{
for(int i=first[x];i;i=t[i].next) if(t[i].y!=fa)
{
int y=t[i].y;
for(int j=;j<=v-*t[i].c;j++) if(f[x][j]!=-)
{
f[y][j+*t[i].c]=mymax(f[y][j+*t[i].c],f[x][j])+w[y];
}
ffind(y,x);
for(int j=;j<=v;j++) if(f[y][j]!=-)
{
f[x][j]=mymax(f[x][j],f[y][j]);
}
}
} int main()
{
while(scanf("%d%d",&n,&v)!=EOF)
{
len=;
memset(first,,sizeof(first));
for(int i=;i<n;i++)
{
int x,y,c;
scanf("%d%d%d",&x,&y,&c);
ins(x,y,c);ins(y,x,c);
}
for(int i=;i<=n;i++) scanf("%d",&w[i]);
sum=;
dfs(,);
if(sum>v)
{
printf("Human beings die in pursuit of wealth, and birds die in pursuit of food!\n");
}
else
{
memset(f,-,sizeof(f));
f[][]=w[];
ffind(,);
int ans=;
for(int i=;i<=v-sum;i++) ans=mymax(ans,f[][i]);
printf("%d\n",ans);
} }
return ;
}

[HDU 4276]

2016-10-18 10:51:20

上一篇:npm publish报错记录


下一篇:发布者订阅者模式(Publish/Subscribe)