poj 2408 Apple Tree

http://poj.org/problem?id=2486node

典型的回溯題目:特別是狀態方程用三維的來標記是否要走迴路。ios

題意:一顆樹,n個點(1-n),n-1條邊,每一個點上有一個權值,求從1出發,走V步,最多能遍歷到的權值
思路:

樹形dp,比較經典的一個樹形dp。首先很容易就能夠想到用dp[root][k]表示以root爲根的子樹中最多走k時所能得到的最多蘋果數,接下去咱們很習慣地會想到將k步在root的全部子結點中分配,也就是進行一次揹包,就能夠得出此時狀態的最優解了,可是這裏還有一個問題,那就是在進行揹包的時候,對於某個孩子son走完以後是否回到根結點會對後面是否還能分配有影響,爲了解決這個問題,咱們只須要在狀態中增長一維就能夠了,用dp[root][k][0]表示在子樹root中最多走k步,最後仍是回到root處的最大值,dp[root][k][1]表示在子樹root中最多走k步,最後不回到root處的最大值。由此就能夠得出狀態轉移方程了:web

Apple Tree
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 6836   Accepted: 2268

Descriptionapp

Wshxzt is a lovely girl. She likes apple very much. One day HX takes her to an apple tree. There are N nodes in the tree. Each node has an amount of apples. Wshxzt starts her happy trip at one node. She can eat up all the apples in the nodes she reaches. HX is a kind guy. He knows that eating too many can make the lovely girl become fat. So he doesn’t allow Wshxzt to go more than K steps in the tree. It costs one step when she goes from one node to another adjacent node. Wshxzt likes apple very much. So she wants to eat as many as she can. Can you tell how many apples she can eat in at most K steps.

Inputspa

There are several test cases in the input   Each test case contains three parts.   The first part is two numbers N K, whose meanings we have talked about just now. We denote the nodes by 1 2 ... N. Since it is a tree, each node can reach any other in only one route. (1<=N<=100, 0<=K<=200)   The second part contains N integers (All integers are nonnegative and not bigger than 1000). The ith number is the amount of apples in Node i.   The third part contains N-1 line. There are two numbers A,B in each line, meaning that Node A and Node B are adjacent.   Input will be ended by the end of file.  
Note: Wshxzt starts at Node 1.

Outputcode

For each test case, output the maximal numbers of apples Wshxzt can eat at a line.

Sample Inputorm

2 1 
0 11
1 2
3 2
0 1 2
1 2
1 3

Sample Outputblog

11
2

dp[root][j][0] = MAX (dp[root][j][0] , dp[root][j-k][0] + dp[son][k-2][0]);//從s出發,要回到s,須要多走兩步s-t,t-s,分配給t子樹k步,其餘子樹j-k步,都返回 three

dp[root][j]][1] = MAX(  dp[root][j][1] , dp[root][j-k][0] + dp[son][k-1][1]) ;//先遍歷s的其餘子樹,回到s,遍歷t子樹,在當前子樹t不返回,多走一步ip

dp[root][j][1] = MAX (dp[root][j][1] , dp[root][j-k][1] + dp[son][k-2][0]);//不回到s(去s的其餘子樹),在t子樹返回,一樣有多出兩步

 

//(1)dp[i][j+2][0] = max(dp[i][j+2][0], dp[i][j-k][0]+dp[son][k][0]);
//(2)dp[i][j+1][1] = max(dp[i][j+1][1], dp[i][j-k][0]+dp[son][k][1]);  人留在i的子節點son的子樹中
//(3)dp[i][j+2][1] = max(dp[i][j+2][1], dp[i][j-k][1]+dp[son][k][0]);  人留在不是son的i的子節點的子樹中
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int dp[300][300][3],head[300],vis[300],w[300];
int len,n,k;
struct node
{
    int now,next;
} tree[505];
int max(int x,int y)
{
    if(x>y)
        return x;
    else
        return y;
}
void add(int x,int y)
{

    tree[len].now = y;
    tree[len].next = head[x];
    head[x] = len++;
}
void dfs(int root,int mark)
{
    int j,son,t,i;

      for(i=0;i<=k;i++)
        dp[root][i][0] = dp[root][i][1] = w[root];
    for(i=head[root];i!=-1;i=tree[i].next)
     {
        printf("i=%d\n",i);
         son = tree[i].now;
         if(son == mark)//已經加了,就不要加,否則就死循環。
            continue;
           dfs(son,root);
              for(j = k; j>=1; j--)
           {
            for(t = 1; t<=j; t++)
            {
                dp[root][j][1]=max(dp[root][j][1],dp[root][j-t][1]+dp[son][t-2][1]);
                dp[root][j][0]=max(dp[root][j][0],dp[root][j-t][1]+dp[son][t-1][0]);
                dp[root][j][0]=max(dp[root][j][0],dp[root][j-t][0]+dp[son][t-2][1]);

            }
          }

     }
}
int main()
{
    int i,a,b,j;
    while(~scanf("%d%d",&n,&k))
    {
            len=0;
        memset(head,-1,sizeof(head));
        memset(dp,0,sizeof(dp));
        memset(vis,0,sizeof(vis));
        memset(w,0,sizeof(w));
        for(i = 1; i<=n; i++)
        {
            scanf("%d",&w[i]);

        }
    for(i=1;i<n;i++)
            {
                cin>>a>>b;
                 add(a,b);
                 add(b,a);
            }
            dfs(1,0);
         printf("%d\n",max(dp[1][k][0],dp[1][k][1]));
    }
    return 0;
}
相關文章
相關標籤/搜索