HDU 1011 Starship Troopers(DP+DFS)深度優先探索+ダイナミックプランニング

9144 ワード

Starship Troopers
Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 8444    Accepted Submission(s): 2357
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
ZJCPC2004
 
Recommend
JGShining
 
構想:テーマは大まかに言います:星間騎兵は洞窟の宝を奪い、宝はbrainですが、宝はバグが守っているので、騎兵はバグsを破ってこそ宝を奪うことができ、1人の騎兵は20のバグsを破ることができます.だから、現在のi洞窟のバグsの数がバグ[i]であれば、騎兵はcost=ceil(bug[i]/20)で宝を奪うことができます.私たちの目標はできるだけ多くの宝を取ることです.最大の宝の数を解き、洞窟の分布(図)は木状である.
木の形が見えてDFSを思い浮かべやすく、
ノードを通過するごとに、代価を払って収益を得る必要がある.これはバックパックの問題であり、木状のバックパックである.DP思想では、DPは状態遷移式を見つける必要がある.sodierのノードparentでの収益がGain[parent][sodier]であると仮定すると、状態はGain[son][send]に遷移することができ、sendはサブノードに派遣された騎兵の数を表し、Gain[parent][sodier]=max{Gain[parent][sodier-send],Gain[son][send]},sendの値は[1,sodier-send]であり,1サイクルで比較する[1,sodier-send]、ここで起点が1であれば,派遣兵士は少なくとも1人で宝を得ることができる.
コードスタイルはDFS+DPです.いくつかのバージョンがあります.
solution 1:
//        ,DP
//GetBrain(parent, Soldier) = max{ GetBrain(parent, Soldier-send) + GetBrain(son, send) } 
//1<=send<=Soldier-cost(parent),son parent      }
//Gain[i][j]  (i,j,Gain[i][j])=j    i      Gain[i][j]  
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
using namespace std;

const int N = 110;
int n,SodierSum,Gain[N][N],bug[N],brain[N];

vector<int> g[N];
/*   ,g[i][0]     i       , 1      
2 2-> 5
2 1-> 5-> 3-> 4
2 2-> 4
3 2-> 5-> 3
3 4-> 1-> 2 
*/
bool visited[N];

