Thematic training
1#include <stdio.h>2#include <iostream>3#include <string.h>4#include <algorithm>5 using namespacestd;6 Const intmaxn=1010;7 Const intinf=0x3fffffff;8 structNode9 {Ten intto ; One intNext; A}edge[maxn*2]; - intTol; - intHEAD[MAXN]; the intdp[maxn][ -]; - intprice[maxn][ -]; - intpower[maxn][ -]; - + voidInit () - { +memset (head,-1,sizeof(head)); ATol=0; at } - voidAddintAintb) - { -edge[tol].to=b; -edge[tol].next=Head[a]; -head[a]=tol++; inedge[tol].to=A; -edge[tol].next=Head[b]; tohead[b]=tol++; + } - intn,m; the intTMP[MAXN]; * voidDfsintUintpre) $ {Panax Notoginseng if(head[u]==-1|| (edge[head[u]].to==pre&&edge[head[u]].next==-1))//leaf knot Point -{//the condition of the leaf knot is not wrong!!!! the for(intI=0; i<=m;i++) dp[u][i]=0; + A for(intI=0; i<=m;i++) tmp[i]=Dp[u][i]; the //because there are points with a price of 0. So the reverse DP is not guaranteed to take only one item. + for(inti=m;i>=0; i--) - { $ for(intj=1; j<=price[u][0];j++) $ if(price[u][j]<=i) -Dp[u][i]=max (dp[u][i],tmp[i-price[u][j]]+power[u][j]); - theTmp[i]=dp[u][i];//The tmp array is the previous state of the recorded DP - }Wuyi return; the } - for(intI=0; i<=m;i++) dp[u][i]=INF; Wu for(inti=head[u];i!=-1; i=edge[i].next) - { About intv=edge[i].to; $ if(V==pre)Continue; - DFS (v,u); - for(intj=m;j>=0; j--) - { A intt=0; + for(intk=0; k<=j;k++)//here K must start from 0. theT=max (T,min (dp[u][j-k],dp[v][k])); -dp[u][j]=T; $ } the } the the the for(intI=0; i<=m;i++) tmp[i]=Dp[u][i]; - for(inti=m;i>=0; i--) in { the for(intj=1; j<=price[u][0];j++) the if(price[u][j]<=i) AboutDp[u][i]=max (dp[u][i],tmp[i-price[u][j]]+power[u][j]); the //The same as the above, the backpack has a TMP array. thetmp[i]=Dp[u][i]; the } + } - intMain () the {Bayi intu,v; the intT; thescanf"%d",&T); - while(t--) - { the init (); thescanf"%d",&n); the for(intI=1; i<n;i++) the { -scanf"%d%d",&u,&v); the Add (u,v); the } thescanf"%d",&m);94 for(intI=1; i<=n;i++) the { thescanf"%d", &price[i][0]); thepower[i][0]=price[i][0];98 for(intj=1; j<=price[i][0];j++) About { -scanf"%d%d",&price[i][j],&power[i][j]);101 }102 }103Dfs1,0);104printf"%d\n", dp[1][m]); the }106 return 0;107}
HDU 4044 2011 Beijing Online Race e-Tree DP * * * *