概述
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.
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.
The last test case is followed by two -1's.
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
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;
}
最后
以上就是帅气大叔为你收集整理的G - Starship Troopers (树形dp)的全部内容,希望文章能够帮你解决G - Starship Troopers (树形dp)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复