练习点: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;
}