HDOJ1011 Starship Troopers

练习点:DFS,DP

http://acm.hdu.edu.cn/showproblem.php?pid=1011

 

#include<vector>
#include<cstdio>
#include<memory.h>
#include <iostream>
using namespace std;
#define N 101
int num_bug_cost[N];
int posb_roomnow_tropnow[N][N];
vector<int> path[N];
bool visited[N];
int num_room;
int num_trooper;
int max(int a,int b)  
{  
    return a>b?a:b;  
}  
void Starship(int root_rm)
{
	visited[root_rm] = true;
	for (int i = 0; i <path[root_rm].size(); i++)
	{
		int tmp_room = path[root_rm].at(i);
		if (visited[tmp_room])
		{
			continue;
		}
		Starship(tmp_room);
		for (int j = num_trooper - 1; j >= num_bug_cost[root_rm]; j--) //至少还能容下一个trooper 
		{
			for (int index=1;index+j<=num_trooper;index++)
			{
				if(posb_roomnow_tropnow[tmp_room][index])
				{
					posb_roomnow_tropnow[root_rm][j+index]=max(posb_roomnow_tropnow[root_rm][j+index],posb_roomnow_tropnow[root_rm][j]+posb_roomnow_tropnow[tmp_room][index]);  
      			}  
  			} 
		}
	}
}

int main()
{	
	while (scanf("%d%d", &num_room, &num_trooper) != EOF)
	{
		if (num_room==-1 && num_trooper==-1)
			break; 
		memset(num_bug_cost, 0, sizeof(num_bug_cost));
		memset(posb_roomnow_tropnow, 0, sizeof(posb_roomnow_tropnow));
		memset(visited, 0, sizeof(visited)); //修正忘记清空 
		for (int i = 1; i<=num_room; i++)
		{
			int possibility;
			scanf("%d%d", &num_bug_cost[i], &possibility);
			num_bug_cost[i] = (num_bug_cost[i] + 19)/20;
			for (int j=num_bug_cost[i];j<=num_trooper;j++)
			{
				posb_roomnow_tropnow[i][j]=possibility;
			} 
		}
		for (int i = 0; i<=num_room; i++)
		{
			path[i].clear();
		}
		
		for (int i = 1; i <num_room; i++)
		{
			int tmp_rm, tmp_adj_rm;
			scanf("%d%d", &tmp_rm, &tmp_adj_rm);
			path[tmp_rm].push_back(tmp_adj_rm);
			path[tmp_adj_rm].push_back(tmp_rm);
		}
		if (num_trooper==0)
			cout<<"0"<<endl;
		else{
			Starship(1);
			cout<<posb_roomnow_tropnow[1][num_trooper]<<endl;
		}
		
		
	}
	return 0;
} 




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值