HDU-1011 Starship Troopers (tree-shaped dp+ Group backpack)

Source: Internet
Author: User

Title: Give a Tree a tree with a root, and give the capacity. Maximum weight at no more than the capacity. The prerequisite is that the parent node is selected to select child nodes.

Problem Analysis: A group backpack on a tree.

PS: The case of M is 0 o'clock in the special sentence.

The code is as follows:

# include<iostream># include<cstdio># include<vector># include<cstring># include< algorithm>using namespace Std;const int n=105;const int inf=1000000000;int n,m;int bug[n];int w[n];int Dp[N][N]; Vector<int>e[n];void init () {memset (dp,0,sizeof (DP)); for (int i=1;i<=n;++i) {scanf ("%d%d", bug+i,w+i); E[i]. Clear ();} int a,b;for (int i=1;i<n;++i) {scanf ("%d%d", &a,&b); E[a].push_back (b); E[b].push_back (a);}} int gettroopers (int x) {return (x+19)/20;} int dfs (int u,int fa) {int x=gettroopers (Bug[u]), for (int i=x;i<=m;++i) dp[u][i]=w[u];for (int i=0;i<e[u].size (); + +i) {int v=e[u][i];if (V==FA) Continue;dfs (V,u), for (int. j=m;j>=x;--j) {for (int k=1;k<=j-x;++k) {Dp[u][j]=max (dp[ U][j],dp[v][k]+dp[u][j-k]);}}} void Solve () {if (m==0) printf ("0\n"), Else{dfs (1,-1);p rintf ("%d\n", Dp[1][m]);}} int main () {while (~scanf ("%d%d", &n,&m) && (n!=-1| | M!=-1)) {init (); Solve ();} return 0;}

  

HDU-1011 Starship Troopers (tree-shaped dp+ Group backpack)

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.