题意:从起点出发,每个节点都有bugs和brain,需要击杀bugs获得brain。击杀bugs需要消耗士兵。问题是怎样获得最多的brain。
求解:基本思路跟1561一样。需要特别注明的有两点:
1、m==0时需要特别处理,这个我还没想明白为什么。在m==0时我的输出也是正确的。
2、1只是入口,边是双向的。。。。这个好坑。
#include<stdio.h>
#include<string.h>
#define N 105
struct node
{
int son;
int next;
}edge[N*2];
int id,head[N],cost[N],value[N],dp[N][N],vis[N];
int n,m;
int Max(int x,int y)
{
if(x>y) return x;
else return y;
}
void AddEdge(int x,int y)
{
edge[id].son=y;edge[id].next=head[x];head[x]=id++;
edge[id].son=x;edge[id].next=head[y];head[y]=id++;
return ;
}
void dfs(int father)
{
int u=head[father];
vis[father]=1;
for(int i=cost[father];i<=m;i++)
dp[father][i]=value[father];
for(/*int*/ i=u;i!=-1;i=edge[i].next)
{
int son=edge[i].son;
if(vis[son]) continue;
dfs(son);
for(int k=m;k>=cost[father];k--)
{
for(int j=1;j<=k-cost[father];j++)
dp[father][k]=Max(dp[father][k],dp[father][k-j]+dp[son][j]);
}
}
return ;
}
int main()
{
while(scanf("%d%d",&n,&m),n+m!=-2)
{
if(n==-1&&m==-1) break;
id=0;
memset(head,-1,sizeof(head));
for(int i=1;i<=n;i++)
{
scanf("%d%d",&cost[i],&value[i]);
if(cost[i]%20==0) cost[i]/=20;
else cost[i]=cost[i]/20+1;
}
for(/*int*/ i=1;i<n;i++)
{
int x,y;
scanf("%d%d",&x,&y);
AddEdge(x,y);
}
/*
if(m==0)
{
printf("0\n");
continue;
}
*/
memset(vis,0,sizeof(vis));
memset(dp,0,sizeof(dp));
dfs(1);
printf("%d\n",dp[1][m]);
}
return 0;
}