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.
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
- POJ 2486 Apple Tree 树形DP
- POJ 2486 Apple Tree 树形dp
- POJ--2486--Apple Tree--树形回溯DP
- 【树形DP】 POJ 2486 Apple Tree
- poj 2486 Apple Tree(经典树形DP)
- poj 2486 Apple Tree 树形dp
- *(中等) 树形dp POJ 2486 Apple Tree
- poj 2486 Apple Tree (树形dp)
- poj 2486 Apple Tree(树形dp)
- POJ 2486 Apple Tree(树形dp)
- Apple Tree - POJ 2486 树形dp
- POJ 2486 Apple Tree 树形DP
- poj 2486 Apple Tree (树形dp)
- poj 2486 Apple Tree(树形dp)
- POJ 2486 Apple Tree (树形dp)
- poj 2486 Apple Tree 树形DP
- POJ 2486 Apple Tree(树形dp)
- poj 2486-Apple Tree - 树形DP
- Android工作笔记_两种完全退出所有Activity的解决方案
- shell下路径带空格问题
- CloudB项目概述
- Android之Styles and Themes
- POJ 3979 分数加减法(水题)
- Apple Tree - POJ 2486 树形dp
- 第七周项目三(3) 某员工的周工资
- Layered Windows 实现透明窗体
- Cocos2d-x 更改文字换行风格 ( cocos2dx change line )
- eclipse 快捷键大全
- top_k问题
- 本博客迁移至www.smallgui.com千年小龟龟
- 织梦DedeCMS文章按日期时间归档分类插件
- 位域