HDU-4003 Find Metal Mineral (tree-shaped dp+ Group backpack)

Source: Internet
Author: User

The main idea: using M-robot to traverse a root tree with n nodes, the Benquan represents a robot's price at the cost of this edge.

Topic Analysis: The definition State DP (ROOT,K) represents the total cost of having k robots in a subtree with root as root when the final traversal is complete. The state transition equation is:

DP (ROOT,K) =min (DP (ROOT,K), DP (SON,J) +DP (root,k-j) +j*w (Root,son)) j>0

Note that when J is 0, all bots return to root after traversing the subtree of son. It can be proved that if all the robots that traverse son go back to root, the more robots that traverse son, the higher the cost.

The code is as follows:

# include<iostream># include<cstdio># include<cstring># include<algorithm>using namespace std;# define LL long longconst int n=10005;struct edge{int to,w,nxt;}; Edge e[n<<1];int head[n];int cnt,n,s,m;int dp[n][12];void Add (int u,int v,int W) {e[cnt].to=v;e[cnt].w=w;e[cnt]. nxt=head[u];head[u]=cnt++;} void init () {int A,b,w;cnt=0;memset (dp,0,sizeof (DP)), memset (Head,-1,sizeof (head)), for (int i=1;i<n;++i) {scanf ("% d%d%d ", &a,&b,&w); add (a,b,w); add (b,a,w);}} void Dfs (int u,int fa) {for (int i=head[u];i!=-1;i=e[i].nxt) {int v=e[i].to;if (V==FA) Continue;dfs (V,u), for (int j=m;j >=0;--J) {dp[u][j]+=dp[v][0]+2*e[i].w;for (int k=1;k<=j;++k) dp[u][j]=min (dp[u][j],dp[u][j-k]+dp[v][k]+k*e[i ].W);}}} void Solve () {DFS (s,-1);p rintf ("%d\n", Dp[s][m]);} int main () {while (~scanf ("%d%d%d", &n,&s,&m)) {init (); Solve ();} return 0;}

  

HDU-4003 Find Metal Mineral (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.