添加链接描述
Problem Description
You, the leader of Starship Troopers, are sent to destroy a base of the bugs. The base is built underground. It is actually a huge cavern, which consists of many rooms connected with tunnels. Each room is occupied by some bugs, and their brains hide in some of the rooms. Scientists have just developed a new weapon and want to experiment it on some brains. Your task is to destroy the whole base, and capture as many brains as possible.
To kill all the bugs is always easier than to capture their brains. A map is drawn for you, with all the rooms marked by the amount of bugs inside, and the possibility of containing a brain. The cavern’s structure is like a tree in such a way that there is one unique path leading to each room from the entrance. To finish the battle as soon as possible, you do not want to wait for the troopers to clear a room before advancing to the next one, instead you have to leave some troopers at each room passed to fight all the bugs inside. The troopers never re-enter a room where they have visited before.
A starship trooper can fight against 20 bugs. Since you do not have enough troopers, you can only take some of the rooms and let the nerve gas do the rest of the job. At the mean time, you should maximize the possibility of capturing a brain. To simplify the problem, just maximize the sum of all the possibilities of containing brains for the taken rooms. Making such a plan is a difficult job. You need the help of a computer.
Input
The input contains several test cases. The first line of each test case contains two integers N (0 < N <= 100) and M (0 <= M <= 100), which are the number of rooms in the cavern and the number of starship troopers you have, respectively. The following N lines give the description of the rooms. Each line contains two non-negative integers – the amount of bugs inside and the possibility of containing a brain, respectively. The next N - 1 lines give the description of tunnels. Each tunnel is described by two integers, which are the indices of the two rooms it connects. Rooms are numbered from 1 and room 1 is the entrance to the cavern.
The last test case is followed by two -1’s.
Output
For each test case, print on a single line the maximum sum of all the possibilities of containing brains for the taken rooms.
Sample Input
5 10
50 10
40 10
40 20
65 30
70 30
1 2
1 3
2 4
2 5
1 1
20 7
-1 -1
Sample Output
50
7
Source
ZJCPC2004
题意
有n个洞组成一棵树,你有m个士兵,你从1号房间开始攻打,每个洞有a个"bugs"和b的价值。你的一个士兵可以打20个"bugs",为了拿到这个洞的价值b你必须留下k个士兵消灭这个洞的所有"bugs"(k*20>="bugs"的数量,且留下的士兵不可以再去攻打其他的洞,且必须攻打了前面的洞才可以攻打后面的洞)。问你花费这m个士兵可以得到的最大价值是多少。
题解
- 定义DP(i,j):i 为根的树,用 j 个战士最多可以获得的价值。
- 状态转移方程:dp[u][j] = max(dp[u][j], dp[u][j - k] + dp[v][k]);
- 坑点:
- 输入数据中,M和每个房间的bug数可能为0;(检验你的代码能否处理这个特殊情况)
- .输入(N-1)条边时,可能出现大的房间编号在前的情况(比如5 3);
- 一个房间bug数为0时,也需要派至少一个士兵经过该房间获得brain值,但这些士兵可以继续前进;
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 = 3e2 + 5;
const int MOD = 1e9 + 7;struct Edge {
int next;int to;
}edge[MAXN << 1];
int head[MAXN];
int cnt;
int dp[MAXN][MAXN];
int fa[MAXN];
int v[MAXN];//占领获得价值
int w[MAXN];//需要几个士兵
int n, m;
void addEdge(int u, int v) {
edge[cnt].to = v;//edge[i]:第i条边的终点edge[cnt].next = head[u];//head[i]:以i为起点的最后一条边的储存位置head[u] = cnt++;
}
void dfs(int u, int fa) {
for (int i = w[u]; i <= m; i++)dp[u][i] = 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 = m; j >= w[u]; j--) {
for (int k = 1; j - k >= w[u]; k++) {
dp[u][j] = max(dp[u][j], dp[u][j - k] + dp[v][k]);}}}
}
int main() {
while (cin >> n >> m) {
if (n == -1 && m == -1)break;memset(dp, 0, sizeof dp);memset(head, -1, sizeof head);memset(fa, -1, sizeof fa);cnt = 0;for (int i = 1; i <= n; i++) {
cin >> w[i] >> v[i];w[i] = w[i] / 20 + (w[i] % 20 != 0);//0也需要一名战士}for (int i = 1; i < n; i++) {
int a, b;cin >> a >> b;if (a > b) // 针对大编号在前的情况,建树时需注意顺序(无向图则不需要考虑)swap(a, b);addEdge(a, b); fa[b] = a;}int root;for (root = 1; fa[root] != -1; root = fa[root]);if (m == 0) {
cout << 0 << endl;continue;}dfs(root, -1);cout << dp[root][m] << endl;}return 0;
}