void init()
{
    memset(visited,false,sizeof(visited));
    memset(Gain,0,sizeof(Gain));
    for(int i=0;i<=n;++i)
        g[i].clear();
}
void dfs(int root)
{
    visited[root]=true;
    int size=g[root].size();

	//    ,         ,    
    int cost=(bug[root]+19)/20;
	//        bugs,        
    if( SodierSum < cost ) 
		return ;
	// cost  ,  sodier     >=cost      
	//     >=cost,        ,  brain[root]
    for(int i=cost;i<=SodierSum;++i)    
        Gain[root][i]=brain[root];

    for(int i=0;i<size;++i)
    {
        int son=g[root][i];//    ,       ,         
        if(visited[son])
			continue;
        dfs(son); //dfs+dp  
        for(int sodier=SodierSum;sodier>=cost;--sodier)//  sodier 
        {
            for(int send=1;send<=sodier-cost;send++)//        sodier-cost,       cost     
				//    ,  
				if( sodier - send >= cost) //       >=cost,    .
					Gain[root][sodier]=max(Gain[root][sodier],Gain[root][sodier-send]+Gain[son][send]);
        }
    }
}
int main()
{
    int x,y;
    while(scanf("%d %d",&n,&SodierSum)==2)
    {
        if(n==-1 && SodierSum==-1)
            break;
        for(int i=1;i<=n;++i)
            scanf("%d %d",&bug[i],&brain[i]);
        init();
        for(int root=1;root<n;root++)
        {
            scanf("%d %d",&x,&y);
            g[x].push_back(y);
            g[y].push_back(x);
        }
        if(SodierSum==0)//         brain
        {
            printf("0
"); continue; } else { dfs(1); cout << Gain[1][SodierSum] << endl; } } return 0; }

solution 2:
#include<iostream>
using namespace std;

const int MAXN=110;
int N,M;

struct Node
{
	int number;	//number:    bug 
	int p;		//p:    possible;
};

Node node[MAXN];//     

int dp[MAXN][MAXN];//DP,dp[i][j]      i ,  j          

int adj[MAXN][MAXN];//   。adj[i][j]:   i,adj[i][0]           (         )  
//      
/*
3 1-> 2-> 3
5 3-> 4-> 5-> 6-> 7
6 9-> 4-> 3-> 2-> 5-> 6
*/
//adj[i][j](j>=1)      ,j=    +1
bool vis[MAXN];//     

int max(int a, int b)
{
	return a>b ? a:b;
}

void dfs(int root)//DFS,               possible
{
	vis[root]=true;//     
	int cost=(node[root].number+19)/20;//             
	//        bugs, brain    

	for(int i=cost;i<=M;i++) 
		dp[root][i]=node[root].p;//  i      possible

	for(int i=1;i<=adj[root][0];i++)//adj[root][0]  root        
	{
		int u=adj[root][i];//         
		if(vis[u]) 
			continue;	//      
		dfs(u);			//        possible
		for(int j=M;j>=cost;j--)//       
		{
			for(int k=1;k<=M-cost;k++)//      k,     1            , 1  ,  !!
			{
				//     ,  !DP  
				if( j-k >= cost ) //       >=cost
	
					dp[root][j]=max(dp[root][j],dp[root][j-k]+dp[u][k]);
			}    
		}    
	}    
}  

int main()
{
	int b,e;
	while(cin >> N >> M)
	{
		if(N==-1&&M==-1) break;
		memset(vis,false,sizeof(vis));
		memset(dp,0,sizeof(dp));
		memset(adj,0,sizeof(adj));
		for(int i=1;i<=N;i++)
			cin >> node[i].number >>node[i].p;
		for(int i=1;i<N;i++)//   
		{
			cin >> b >> e;//    b e
			adj[b][0]++; //  +1
			adj[b][adj[b][0]]=e;// b          e
			adj[e][0]++; //  +1
			adj[e][adj[e][0]]=b;//  
		}
		if(M==0)//     ,    0   ,M=0      
			cout << 0 << endl;
		else
		{
			dfs(1);
			cout << dp[1][M] << endl;
		}
	} 
	return 0;
} 

solution 3:
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<algorithm> 
#include<string.h>

using namespace std;
int map[110][110];
int bug[110];
int bra[110];
int vis[110];
int dp[110][110];
int n,m;
//dp[i][j]   i   j      brain 
//       
//dp[i][j]=max{dp[x][j],dp[x][j-k]+dp[i][k]};
void  dfs(int x,int pre)//     visited   ,    visit       ,      .
{
	int tr=(bug[x]+19)/20;
	for(int i=m;i>=tr;i--)//     
		dp[x][i]=bra[x];  //        bugs, brain    

	for(int i=1;i<=n;++i)
	{
		if(map[x][i]&&pre!=i)
		{
			dfs(i,x);
			for(int j=m;j>=tr;j--)
			{
				for(int k=1;k<=j-tr;k++)
				{
					dp[x][j]=max(dp[x][j],dp[x][j-k]+dp[i][k]);     
				}      
			}
		}      
	}     
} 
int main()
{
	while(cin>>n>>m && (n!=-1||m!=-1))
	{
		for(int i=1;i<=n;i++)
			cin>>bug[i]>>bra[i];
		int a,b;
		memset(map,0,sizeof(map));
		memset(dp,0,sizeof(dp));
		
		for(int i=1;i<n;i++)
		{
			cin>>a>>b;
			map[a][b]=1;//     
			map[b][a]=1;     
		}                
		if(m==0)
		{
			cout<<0<<endl;
			continue;      
		}
		dfs(1,-1);
		cout<<dp[1][m]<<endl;
	}
	
	return 0;
}