poj 2486 Apple Tree (树形dp)
来源:互联网 发布:vmware网络配置文件 编辑:程序博客网 时间:2024/06/04 19:39
Apple Tree
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 6674 Accepted: 2208
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号节点,能走m步,问你最多能得到几个苹果。
思路:
树形dp,从每个点走后可以回来或者不回来,可以走k步,构造状态
dp[2][j][k] 01-表示是否回来 j-在哪个点 k-能走几步 dp-能得到的最大苹果数
于是可以分为三种情况:
1.从一个点出发最终回到该点。
2.从一个点出发最终不回该点,走到刚访问了的子树。
3.从一个点出发最终不回该点,走到以前访问过的子树。
转移方程见代码
那么每种状态都能合理转移了,注意循环的顺序。
代码:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <string>#include <map>#include <stack>#include <vector>#include <set>#include <queue>#pragma comment (linker,"/STACK:102400000,102400000")#define maxn 205#define MAXN 400005#define mod 1000000009#define INF 0x3f3f3f3f#define pi acos(-1.0)#define eps 1e-6typedef long long ll;using namespace std;int n,m,k,ans,cnt,tot,flag;int pp[maxn],num[maxn],dp[2][maxn][maxn];struct Node{ int v,w,next;} edge[maxn];void addedge(int u,int v,int w){ cnt++; edge[cnt].v=v; edge[cnt].w=w; edge[cnt].next=pp[u]; pp[u]=cnt;}void dfs(int u,int pre){ int i,j,t,v; for(j=0; j<=m; j++) { dp[0][u][j]=dp[1][u][j]=num[u]; } for(i=pp[u]; i; i=edge[i].next) { v=edge[i].v; if(v!=pre) { dfs(v,u); for(j=m; j>=0; j--) { for(k=0; k<=j; k++) { dp[0][u][j+1]=max(dp[0][u][j+1],dp[0][v][k]+dp[1][u][j-k]); dp[0][u][j+2]=max(dp[0][u][j+2],dp[1][v][k]+dp[0][u][j-k]); dp[1][u][j+2]=max(dp[1][u][j+2],dp[1][v][k]+dp[1][u][j-k]); } } } }}void solve(){ int i,j,t; dfs(1,0); ans=0; for(i=0; i<=m; i++) { ans=max(ans,dp[0][1][i]); ans=max(ans,dp[1][1][i]); }}int main(){ int i,j,t; while(~scanf("%d%d",&n,&m)) { for(i=1; i<=n; i++) { scanf("%d",&num[i]); } int u,v; cnt=0; memset(pp,0,sizeof(pp)); for(i=1; i<n; i++) { scanf("%d%d",&u,&v); addedge(u,v,1); addedge(v,u,1); } solve(); printf("%d\n",ans); } return 0;}/*2 10 111 23 20 1 21 21 3*/
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
- ORA-12505, TNS:listener does not currently know of SID given in connect descriptor
- hdu 1297 Children’s Queue
- JSP过滤器Filter配置过滤类型汇总
- 简单的带空格等式计算
- 黑客非法探取密码的原理及安全防范
- poj 2486 Apple Tree (树形dp)
- 黑马程序员_12_7k面试题之交通灯
- Java 反射机制浅析
- WebRTC源码分析二:音频模块结构
- java 复制
- 2014我的职业规划
- iTVLobby问题(5)
- [C#] 计算大文件的MD5的两种方式(直接调用方法计算,流计算-适用于大文件)
- ELF格式文件符号表全解析及readelf命令使用方法