hdu-1011 Starship Troopers

Starship Troopers

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

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
 
Recommend
JGShining   |   We have carefully selected several similar problems for you:  1561 1203 2955 2415 1114 
 
题意:
是有n个洞组成一棵树,你有m个士兵,你从1号房间开始攻打,每个洞有a个"bugs"和b的价值。你的一个士兵可以打20个"bugs",为了拿到这个洞的价值b你必须留下k个士兵消灭这个洞的所有"bugs"(k*20>="bugs"的数量,且留下的士兵不可以再去攻打其他的洞,且必须攻打了前面的洞才可以攻打后面的洞)。问你花费这m个士兵可以得到的最大价值是多少。 
 
题解:
树形dp,可以看成有依赖的背包。dp[i][j]表示花费j个士兵在以i为根的子树里能获得的最大可能性。
深搜的同时dp,对每一个节点i进行dp时,可以把i的每一个子树看成一个泛化物品(或者一个物品组),每个泛化物品枚举分配多少体积,就转化为了01背包问题。
开始我把m转化成了可以攻打得bug数量,后来发现一个士兵不可以被拆的。
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#define mm 210
#define nn 110
using namespace std;
int e=0,m,fir[nn],nxt[mm],to[mm],bug[nn],pos[nn],dp[nn][nn];
bool vis[nn];
int get()
{
	int ans=0,f=1;char ch=getchar();
	while(!isdigit(ch)) {if(ch=='-') f=-1;ch=getchar();}
	while(isdigit(ch)) {ans=ans*10+ch-'0';ch=getchar();}
	return ans*f;
}
void add(int a,int b)
{
	nxt[++e]=fir[a];fir[a]=e;to[e]=b;
	nxt[++e]=fir[b];fir[b]=e;to[e]=a;
}
void init(int n)
{
	e=0;        ////////////////
	memset(dp,0,sizeof(dp));
	memset(vis,0,sizeof(vis));
	memset(fir,0,sizeof(fir));
	memset(nxt,0,sizeof(nxt));
}
void dfs(int q)
{
	int zc=(bug[q]+19)/20;
	for(int i=zc;i<=m;i++)
	  dp[q][i]=pos[q];
	for(int i=fir[q];i;i=nxt[i])
	  if(!vis[to[i]])
	  {
	  	vis[to[i]]=1;
	  	dfs(to[i]);
	  	for(int j=m;j>=zc;j--)
	  	  for(int k=1;k<=j-zc;k++)
	  	    dp[q][j]=max(dp[q][j],dp[q][j-k]+dp[to[i]][k]);
	  }
}
int main()
{
	int n,a,b;
	while(1)
	{
		n=get();m=get();
		if(n==-1&&m==-1)
		  return 0;
		init(n);
		for(int i=1;i<=n;i++)
		{
			bug[i]=get();pos[i]=get();
		}
		for(int i=1;i<n;i++)
		{
			a=get();b=get();
			add(a,b);
		}
		if(m==0) {printf("0\n");continue;}            //我不知道为什么这里要特判
		vis[1]=1;
		dfs(1);
		printf("%d\n",dp[1][m]);
	}
	return 0;
}

  

posted @ 2017-09-05 15:51  o00v00o  阅读(165)  评论(0编辑  收藏  举报