树形dp--hdu4616

来源:互联网 发布:荣哥ug编程教学百度云 编辑:程序博客网 时间:2024/05/17 08:12
B - Game
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Submit Status Practice HDU 4616

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

146

158

题意:有棵树上有n个结点,每个点有若干价值的礼物,有些点可能有馅阱,有m次掉入馅阱的机会,但 第m次掉入馅阱后则不能再走,并每个

点最多只能经过一次。问:可以任意点为起点,最多可以拿到总价值多大的礼物。

<pre name="code" class="cpp">#include <iostream>#include <stdio.h>#include <queue>#include <string.h>using namespace std;const int maxn=50050,maxm=50050*2;struct Edge{    int to,next;} edge[maxm];int head[maxn],tot;int trap[maxn],ans;int val[maxn];int vis[maxn];int dp[maxn][4][2];/**dp[node][i][0]: node节点 在 消耗i陷阱时 并从该节点往下走(或者理解为还有能力往下走)的最大权值dp[node][i][1]: node节点 在 消耗i陷阱时 并从子节点往上走(到该节点或者理解为没有能力接着走了)的最大权值*/int trap_Num;void init(){    memset(head,-1,sizeof(head));    tot=0;}void add_edge(int u,int v){    edge[tot].to=v;    edge[tot].next=head[u];    head[u]=tot++;}void dfs(int u){    vis[u]=1;    dp[u][trap[u]][0]=dp[u][trap[u]][1]=val[u];///初始化    for(int i=head[u];i!=-1;i=edge[i].next)    {        int v=edge[i].to;        if(vis[v])            continue;        dfs(v);        for(int i=0;i<=trap_Num;i++)        {            for(int j=0;j+i<=trap_Num;j++)            {                if(i!=trap_Num)                ans=max(ans,dp[u][i][0]+dp[v][j][1]);///可以从任何一点出发                if(j!=trap_Num)                      /// 所以要研究从u向下(子树)发展的val                ans=max(ans,dp[u][i][1]+dp[v][j][0]);///和从u向上(父树)发展的val                if(i+j<trap_Num)                    ans=max(ans,dp[u][i][0]+dp[v][j][0]);            }        }        for(int i=0;i<=trap_Num;i++)        {            if(i+trap[u]>trap_Num)                break;            dp[u][i+trap[u]][0]=max(dp[u][i+trap[u]][0],val[u]+dp[v][i][0]);            if(i)                dp[u][i+trap[u]][1]=max(dp[u][i+trap[u]][1],val[u]+dp[v][i][1]);        }    }}int main(){    int n,t;    int u,v;    scanf("%d",&t);    while(t--)    {        scanf("%d%d",&n,&trap_Num);        init();        for(int i=0; i<n; i++)            scanf("%d%d",&val[i],&trap[i]);        for(int i=1; i<n; i++)        {            scanf("%d%d",&u,&v);            add_edge(u,v);            add_edge(v,u);        }        memset(vis,0,sizeof(vis));        memset(dp,0,sizeof(dp));        ans=0;        dfs(0);        printf("%d\n",ans);    }    return 0;}


0 0
原创粉丝点击