Test instructions
Given n fixed points and M soldiers, N fixed point will eventually constitute a tree, each point has a certain x bugs and Y value, every 20 bugs need to consume a soldier, less than 20 also consumes one, and then finally harvest Y value, only the parent node is occupied after the child node can be occupied.
DP State Transfer equation:
Dp[p][j]=max (Dp[p][j],dp[p][j-k]+dp[t][k]);
Look at the Wang Dashin code, DFS write, first from the leaf node to go up to the dynamic planning, I saw the DP equation did not write it out.
1#include <iostream>2#include <cstring>3#include <cstdio>4#include <vector>5#include <algorithm>6 using namespacestd;7 #defineMax 1028 structnode9 {Ten intBugs,val; One }cav[max]; Avector<int>Node[max]; - intn,m; - intVis[max],dp[max][max]; the intb; - voidDfsintp) - { - inti,j,k; +vis[p]=1; - intTemp= (cav[p].bugs+ +)/ -; + for(i=temp;i<=m;i++) Adp[p][i]=Cav[p].val; at for(i=0; I<node[p].size (); i++) - { - intt=Node[p][i]; - if(Vis[t]) - Continue; - DFS (t); in for(j=m;j>=temp;j--)//to complete the dynamic planning of the point when backtracking - for(k=1; k<=j-temp;k++) toDp[p][j]=max (Dp[p][j],dp[p][j-k]+dp[t][k]);//ensure that the parent node is able to seize + } - return; the } * intMain () $ {Panax Notoginseng inti,j; -Freopen ("In.txt","R", stdin); the while(SCANF ("%d%d", &n,&m)! =EOF) + { A if(n==-1&&m==-1) the Break; +memset (Vis,0,sizeof(Vis)); -Memset (DP,0,sizeof(DP)); $ for(i=0; i<=n;i++) node[i].clear (); $ for(i=1; i<=n;i++) -scanf"%d%d",&cav[i].bugs,&cav[i].val); - for(i=0; i<n-1; i++) the { -scanf"%d%d",&a,&b);Wuyi Node[a].push_back (b); the Node[b].push_back (a); - } Wu if(m==0) - { Aboutprintf"0\n"); $ Continue; - } -Dfs1); -printf"%d\n", dp[1][m]); A } + return 0; the}
Starship Troopers (HDU 1011 tree DP)