poj2486 Apple Tree 树形dp
来源:互联网 发布:淘宝海报在线制作 编辑:程序博客网 时间:2024/05/18 12:01
Apple Tree
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 7260 Accepted: 2435
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
Source
POJ Contest,Author:magicpig@ZSU
题目大意:给你一颗树,每个点都有权值,现在求从1出发最多走k步能获得的最大价值
解题思路:
用dp[i][j][2]表示从i点开始走j步是否回到i点可以得到的最大价值,然后枚举状态,进行背包就行了
#include <iostream>#include <cstdio>#include <cstring>#include <vector>#define maxn 110using namespace std;int n,k;int va[maxn];vector<int> G[110];void init(){ for(int i=0;i<110;i++) G[i].clear();}void read(){ for(int i=1;i<=n;i++) scanf("%d",&va[i]); int u,v; for(int i=0;i<n-1;i++){ scanf("%d%d",&u,&v); G[u].push_back(v); G[v].push_back(u); }}int dp[maxn][210][2];void dfs(int u,int fa){ dp[u][0][1]=va[u],dp[u][0][0]=va[u]; int nn=G[u].size(); for(int i=0;i<nn;i++){ int v=G[u][i]; if(v!=fa){ dfs(v,u); for(int j=k;j>=0;j--){ for(int p=k;p>=0;p--){ if(j+p+2<=k) dp[u][j+p+2][0]=max(dp[u][j+p+2][0],dp[u][j][0]+dp[v][p][1]); if(j+p+1<=k) dp[u][j+p+1][0]=max(dp[u][j+p+1][0],dp[u][j][1]+dp[v][p][0]); if(j+p+2<=k) dp[u][j+p+2][1]=max(dp[u][j+p+2][1],dp[u][j][1]+dp[v][p][1]); } } } }}void solve(){ memset(dp,0,sizeof dp); dfs(1,0); int ret=0; for(int i=0;i<=k;i++){ ret=max(ret,max(dp[1][i][0],dp[1][i][1])); } printf("%d\n",ret);}int main(){ while(~scanf("%d%d",&n,&k)){ init(); read(); solve(); } return 0;}
0 0
- POJ2486:Apple Tree(树形DP)
- poj2486 Apple Tree 树形dp
- 【poj2486】【Apple Tree】【树形dp】
- poj2486 Apple Tree 树形DP
- POJ2486 Apple Tree【树形dp】
- POJ2486 Apple Tree 树形DP
- poj2486 Apple Tree (树形dp)
- poj2486 Apple Tree 树形dp背包
- poj2486 Apple Tree 树形dp好题
- POJ2486 apple tree(树形DP)
- POJ2486:Apple Tree(树形DP) ★
- poj2486--Apple Tree(树状dp)
- poj2486 Apple Tree(树形DP,有反回的情况,求最大)........很典型
- Apple Tree--树形DP
- Apple Tree(树形DP)
- POJ2486--Apple Tree
- poj2486 Apple Tree
- poj2486 Apple Tree
- Python24中使用urllib时遇到IOError的正确打开方式
- 我希望 明天的自己 像一道彩虹 还是刚淋过雨
- POJ动态规划题目列表
- first struts2 project
- 驾驭你的“职场布朗运动”
- poj2486 Apple Tree 树形dp
- C-CommandObject
- 仿写版《陈情表》
- 设置UIView能否被点击
- 程序员生存定律-六个程序员的故事(1)
- 少侠请小心介绍
- ExtJs事件处理
- n节点组成二叉树的个数
- ZOJ3800 GCD+离线线段树