hdu 4616 Game (树形dp)

8 篇文章 1 订阅
4 篇文章 0 订阅

Game

Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 2007 Accepted Submission(s): 649

Problem Description
  Nowadays, there are more and more challenge game on TV such as ‘Girls, Rush Ahead’. Now, you participate int a game like this. There are N rooms. The connection of rooms is like a tree. In other words, you can go to any other room by one and only one way. There is a gift prepared for you in Every room, and if you go the room, you can get this gift. However, there is also a trap in some rooms. After you get the gift, you may be trapped. After you go out a room, you can not go back to it any more. You can choose to start at any room ,and when you have no room to go or have been trapped for C times, game overs. Now you would like to know what is the maximum total value of gifts you can get.

Input
  The first line contains an integer T, indicating the number of testcases.
  For each testcase, the first line contains one integer N(2 <= N <= 50000), the number rooms, and another integer C(1 <= C <= 3), the number of chances to be trapped. Each of the next N lines contains two integers, which are the value of gift in the room and whether have trap in this rooom. Rooms are numbered from 0 to N-1. Each of the next N-1 lines contains two integer A and B(0 <= A,B <= N-1), representing that room A and room B is connected.
  All gifts’ value are bigger than 0.

Output
  For each testcase, output the maximum total value of gifts you can get.

Sample Input
2
3 1
23 0
12 0
123 1
0 2
2 1
3 2
23 0
12 0
123 1
0 2
2 1

Sample Output
146
158


题意:给定一棵树,可以任意选取一点作为起点。每个节点都有一个>0的权值,进入一个结点可以获得其权值,但可能也会进入陷阱,并且不能返回已经进入过的结点。如果进入陷阱达到C次或者没有去路游戏结束。
问能获得权值的最大值。

树形dp,dp[x][i][0]表示x结点向下经过了i个陷阱。
dp[x][i][1]表示x结点向上经过了i个陷阱。
代码:

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
using namespace std;
vector<int> v[50005];
int c[50005],val[50005];
int dp[50005][4][2];
int vis[50005];
int n,C;
int num;
void dfs(int x)
{
    vis[x]=1;
    dp[x][c[x]][0] = dp[x][c[x]][1] = val[x];
    for(int i=0;i<v[x].size();i++)
    {
        int X=v[x][i];
        if(vis[X])
            continue;
        dfs(X);
        for(int j=0;j<=C;j++)
        {
            for(int k=0;k+j<=C;k++)
            {
                if(j != C)
                    num = max(num,dp[x][j][0]+dp[X][k][1]);
                if(k != C)
                    num = max(num,dp[x][j][1]+dp[X][k][0]);
                if(j + k < C)
                    num = max(num,dp[x][j][0]+dp[X][k][0]);
            }
        }
        for(int j=0;j<=C;j++)
        {
            if(j+c[x]>C)
                break;
            dp[x][j+c[x]][0]=max(dp[x][j+c[x]][0],val[x]+dp[X][j][0]);
            if(j)
 //这里是最大的注意点:如果j==0则假如c[x]=1则dp[x][1][1]=
 //max(dp[x][1][1],dp[X][0][1]+val[x])即超过了C还继续走了
                dp[x][j+c[x]][1]=max(dp[x][j+c[x]][1],val[x]+dp[X][j][1]);
        }
    }
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&C);
        for(int i=0;i<=n;i++)
            v[i].clear();
        for(int i=0; i<n; i++)
        {
            scanf("%d%d",&val[i],&c[i]);
        }
        for(int i=0; i<n-1; i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            v[x].push_back(y);
            v[y].push_back(x);
        }
        memset(vis,0,sizeof(vis));
        memset(dp,0,sizeof(dp));
        num=0;
        dfs(0);
        cout<<num<<endl;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值