hdu 4616 Game ( 经典树形dp )

Game

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


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

Source

Recommend
zhuyuanchen520


题意:很多房间组成一棵树,每个房间里有礼物。拿到礼物后可能会陷入陷阱。
             最多能逃脱陷阱C次。且不能再回到走过的房间。逃脱后可任选房间继续开始。
             当陷入陷阱达到C次或者没有房间可走,游戏结束。问能获得的最大礼物值。
分析:
对边dp,每条边只有走和不走两种情况。记忆化dp
dp[id][tra]表示以id这条边的儿子节点经过tra个陷阱获得的最大礼物值。
dp[id][i]=max(dp[v][i+trap[v]]+val[u]),v是u的儿子。

感想:
对记忆化dp真的很不会用。。。对递归调用的程序总是思路很不清晰= =
我的基础真的好差啊。。。。~~~啊啊啊,我要提高!!

代码:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#define MAX 50010
using namespace std;

struct node
{
    int u,v;
    int next;
}edge[MAX<<1];
int val[MAX],trap[MAX];
int head[MAX];
int dp[MAX<<1][5];
int cnt,n,c;

void add(int u,int v)
{
    edge[cnt].u=u;
    edge[cnt].v=v;
    edge[cnt].next=head[u];
    head[u]=cnt++;
}
int dfs(int id,int tra)
{
    if(tra==c)
        return 0;
    if(dp[id][tra]!=-1)
        return dp[id][tra];
    int u=edge[id].u;
    int v=edge[id].v;
    int best=val[v];
    int t=tra+trap[v];
    for(int k=head[v];k!=-1;k=edge[k].next)
    {
        if(edge[k].v!=u)
            best=max(best,dfs(k,t)+val[v]);
    }
    return dp[id][tra]=best;
}
int main()
{
    int T,i,a,b,u;
    scanf("%d",&T);
    while(T--)
    {
        memset(dp,-1,sizeof(dp));
        memset(head,-1,sizeof(head));
        scanf("%d%d",&n,&c);
        for(i=0;i<n;i++)
            scanf("%d%d",&val[i],&trap[i]);
        cnt=0;
        for(i=1;i<n;i++)
        {
            scanf("%d%d",&a,&b);
            add(a,b);
            add(b,a);
        }
        int ans=0;
        //printf("test: cnt=%d\n",cnt);
        for(i=0;i<cnt;i++)
        {
            u=edge[i].u;
            ans=max(ans,dfs(i,trap[u])+val[u]);
        }
        printf("%d\n",ans);
    }
    return 0;
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值