HDU 4616 Game 【有限制的树型dp】

来源:互联网 发布:ubuntu还是deepin 编辑:程序博客网 时间:2024/04/28 22:41

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
 

才发现居然这个题忘记写了惊恐orz我不是有意的委屈

  1. dp[node][i][0]: node节点 在 消耗i陷阱时 并从该节点往下走(或者理解为还有能力往下走)的最大权值 
  2.  dp[node][i][1]: node节点 在 消耗i陷阱时 并从子节点往上走(到该节点或者理解为没有能力接着走了)的最大权值
也可以理解为:dp[i][j][flag]:在根为i的子树里,从一点跑到i的最大价值,flag为0代表起点不为陷阱,1代表起点为陷阱。
更新ans时,相当于将根为i的子树的一条树链合上i的相邻节点的一条树链,枚举得到最大值,更新ans即可。
每次更新全局最优之前,先在局部最优的情况下更新ans。

貌似后一种说法更好理解(不及时写博客的恶果啊==找那个看得懂的题解找了好久)

推荐:点击打开链接

#include<cstdio>    #include<iostream>    #include<cstring>    #include<algorithm>    using namespace std;    const int mm=100010;    int head[mm],nxt[mm],ver[mm];    /**head[i]表示第i个人指向的链表头*/    /**ver[i]表示链表中的数值*/    /**nxt[i]表示链表元素的下一个元素*/    int dp[mm][5][2];    int trap[mm],gift[mm];    int n,edge,cc,ans;/**n表示多少条读入,size表示已编号人数,edge表示关系边数*/    void addedge(int u,int v)/**建立关系边,这个是用数组模拟指针链表,效率会高一点,也好写一点*/    {        ver[edge]=v,nxt[edge]=head[u],head[u]=edge++;        /**给u的链表增加元素,具体自己手动试一下就明白了*/    }    void treeDP(int u,int pre)/**树型DP,一般都是以搜索的形式来做*/    {        dp[u][trap[u]][0]=dp[u][trap[u]][1]=gift[u];        for(int i=head[u];i!=-1;i=nxt[i])        {            int v=ver[i];            if(v==pre) continue;            treeDP(v,u);            for(int j=0;j<=cc;j++)            {                for(int k=0;k+j<=cc;k++)                {                    if(j!=cc) ans=max(ans,dp[u][j][0]+dp[v][k][1]);                    if(k!=cc) ans=max(ans,dp[u][j][1]+dp[v][k][0]);                    if(j+k<cc) ans=max(ans,dp[u][j][0]+dp[v][k][0]);                }            }            for(int j=0;j<=cc;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()    {       // freopen("cin.txt","r",stdin);        int ttt;        scanf("%d",&ttt);        while(ttt--)        {            scanf("%d%d",&n,&cc);            edge=ans=0;            memset(head,-1,sizeof(head));            memset(dp,0,sizeof(dp));            for(int i=0;i<n;i++)scanf("%d%d",&gift[i],&trap[i]);            for(int i=1;i<n;i++)            {                int u,v;                scanf("%d%d",&u,&v);                addedge(u,v);                addedge(v,u);            }            treeDP(0,-1);            printf("%d\n",ans);        }        return 0;    }


0 0
原创粉丝点击