*(中等) 树形dp POJ 2486 Apple Tree

探讨在一棵苹果树中,从根节点出发,在限定步数内如何收集最多的苹果。使用DP算法解决该问题,通过两种状态转移方程实现最优解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Apple Tree
Time Limit: 1000MSMemory Limit: 65536K
Total 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.

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

题意:给出一颗树,根节点为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);
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值