hdu4616 树形dp(有限制的权值最大链)

来源:互联网 发布:证大财富淘宝贷假不假 编辑:程序博客网 时间:2024/04/30 11:50

http://acm.hdu.edu.cn/showproblem.php?pid=4616

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
23 123 012 0123 10 22 13 223 012 0123 10 22 1
 

Sample Output
146158
 

Author
SYSU
 

Source
2013 Multi-University Training Contest 2
/**hdu4616  树形dp(有限制的权值最大链)题目大意:给定一棵树,每一个节点有一个权值,有的节点可能存在陷阱,经过一个顶点可以能获得权值但也经历陷阱。找出一条路径,起点自选,          使在经历陷阱数不超过C的情况下,获得的最大的权值是多少?解题思路:一道很好的树形dp。如果把陷阱去掉,应该算一个经典的题目。其实加上一样:dp[u][j][k]表示到节点u经过j个陷阱,并要往根节点方向          或要往叶子节点方向去,所获得最大权值(1表示往叶子节点,0往根节点)          有一点值得一提:就是如果u点有陷阱,那么dp[u][1][1]不能由dp[v][0][1]转移过来,因为一旦限制为1,那么就不能由u往叶子方向走了          这一点很难理解。我们在求最大值是会用到dp[u][0][1]的,怎么办呢?我们发现dp[u][0][1]是可以由dp[u][0][0]代替的。*/#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>using namespace std;const int maxn=50050;int head[maxn],ip;int n,m,ans;void init(){    memset(head,-1,sizeof(head));    ip=0;}struct note{    int v,next;}edge[maxn*2];void addedge(int u,int v){    edge[ip].v=v,edge[ip].next=head[u],head[u]=ip++;}int dp[maxn][5][2],gift[maxn],trap[maxn];void dfs(int u,int pre){    dp[u][trap[u]][0]=dp[u][trap[u]][1]=gift[u];    for(int i=head[u];i!=-1;i=edge[i].next)    {        int v=edge[i].v;        if(v==pre)continue;        dfs(v,u);        for(int j=0;j<=m;j++)        {            for(int k=0;k+j<=m;k++)            {                if(j!=m)ans=max(ans,dp[u][j][0]+dp[v][k][1]);                if(k!=m)ans=max(ans,dp[u][j][1]+dp[v][k][0]);                if(j+k<m)ans=max(ans,dp[u][j][0]+dp[v][k][0]);            }        }        for(int j=0;j<=m;j++)        {            dp[u][j+trap[u]][0]=max(dp[u][j+trap[u]][0],dp[v][j][0]+gift[u]);            if(j!=0)            {                dp[u][j+trap[u]][1]=max(dp[u][j+trap[u]][1],dp[v][j][1]+gift[u]);            }        }    }}int main(){    int T;    scanf("%d",&T);    while(T--)    {        scanf("%d%d",&n,&m);        for(int i=0;i<n;i++)        {            scanf("%d%d",&gift[i],&trap[i]);        }        init();        for(int i=0;i<n-1;i++)        {            int u,v;            scanf("%d%d",&u,&v);            addedge(u,v);            addedge(v,u);        }        ans=0;        memset(dp,0,sizeof(dp));        dfs(0,-1);        printf("%d\n",ans);    }    return 0;}


0 0
原创粉丝点击