Hdu 4003 Tree-shaped dp+ Group backpack

Source: Internet
Author: User

Still a status group

---ask K robots to start at the same point and traverse the minimum cost required for all points

Sample INPUT3 1 11 2 11 3 13 1 21 2 11 3 1

Sample Output32
1 /*2 HDU 40033 tree-shaped dp+ can only select one item of the group backpack4 5 Dp[pos][num] Indicates that the minimum value of NUM robots is used under a sub-tree with POS as the root node6 In particular, when num==0, Dp[pos][0] said to use a robot to go through all the subtrees, and finally back to the POS node7 State transfer: Dp[pos][num]=min∑{dp[pos_j][num_j]+w_j},pos_j is all the sons of Pos,8 9 Ten in order to let the group backpack only choose one, first let dp[u][0] such as backpack One  A The "group backpack" pseudo-code using a one-dimensional array is as follows: - for all Groups I -  the For V=v. 0 -  - for all k belongs to Group I -  + F[v]=max{f[v],f[v-c[k]]+w[k]} -  +  A */ at  -#include <stdio.h> -#include <algorithm> -#include <string.h> -#include <iostream> - using namespacestd; in  - Const intmaxn=10010; to  + structNode - { the     intto ; *     intNext; $     intCost//the cost of the routePanax Notoginseng}edge[maxn*2];//Tree without Direction - intHEAD[MAXN]; the intTol; +  A intdp[maxn][ One];//Dp[i][j] Represents a tree with I as the root of the use of J-man. Dp[i][0] means using a person and returning to the top point . the  + voidInit () - { $memset (head,-1,sizeof(head)); $Tol=0; -Memset (DP,0,sizeof(DP)); - } the  - voidAddintAintBintval)Wuyi { theedge[tol].to=b; -edge[tol].cost=Val; Wuedge[tol].next=Head[a]; -head[a]=tol++; Aboutedge[tol].to=A; $edge[tol].cost=Val; -edge[tol].next=Head[b]; -head[b]=tol++; - } A  + intn,k; the  - voidDfsintUintpre) $ { the      for(inti=head[u];i!=-1; i=edge[i].next) the     { the         intv=edge[i].to; the         if(V==pre)Continue; - DFS (v,u); in          for(intk=k;k>=0; k--) the         { thedp[u][k]+=dp[v][0]+2*edge[i].cost;//Put dp[u][0] in your backpack and make sure you pick at least one About          //Group Backpack the              for(intj=1; j<=k;j++) theDp[u][k]=min (dp[u][k],dp[u][k-j]+dp[v][j]+j*edge[i].cost); the         } +     } - } the intMain ()Bayi { the     intS; the     intA,b,val; -      while(SCANF ("%d%d%d", &n,&s,&k)! =EOF) -     { the init (); the          for(intI=1; i<n;i++) the         { thescanf"%d%d%d",&a,&b,&val); - Add (a,b,val); the         } theDFS (s,-1); theprintf"%d\n", Dp[s][k]);94     } the     return 0; the}

Hdu 4003 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.