POJ 2486 Apple Tree
2015-09-10 22:44
295 查看
Apple Tree
Time Limit: 1000msMemory Limit: 65536KB
This problem will be judged on PKU. Original ID: 2486
64-bit integer IO format: %lld Java class name: Main
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 inputEach 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
Source
POJ Contest,Author:magicpig@ZSU解题:树形dp
#include <iostream> #include <cstdio> #include <vector> #include <cstring> using namespace std; const int maxn = 200; vector<int>g[maxn]; int dp[maxn][maxn][2],w[maxn],n,K; void dfs(int u,int fa) { for(int i = 0; i <= K; ++i) dp[u][i][0] = dp[u][i][1] = w[u]; for(int i = g[u].size()-1; i >= 0; --i) { if(g[u][i] == fa) continue; dfs(g[u][i],u); for(int j = K; j > 0; --j) { for(int k = 1; k <= j; ++k) { dp[u][j][1] = max(dp[u][j][1],dp[u][j - k][0] + dp[g[u][i]][k - 1][1]); if(k >= 2) { dp[u][j][1] = max(dp[u][j][1],dp[u][j-k][1] + dp[g[u][i]][k-2][0]); dp[u][j][0] = max(dp[u][j][0],dp[u][j - k][0] + dp[g[u][i]][k - 2][0]); } } } } } int main() { int u,v; while(~scanf("%d%d",&n,&K)) { for(int i = 1; i <= n; ++i) scanf("%d",w+i); for(int i = 0; i < maxn; ++i) g[i].clear(); for(int i = 1; i < n; ++i) { scanf("%d%d",&u,&v); g[u].push_back(v); g[v].push_back(u); } memset(dp,0,sizeof dp); dfs(1,-1); printf("%d\n",max(dp[1][K][0],dp[1][K][1])); } return 0; }
View Code
相关文章推荐
- android 自定义view 让文字飞起来
- iOS NSAarray的initWithArray:copyItems:方法
- Android 酷炫来袭:制作属于你自己的音频播放器(综合运用MediaPlayer、Service、Broadcast、ListView、SeekBar)
- Android学习——其他View
- android点击空白区域隐藏软键盘
- Android Api Demos登顶之路(六十三)Content-->Resources Smallest Width
- IOS XMPP 学习笔记
- iOS-画板程序(手势操作无)
- iOS-沙盒
- Android常见问题2:Resources$NotFoundException(1)
- android开发遇到的奇葩坑
- LeetCode:Happy Number
- 为什么选择CocoaPods进行iOS代码的管理
- (3)实体和结构【利用objective-c的runtime特性,结合FMDB实现轻量级的ORM】
- ios开发一些零碎的知识点(一)
- 色相(H)饱和度(S)明度(L)与RGB的转换以及在android上的试验
- 安卓判断是否首次进入app,通过判断跳转欢迎引导界面和主界面
- 安卓开发中Theme.AppCompat.Light报错的解决方法
- Android UTF-8转码
- Android中的SQLite数据库的简单使用