Apple Tree - POJ 2486 树形dp

来源:互联网 发布:键盘按键显示软件 编辑:程序博客网 时间:2024/06/10 19:01

Apple Tree
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 7356 Accepted: 2468

Description

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 111 23 20 1 21 21 3

Sample Output

112

题意:在一棵树上,从根节点开始,可以走k步,每个节点都有权值,问走到的路径的权值和最大是多少。

思路:dp[i][j][0]表示从i节点向下走了j步并且回到i节点的最大权值和,dp[i][j][1]表示从i节点向下走了j步并且不要求回到i节点的最大权值和。很容易想出三个转移方程,dp[u][j][0]=max(dp[u][j][0],dp[u][j-k][0]+dp[v][k-2][0])回来的加上回来的,dp[u][j][1]=max(dp[u][j][1],dp[u][j-k][1]+dp[v][k-2][0])和dp[u][j][1]=max(dp[u][j][1],dp[u][j-k][0]+dp[v][k-1][1])。

AC代码如下:

#include<cstdio>#include<cstring>#include<vector>#include<algorithm>using namespace std;int n,m,val[110],dp[110][210][2],ans;vector<int> vc[110];void dfs(int f,int u){    int i,j,k,len=vc[u].size(),v;    for(i=0;i<len;i++)    {        v=vc[u][i];        if(v==f)          continue;        dfs(u,v);        for(j=m;j>=1;j--)           for(k=1;k<=j;k++)           {              dp[u][j][1]=max(dp[u][j][1],dp[u][j-k][0]+dp[v][k-1][1]);              if(k>1)              {                  dp[u][j][1]=max(dp[u][j][1],dp[u][j-k][1]+dp[v][k-2][0]);                  dp[u][j][0]=max(dp[u][j][0],dp[u][j-k][0]+dp[v][k-2][0]);              }           }    }}int main(){    int i,j,k,u,v;    while(~scanf("%d%d",&n,&m))    {        for(i=1;i<=n;i++)           vc[i].clear();        memset(dp,0,sizeof(dp));        for(i=1;i<=n;i++)        {            scanf("%d",&val[i]);            dp[i][0][0]=dp[i][0][1]=val[i];        }        for(i=1;i<n;i++)        {            scanf("%d%d",&u,&v);            vc[u].push_back(v);            vc[v].push_back(u);        }        ans=0;        dfs(0,1);        for(j=0;j<=m;j++)           ans=max(ans,max(dp[1][j][0],dp[1][j][1]));        printf("%d\n",ans);    }}




0 0