hdu4616 树形dp

Game

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


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
 

Author
SYSU
 

Source

2013 Multi-University Training Contest 2


题意:在一棵树上有n个节点,每个节点有一些宝藏,同时,一些节点可能有障碍,当你进入房间遇到障碍就会影响你的接下来进行了,当你遇到C次障碍就game over ,问最多能获得多少宝藏;(可以从任意一个节点出发)


从每个节点出发,进行枚举,用dp[id][num]表示从 编号为id 的 边u节点出发,遇到了num次障碍的最大宝藏值,然后记忆化搜索

#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
const int maxn= 50000*3;
int C;
struct Edge
{
    int u,v,next;
}edge[maxn];
int tot,head[maxn];
int value[maxn],trap[maxn];
int dp[maxn][5];
void init()
{
    tot=0;
    memset(head,-1,sizeof(head));
    memset(dp,-1,sizeof(dp));
}

void add_edge(int u,int v)
{
    edge[tot].u=u;
    edge[tot].v=v;
    edge[tot].next=head[u];
    head[u]=tot++;
}

int dfs(int num,int id)
{
    if(num==C)
        return 0;
    if(dp[id][num]!=-1)
    return dp[id][num];
    int u=edge[id].u;
    int v=edge[id].v;
    int temp=value[v];
    int t=num+trap[v];

    for(int i=head[v];i!=-1;i=edge[i].next)
    {
        if(edge[i].v!=u)
            temp=max(temp,dfs(t,i)+value[v]);
    }
     dp[id][num]=temp;
     return dp[id][num];
}


int main()
{
    int t,n;
    int u,v;
    scanf("%d",&t);
    while(t--)
    {
        init();
        scanf("%d%d",&n,&C);
        for(int i=0;i<n;i++)
        {
            scanf("%d%d",&value[i],&trap[i]);
        }
        for(int i=1;i<n;i++)
        {
             scanf("%d%d",&u,&v);
             add_edge(u,v);
             add_edge(v,u);
        }

        int ans=0;
        for(int i=0;i<tot;i++)
        {
            ans=max(ans, dfs(trap[edge[i].u],i)+value[edge[i].u]);///枚举每个点出发
        }
        printf("%d\n",ans);


    }
//    cout << "Hello world!" << endl;
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值