POJ 2486——Apple Tree【树形DP】

22 篇文章 0 订阅

题目传送门


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


题意

一棵树n(<=100)个节点,每个节点有a[i]个苹果,从1出发走k(<=200)步,问最多能得到多少个苹果。


题解

  • 首先想到每个节点只有3种情况,要么不经过,要么经过了最后回来,要么经过了最后不回来,只用关心后两种就行。
  • 于是可以想到一个dp[i][j][k],表示从i出发走j步,k=0表示回来,k=1表示不回来,现在想怎么转移。
    转移的关键是每个子节点走多少步,暴力枚举显然是不行的。
    不过仔细想想每个子节点都可以 0 ~ k-1(要回来就是k-2)步,也就是说要把这 k 步分配到所有子节点上,可以联想到分组背包,每个节点的 0 ~ k-1 步为一组,每组内只能选 1 个,组与组之间没有限制,背包容量是 k,然后就可以写出转移了。
    对rt的一个子节点 v :
    枚举剩余容量 j :
    枚举子节点走的步数 k :
  • 状态转移方程
    dp[u][j][0] = max(dp[u][j][0], dp[u][j - k][0] + dp[v][k - 2][0]);
    dp[u][j][1] = max(dp[u][j][1], dp[u][j - k][0] + dp[v][k - 1][1]);
    dp[u][j][1] = max(dp[u][j][1], dp[u][j - k][1] + dp[v][k - 2][0]);

AC-Code

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <fstream>
#include <utility>
using namespace std;
typedef long long ll;
typedef pair<int, int> Pii;
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);

const int INF = 0x7fffffff;
const int MAXN = 2e2 + 5;
const int MOD = 1e4;


struct EDGE {
	int to;
	int next;
}edge[MAXN];
int v[MAXN];
int head[MAXN];
int dp[MAXN][MAXN][2];
int n, k;
int cnt;
void add(int u, int v) {
	edge[cnt].to = v;
	edge[cnt].next = head[u];
	head[u] = cnt++;
}
void dfs(int u, int fa) {
	for (int i = 0; i <= k; i++)
		dp[u][i][0] = dp[u][i][1] = v[u];
	for (int i = head[u]; ~i; i = edge[i].next) {
		int v = edge[i].to;
		if (v == fa)continue;
		dfs(v, u);
		for (int j = ::k; j >= 0; j--) {
			for (int k = 1; j - k >= 0; k++) {
				if (k >= 2)
					dp[u][j][0] = max(dp[u][j][0], dp[u][j - k][0] + dp[v][k - 2][0]);
				dp[u][j][1] = max(dp[u][j][1], dp[u][j - k][0] + dp[v][k - 1][1]);
				if (k >= 2)
					dp[u][j][1] = max(dp[u][j][1], dp[u][j - k][1] + dp[v][k - 2][0]);
			}
		}
	}
}
int main() {
	while (cin >> n >> k) {
		memset(head, -1, sizeof head);
		memset(dp, 0, sizeof dp);
		cnt = 0;
		for (int i = 1; i <= n; i++) {
			cin >> v[i];
		}
		for (int i = 1; i < n; i++) {
			int a, b;
			cin >> a >> b;
			add(a, b);
			add(b, a);
		}
		dfs(1, -1);
		cout << max(dp[1][k][0], dp[1][k][1]) << endl;
	}

	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值