HDU1011 Starship Troopers[Tree DP]

Topic link: HDU1011 Starship Troopers

The meaning of the question: n rooms, m soldiers, given the number and value of bugs in each room, each soldier can eliminate 20 bugs, for each room, only the current bugs are eliminated before they can enter the room connected to it. , If there is no bug in the current room, a soldier should be kept, and the total value that can be obtained is the most;

Analysis: dp[i][j] represents the value of j soldiers in room i;

#include<bits/stdc++.h>
#define pb push_back
using namespace std;
const int maxn=105;
vector<int> g[maxn];
int n,m;
int a[maxn],bug[maxn],dp[maxn][maxn],b[maxn];
void dfs(int x,int fa)
{
	for(int i=b[x];i<=m;i++) dp[x][i]=a[x];
	for(auto y:g[x])
	{
		if(y==fa) continue;
		dfs(y,x);
		for(int j=m;j>=b[x]+1;j--)//正着扫会重复
			for(int k=1;k<=j-b[x];k++)
				dp[x][j]=max(dp[x][j],dp[y][k]+dp[x][j-k]);
	}
}
void rua()
{
	if(n==-1 && m==-1) return;
	for(int i=0;i<=n;i++) g[i].clear();
	memset(dp,0,sizeof(dp));
	for(int i=1;i<=n;i++) scanf("%d%d",&bug[i],&a[i]),b[i]=(bug[i]+19)/20;
	for(int i=1;i<n;i++)
	{
		int x,y;scanf("%d%d",&x,&y);
		g[x].pb(y);g[y].pb(x);
	}
	if(!m) {puts("0");return;}
	dfs(1,1);
	printf("%d\n",dp[1][m]);
}
int main()
{
	while(~scanf("%d%d",&n,&m)) rua();
	return 0;
}

 

Guess you like

Origin blog.csdn.net/qq_43813163/article/details/102609310