hdu4276 The Ghost Blows Light

这篇博客介绍了如何使用树形动态规划解决题目The Ghost Blows Light。首先处理树形结构,确保从1号节点到N号节点的路径上每个节点都是其父节点的最右端。然后通过回溯从子节点到根节点更新最大权值和。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 10 1 2 2 2 3 2 2 5 3 3 4 3 1 2 3 4 5
 

Sample Output
  
  
11
 

Source
 

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;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值