*(中等) 树形dp POJ 2486 Apple Tree
来源:互联网 发布:手机扒谱软件 编辑:程序博客网 时间:2024/06/05 23:42
Apple Tree
Time Limit: 1000MSMemory Limit: 65536KTotal Submissions: 6405Accepted: 2091
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,每个节点有一定数量的苹果,问从1出发,最多走K步,最多能收集多少的苹果
思路:用两个dp数组来dp,其中dp1[i][j] 表示的是从i点出发,走j步,可以不返回到i点最多能收集的苹果数,dp2[i][j]表示的是从i出发,走j步回到i点最多能收集到的苹果数
那么从一个节点出发,有三种行走方式:
1.从根节点出发往下走,不返回到根了。
2.从根节点出发,往左边的分支走,然后返回到根,再往右边的分支走。
3.从根节点出发,往右边的分支走,然后返回到根,在往左边的分支走。
那么转移方程有三个
dp1[i][j] = max(dp1[i][j],dp2[i][j-k-1]+dp1[son][k]);
dp1[i][j] = max(dp1[i][j],dp2[son][k]+dp1[i][j-k-2]);
dp2[i][j] = max(dp2[i][j],dp2[i][j-k-2]+dp2[son][k]);
要注意的是 这里如果想不清楚先后顺序的话, 可以先把dp1复制出来,因为这些值都应该从当前的状态推出(就是执行该步得到的状态的值应该由上一步的状态推出,而应该避免该步推出的值再推出该步的状态)转移的时候先做dp1的转移,再做dp2的
代码:
#include<cstdio>
#include<algorithm>
#include<set>
#include<cstring>
#include<iostream>
#include<map>
#include<cmath>
#include<queue>
#include<vector>
using namespace std;
const int maxn = 100+10;
const int inf = 0x7fffffff;
#define LL long long
bool vis[maxn];
int dp1[maxn][210];
int dp2[maxn][210];
int tem1[maxn][210];
int w[maxn];
struct Node
{
int v;
Node *next;
}*first[maxn],edge[2*maxn];
int m;
void add(int x,int y)
{
edge[++m].v = y;
edge[m].next = first[x];
first[x] = &edge[m];
}
int K;
void dfs(int x,int dep)
{
vis[x] = true;
Node *p = first[x];
dp2[x][0] = w[x];
for (int i = 0 ; i <= K-dep ; ++i)
dp1[x][i] = w[x];
while (p)
{
int v = p->v;
if (!vis[v])
{
dfs(v,dep+1);
}
p = p->next;
}
}
void tree_dp(int x,int dep)
{
vis[x] = true;
Node *p = first[x];
while (p)
{
int v = p->v;
if (!vis[v])
{
tree_dp(v,dep+1);
memcpy(tem1,dp1,sizeof(dp1));
for (int i = K-dep ; i >= 0 ; --i)
for (int j = 0 ; j < i ; ++j) if (dp2[x][i-j-1]!=-1)
dp1[x][i] = max(dp1[x][i],tem1[v][j]+dp2[x][i-j-1]);
for (int i = K-dep ; i >= 0 ; --i)
for (int j = 0 ; j < i ; ++j) if (dp2[v][j]!=-1)
dp1[x][i] = max(dp1[x][i],dp2[v][j]+tem1[x][i-j-2]);
for (int i = K-dep ; i >= 0 ; --i)
for (int j = 0 ; j < i-1 ; ++j) if (dp2[v][j]!=-1 && dp2[x][i-j-2]!=-1)
dp2[x][i] = max(dp2[x][i],dp2[v][j]+dp2[x][i-j-2]);
}
p = p->next;
}
}
int main()
{
int n;
//freopen("in.txt","r",stdin);
while (scanf("%d%d",&n,&K)==2)
{
for (int i = 1 ; i <= n ; ++i)
scanf("%d",w+i);
memset(vis,0,sizeof(vis));
memset(dp1,0,sizeof(dp1));
memset(dp2,-1,sizeof(dp2));
memset(first,0,sizeof(first));
memset(vis,0,sizeof(vis));
m = 0;
for (int i = 0 ; i < n-1 ; ++i)
{
int x,y;
scanf("%d%d",&x,&y);
add(x,y);
add(y,x);
}
dfs(1,0);
memset(vis,0,sizeof(vis));
tree_dp(1,0);
int ans = 0;
for (int i = 0 ; i <= K ; ++i)
ans = max(ans,max(dp1[1][i],dp2[1][i]));
printf("%d\n",ans);
}
}
0 0
- *(中等) 树形dp POJ 2486 Apple Tree
- 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
- 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
- (男人八题之一)树形dp poj 1741 Tree
- (男人八题)多重背包 POJ 1743 Coins
- ACM牛人给的新手建议
- (中等) 树形dp POJ 1155 TELE
- (简单) 树形背包 HDU 1561 The more, The Better
- *(中等) 树形dp POJ 2486 Apple Tree
- (中等) 树形分组背包 HDU 4003 Find Metal Mineral
- (中等) 树形dp(分组背包) POJ 3345 Bribing FIPA
- C++创建对象的两种方法
- UBUNTU linux 批量删除文件
- *(中等) 树形dp+分组背包 HDU 4276 The Ghost Blows Light
- (简单) 树形dp HDU 3586 Information Disturbing
- (简单) 树形dp POJ 3107 Godfather
- (简单) 树形dp POJ 2378 Tree Cutting
原创粉丝点击
热门IT博客