标签:dp
Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6000 Accepted Submission(s): 3548
3 2 0 1 0 2 0 3 7 4 2 2 0 1 0 4 2 1 7 1 7 6 2 2 0 0
5 13
/* 题意:中文题目 思路:树形dp+背包,把没有限制的点当成父亲节点,走完了这个点再走别的点 dp[v][i] 走到v节点走了i个点的价值 转移方程 dp[v][i]=max(dp[v][i],dp[v][i-j]+dp[to][j]); */ #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #include<queue> #include<stack> #include<vector> #include<set> #include<map> #define L(x) (x<<1) #define R(x) (x<<1|1) #define MID(x,y) ((x+y)>>1) #define eps 1e-8 typedef __int64 ll; using namespace std; #define INF 0x3f3f3f3f #define N 205 int va[N],n,p; vector<int>g[N]; int dp[N][N]; int vis[N]; void dfs(int x) { int i,j; vis[x]=1; dp[x][1]=va[x]; for(i=0;i<g[x].size();i++) { int to=g[x][i]; if(!vis[to]) dfs(to); for(int v=p;v>0;v--) for(int j=1;j<v;j++) dp[x][v]=max(dp[x][v],dp[x][v-j]+dp[to][j]); } } int main() { int i,j; while(scanf("%d%d",&n,&p),n+p) { p++; for(i=0;i<=n;i++) g[i].clear(); int x; for(i=1;i<=n;i++) { scanf("%d%d",&x,&va[i]); g[x].push_back(i); } memset(vis,0,sizeof(vis)); memset(dp,0,sizeof(dp)); dfs(0); printf("%d\n",dp[0][p]); } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
HDU 1561 The more, The Better(树形dp+背包)
标签:dp
原文地址:http://blog.csdn.net/u014737310/article/details/46798775