hdu4276 The Ghost Blows Light

来源:互联网 发布:东方财富怎样下载数据 编辑:程序博客网 时间:2024/05/21 18:38

The Ghost Blows Light

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 308    Accepted Submission(s): 99


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 101 2 2 2 3 22 5 33 4 31 2 3 4 5
 

Sample Output
11
 

Source
2012 ACM/ICPC Asia Regional Changchun Online
 

Recommend
liuyiding


呃,树形dp题……

开始在1,最后要跑到N,那么可以设dp[k][t]表示从1号节点在t时间到达k号节点的最大权值和。

那么首先应该处理下树,就是让从1到N的这条路径上的每个节点都在其父节点的最右端(也就是要求最后才能遍历到)

例如

      1

    /    \

  3     2 

 /  \

5  4

要变成

      1

    /    \

  2     3 

        /  \

       4  5


然后从根节点p到子节点t的时候,那么dp[t][i]=dp[p][i-w]+a[t],其中w为经过这条边所需的时间,a[t]表示t节点的价值

之后从子节点t回溯到根节点p的时候,就是dp[p][i]=max(dp[p][i],dp[t][i-w])

因为最后才遍历到n,所以不用担心没有考虑到有其他路线没有走过的情况。

代码

#include <stdio.h>#include <string.h>#include <vector>#include <algorithm>using namespace std;typedef struct{    int num,val;}Tree;vector <Tree> tree[105];int dp[105][505];int ok,n,T;int w[105];void DFS1(int t,int p,int tag){    int i,k;    if (t==n)    {        ok=1;    }    if (ok==1)    {        if (p==-1) return;        swap(tree[p][tag],tree[p][tree[p].size()-1]);        return;    }    for (i=0;i<tree[t].size();i++)    {        k=tree[t][i].num;        if (k==p) continue;        DFS1(k,t,i);        if (ok==1)        {            if (p==-1) return;            swap(tree[p][tag],tree[p][tree[p].size()-1]);            return;        }    }}void DFS2(int t,int p,int tag){    int i,k,j;    if (p==-1)    {        for (i=0;i<=T;i++)        {            dp[t][i]=w[t];        }    }    else    {        for (j=tree[p][tag].val;j<=T;j++)        {            if (dp[p][j-tree[p][tag].val]!=-1) dp[t][j]=dp[p][j-tree[p][tag].val]+w[t];        }    }    for (i=0;i<tree[t].size();i++)    {        k=tree[t][i].num;        if (k==p) continue;        DFS2(k,t,i);    }    if (p==-1) return;    for (i=tree[p][tag].val;i<=T;i++)    {        dp[p][i]=max(dp[p][i],dp[t][i-tree[p][tag].val]);    }}int main(){    int i,j,x,y,z,s;    Tree tag;    while(scanf("%d%d",&n,&T)!=EOF)    {        ok=0;        for (i=1;i<=n;i++)        {            tree[i].clear();        }        for (i=0;i<n-1;i++)        {            scanf("%d%d%d",&x,&y,&z);            tag.num=y;            tag.val=z;            tree[x].push_back(tag);            tag.num=x;            tree[y].push_back(tag);        }        DFS1(1,-1,-1);        memset(dp,-1,sizeof(dp));        dp[1][0]=0;        for (i=1;i<=n;i++)        {            scanf("%d",&w[i]);        }        DFS2(1,-1,-1);        s=-1;        for (i=0;i<=T;i++)        {            s=max(s,dp[n][i]);        }        if (s!=-1) printf("%d\n",s);        else printf("Human beings die in pursuit of wealth, and birds die in pursuit of food!\n");    }    return 0;}



原创粉丝点击