标签:
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1011
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 16276 Accepted Submission(s): 4335
#include<iostream> #include<cstdio> #include<algorithm> #include<vector> #include<cstring> using namespace std; int n,m; int cost[105],value[105]; int dp[105][105]; int father[105]; vector<int> v[105]; bool vis[105]; void tree_dp(int root) { int fcost=cost[root]; vis[root]=1; for(int i=fcost;i<=m;i++) dp[root][i]=value[root]; for(int i=0;i<v[root].size();i++) { int son=v[root][i]; if(vis[son]) continue; tree_dp(son); for(int j=m;j>=fcost;j--) { for(int k=1;k<=j-fcost;k++) dp[root][j]=max(dp[root][j],dp[root][j-k]+dp[son][k]); } } } int main() { while(scanf("%d%d",&n,&m)!=EOF&&(m!=-1&&n!=-1)) { memset(vis,0,sizeof(vis)); //memset(father,0,sizeof(father)); memset(dp,0,sizeof(dp)); for(int i=0;i<=n;i++) v[i].clear(); for(int i=1; i<=n; i++) { int co; scanf("%d%d",&co,&value[i]); cost[i]=co/20; if(co%20>0) cost[i]++; //cout<<cost[i]<<‘*‘<<endl; } for(int i=1;i<n;i++) { int a,b; scanf("%d%d",&a,&b); v[a].push_back(b); v[b].push_back(a); //father[b]++; } if(m==0) {printf("0\n");continue;} tree_dp(1); printf("%d\n",dp[1][m]); } return 0; }
HDU_1011_Starship Troopers_树型dp
标签:
原文地址:http://www.cnblogs.com/jasonlixuetao/p/5443910.html