杭电OJ——1011 Starship Troopers(dfs + 树形dp)

Starship Troopers

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
 
Author
XU, Chuan
 
Source
 
Recommend
JGShining
 
         说实话,第一次做树形dp这类题目,答案参考网上做的,有点头疼,应该做多了就好了吧,凡事都是熟能生巧!    

      有 n(1<n<=100) 个山洞,每个山洞中都有一些 bug,每个山洞中都有一定的概率包含一个 brain。所有的山洞形成一棵树,现在给你 m(0<=m<=100) 个士兵,每个士兵都能消灭 20 个 bugs,并占领这个山洞,山洞的入口的编号是 1,问怎么安排士兵占领山洞才能使捕获 brain 的概率最大!

        典型的树上背包问题

        定义状态 f[u][P] 表示用 P 个士兵占领以 u 为根节点的子树所能获得的概率最大值,状态转移就是一个树形DP过程,目标状态就是 f[1][m]。

       f[u][p] = max {f[u][p], f[u][p - k] + f[v][k] };其中v是u的子节点。

       代码如下:

 

/*比较苦逼的树形DP,慢慢来吧!不着急*/
#include <iostream>
#include <vector>
using namespace std;
const int SIZE = 105;

int roomNumber, trooperNumber;
int cost[SIZE], brain[SIZE];
int dp[SIZE][SIZE];   /*dp[u][p]表示用 P 个士兵占领以 u 为根节点的子树所能获得的概率最大值*/
vector<int> adj[SIZE];   /*图*/

void dfsPulsDp(int p, int pre)
{
	for (int i = cost[p]; i <= trooperNumber; ++i)   /*初始化,首先将dp[p][i]里面填充进brain[p],后面可以更新dp[p][i]的值*/
		dp[p][i] = brain[p]; /*也就是说当我们有cost[p]名队员以至于更多时,我们最少可以获得brain[p]个大脑*/
	int num = adj[p].size();   /*num指p节点含有的支路数*/
	for (int i = 0; i < num; ++i)  /*一条支路一条支路遍历,也就是所谓的dfs*/
	{
		int v = adj[p][i];
	    if (v == pre) continue;  /*避免死循环,节点如果是根部,就继续*/
		dfsPulsDp (v, p);   /*递归解决问题,先将子节点的所能得到的最大值计算出来*/

		for (int j = trooperNumber; j >= cost[p]; --j)  /*当队员人数一定时*/
			for (int k = 1; k <= j - cost[p]; ++k)     /*由于p节点一定要通过,所以一定要花费cost[p]*/
				if (dp[p][j] < dp[p][j - k] + dp[v][k])
				{/*v节点就两种状态,要么选择,要么不选择,选择的话dp[p][j] = dp[p][j - k] + dp[v][k],不选择的话就不变*/
                    dp[p][j] = dp[p][j - k] + dp[v][k];
				}
	}
}

int main()
{
	while ((cin >> roomNumber >> trooperNumber) && (roomNumber != -1) && (trooperNumber != -1))
	{
		int bug, bi1, bi2;
		int i;
		for (i = 0; i < roomNumber; i++)
		{
			cin >> bug >> brain[i];
			cost[i] = (bug + 19) / 20;
		}
		for (i = 0; i < roomNumber; i++)
			adj[i].clear();

		for (i = 0; i < roomNumber - 1; i++)
		{
			cin >> bi1 >> bi2;
			adj[bi1 - 1].push_back(bi2 - 1);
			adj[bi2 - 1].push_back(bi1 - 1);
		}

		if (trooperNumber == 0)
		{
			cout << '0' << endl;
			continue;
		}

        memset(dp, 0, sizeof(dp));     
		dfsPulsDp(0, -1);     
		cout << dp[0][trooperNumber] << endl;
	}
	return 0;
}


原文地址:https://www.cnblogs.com/jiangu66/p/3161501.html