G - Starship Troopers (树形dp)

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

   题意:有 n(1<n<=100) 个山洞,每个山洞中都有一些 bug,每个山洞中都有一定的概率包含一个 brain。所有的山洞形成一棵树,现在给你 m(0<=m<=100) 个士兵,每个士兵都能消灭 20 个 bugs,并占领这个山洞,山洞的入口的编号是 1,问怎么安排士兵占领山洞才能使捕获 brain 的概率最大!(即m个士兵如何分派到n个房间里使得获得的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的子节点。

代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
const int maxn=105;

int N, M;
int cost[maxn], brain[maxn];//cost为消耗士兵个数,brain为包含的brain个数
int dp[maxn][maxn];//dp[u][p]表示用 P 个士兵占领以 u 为根节点的子树所能获得的概率最大值
vector<int> son[maxn];//建树建图

void dfs(int p, int pre)//当前节点以及上一节点
{
	for (int i = cost[p]; i <= M; ++i)//初始化
		dp[p][i] = brain[p];
	int num = son[p].size();//第p个节点含有的子节点数
	for (int i = 0; i < num; ++i)//dfs遍历
	{
		int v=son[p][i];
	    if (v==pre) continue;//避免死循环
		dfs(v, p);
		for (int j=M;j>=cost[p];--j)//两个for循环枚举情况
			for (int k=1;k<=j-cost[p];++k)
				if (dp[p][j]<dp[p][j-k]+dp[v][k])
				{
                    dp[p][j]=dp[p][j-k]+dp[v][k];
				}
	}
}

int main()
{
    ios::sync_with_stdio(0);
	while (cin>>N>>M)
	{
	    if(N==-1&&M==-1)break;
		int bug,a,b;
		int i;
		for (i=0;i<N;i++)
		{
			cin>>bug>>brain[i];
			cost[i]=(bug + 19) / 20;
		}
		for (i = 0; i < N; i++)
			son[i].clear();

		for (i=0;i<N-1;i++)
		{
			cin >>a>>b;
			son[a-1].push_back(b-1);
			son[b-1].push_back(a-1);
		}
		if(M==0){//注意M为0时候的情况。。。
            cout<<"0"<<endl;
            continue;
		}
        memset(dp, 0,sizeof(dp));
		dfs(0, -1);
		cout << dp[0][M] << endl;
	}
	return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值