[关闭]
@xunuo 2017-03-04T12:50:34.000000Z 字数 3538 阅读 997

Apple Tree


Time Limit: 1000MS      Memory Limit: 65536K

树形DP

来源:
poj 2486 Apple Tree
vjudge A-Apple Tree


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 11
1 2
3 2
0 1 2
1 2
1 3

Sample Output

11
2

题意:

一个叔叔给了一个小女孩一颗苹果树,但是又怕她吃胖了,所以呢就规定小女孩吃苹果的时候只能走k步,而小女孩非常喜欢吃苹果,因此就想要在k步之内吃到最多的苹果,hope you can help her......

解题思路:

解题思路...这个东西我并没有(ಥ _ ಥ)
好难啊啊啊啊啊啊啊啊!!!!!!
它的有些东西,到现在都看不懂......(○´・д・)ノ
他状态的转换是这样的:
如果你从 一棵数的顶点向它的某一棵子树v出发,有三种结果:
    1、返回顶点;
    2、留在v子树;
    3、留在其他子树;
故,设DP[u][k]0/1为在u节点为根的子树上走k步并且不回/回到u的最大权值,则它的状态方程为:
    1、dp[u][i][1]=max(dp[u][i][1],dp[v][j-2][1]+dp[u][i-j][1]); //最后要返回顶点
    2、 dp[u][j][0]=max(dp[u][j][0],dp[v][w-1][0]+dp[u][j-w][1]);//留在该子树,故要先把其他的子树跑完了才去v子树
    3、dp[u][j][0]=max(dp[u][j][0],dp[v][w-2][1]+dp[u][j-w][0]);//;留在其他子树,要先跑完v,回去之后才去跑其他的子树

完整代码--------(用邻接表和vector都敲了一次,然而还是不明白)

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<math.h>
  4. #include<vector>
  5. #include<algorithm>
  6. using namespace std;
  7. int num[210],n,k;
  8. int dp[210][410][2];///0表示不回,1表示回
  9. int head[210];
  10. struct node
  11. {
  12. int u,v,val,next;
  13. }tree[510];
  14. int l;
  15. ///***///
  16. void add(int a,int b)
  17. {
  18. tree[l].u=a;
  19. tree[l].v=b;
  20. tree[l].next=head[a];
  21. head[a]=l;
  22. l++;
  23. }
  24. void dfs(int u,int p)
  25. {
  26. for(int i=head[u];i!=-1;i=tree[i].next)
  27. {
  28. int v=tree[i].v;
  29. if(v==p)
  30. continue;
  31. dfs(v,u);
  32. for(int j=k;j>0;j--)
  33. for(int w=1;w<=j;w++)
  34. {
  35. dp[u][j][0]=max(dp[u][j][0],dp[v][w-1][0]+dp[u][j-w][1]);
  36. dp[u][j][0]=max(dp[u][j][0],dp[v][w-2][1]+dp[u][j-w][0]);
  37. dp[u][j][1]=max(dp[u][j][1],dp[v][w-2][1]+dp[u][j-w][1]);
  38. }
  39. }
  40. }
  41. int main()
  42. {
  43. while(scanf("%d%d",&n,&k)!=EOF)
  44. {
  45. memset(num,0,sizeof(num));
  46. memset(dp,0,sizeof(dp));
  47. memset(head,-1,sizeof(head));
  48. for(int i=1;i<=n;i++)
  49. {
  50. scanf("%d",&num[i]);
  51. for(int j=0;j<=k;j++)
  52. dp[i][j][0]=dp[i][j][1]=num[i];
  53. }
  54. int a,b;
  55. l=0;
  56. for(int i=1;i<n;i++)
  57. {
  58. scanf("%d%d",&a,&b);
  59. add(a,b);
  60. add(b,a);
  61. }
  62. dfs(1,0);
  63. int ans=max(dp[1][k][0],dp[1][k][1]);
  64. printf("%d\n",ans);
  65. }
  66. return 0;
  67. }
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<math.h>
  4. #include<vector>
  5. #include<algorithm>
  6. using namespace std;
  7. int dp[210][210][2];///0表示不回,1表示回
  8. int num[210],n,k;
  9. vector<int>g[210];
  10. void dfs(int u,int p)
  11. {
  12. for(int i=0;i<g[u].size();i++)
  13. {
  14. int v=g[u][i];///
  15. if(v==p)
  16. continue;
  17. dfs(v,u);
  18. for(int j=k;j>0;j--)
  19. for(int w=1;w<=j;w++)
  20. {
  21. dp[u][j][0]=max(dp[u][j][0],dp[v][w-1][0]+dp[u][j-w][1]);
  22. dp[u][j][0]=max(dp[u][j][0],dp[v][w-2][1]+dp[u][j-w][0]);
  23. dp[u][j][1]=max(dp[u][j][1],dp[v][w-2][1]+dp[u][j-w][1]);
  24. }
  25. }
  26. }
  27. int main()
  28. {
  29. while(scanf("%d%d",&n,&k)!=EOF)
  30. {
  31. memset(dp,0,sizeof(dp));
  32. for(int i=1;i<=n;i++)
  33. {
  34. scanf("%d",&num[i]);
  35. for(int j=0;j<=k;j++)
  36. dp[i][j][0]=dp[i][j][1]=num[i];
  37. g[i].clear();
  38. }
  39. int a,b;
  40. for(int i=1;i<n;i++)
  41. {
  42. scanf("%d%d",&a,&b);
  43. g[a].push_back(b);
  44. g[b].push_back(a);
  45. }
  46. dfs(1,0);
  47. int ans=max(dp[1][k][0],dp[1][k][1]);
  48. printf("%d\n",ans);
  49. }
  50. return 0;
  51. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注