Starship Troopers (HDU 1011 tree DP)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.