POJ 2486 Apple Tree (经典树形DP)

题目链接:https://vjudge.net/problem/POJ-2486

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.

Input

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.

Output

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

Sample Input

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

Sample Output11

2

 

题意:一棵树,一共n个点,每个点上有一个权值,求从1出发,走k步,最多能遍历到的权值,可以往回走。

题解:设dp[0][s][j]表示从s(当前根节点)出发,走 j 步,回到s所能获得的最大权值
dp[1][s][j]表示从s(当前根节点)出发,走j步,不回到s所能获得的最大权值
1、在t子树返回,其他子树也返回,即回到当前根节点s
2,、不返回根节点,但在t子树返回,即相当于从t出发走k步返回t的最优值
加上 从s出发走j-k步到其他子树不返回的最优值,
中间有s与t连接起来,其实就等于从s出发遍历t子树后(dp[0][t][k])
又回到s(这一步多了中间的来回两步),再走出去(其他子树)【dp[1][s][j-k]】,不回来
3、不返回根节点,在t子树也不返回,等价于从s出发遍历其他子树,
回到s(dp[0][s][j-k]),再走向t子树,不回到t(dp[1][t][k]),这个过程s-t只走了一步。

 

AC代码:

#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm> 
using namespace std;
vector<int> g[1010];
int dp[2][1010][210];
int n,V,val[1010];
bool used[1010];

void dfs(int s){
    int i;
    used[s]=true;
    for(i=0;i<=V;i++) dp[0][s][i]=dp[1][s][i]=val[s];//强制加入
    for(i=0;i<g[s].size();i++){
        int t=g[s][i];
        if(used[t]) continue;
        dfs(t);
        for(int j=V;j>=0;j--){
            for(int k=0;k<=j;k++){//中间的桥梁一定要走,所以把状态依次转变过来
            	//需要多走两步s-t,t-s
                dp[0][s][j+2]=max(dp[0][s][j+2],dp[0][t][k]+dp[0][s][j-k]);
                //需要多走两步s-t,t-s
                dp[1][s][j+2]=max(dp[1][s][j+2],dp[0][t][k]+dp[1][s][j-k]);
                //需要多走一步s-t
                dp[1][s][j+1]=max(dp[1][s][j+1],dp[1][t][k]+dp[0][s][j-k]);
            }
        }
    }
}
int main(){
    int i,a,b;
    while(scanf("%d%d",&n,&V)!=EOF){
        for(i=0;i<=1000;i++)
            g[i].clear();
        for(i=1;i<=n;i++)
            scanf("%d",&val[i]);
        for(i=0;i<n-1;i++){
            scanf("%d%d",&a,&b);
            g[a].push_back(b);
            g[b].push_back(a);
        }
        memset(dp,0,sizeof(dp));
        memset(used,false,sizeof(used));
        dfs(1);
        printf("%d\n",dp[1][1][V]);
    }
    return 0;
}

  

 

 

posted @ 2018-09-04 21:24  ccsu_dj辉  阅读(129)  评论(0编辑  收藏  举报