Test instructions: Given a tree map, n nodes, there is edge right, to send k<11 a robot from the node S, traverse all points, whenever 1 robots through 1 sides will spend the side of the Benquan, Edge is repeatable walk. Ask the minimum cost of traversing all points?
Ideas:
Very classical, first need to know a little, to go down reasoning. "If you send a C robot to your child at T Point, then only 1 robots can walk back to T, otherwise the cost is always not worth it."
A little proof:
(1) Suppose to send 1 robots to u, stroll back to you Father T, spend v= subtree u Benquan and * * + E (t,u) * *. If the robot does not, it must cost less than V.
(2) Assuming that 2 robots are sent to u, and that 2 robots will return, it is obviously much more expensive than (1). If only 1 robots return, the cost is still higher than "send only 1 robots toward U". (You can try to draw a tree has 3 layers of trees, the first two floors only 1 nodes, the third layer of nodes can be arbitrary, to simulate a bit to know the results)
The conclusion is that either just send a C robot to the child U, and 1 don't Come back (note: 0<c<=k), or send 1 robots out, and walk through the subtree u go back to T.
This is a good solution. Enumerate the number of children (groups) under Point T, and then enumerate the number of robots (volumes) that may be obtained by the point T, and then enumerate the number of robots (items) assigned to the child.
There is also the problem of initializing the DP value, because each child must be traversed, and the regular group backpack is optional (up to 1 pieces) or not selected items, you can first "Send 1 robots and reclaim 1 robot's DP value" into the backpack (guaranteed that this capacity must have traversed the subtree u), Then if there is a better state to update this capacity again.
Using dp[t][0] means "There are 1 robots arriving at T to traverse the subtree and then go back to T" cost. Dp[t][i] represents the minimum cost of having an I robot reaching T and traversing the complete subtrees tree T, without requiring a return to T.
1 //#include <bits/stdc++.h>2#include <cstdio>3#include <cstring>4#include <iostream>5 #definePII pair<int,int>6 #defineMax (x, y) (x>y?x:y)7 #defineMin (x, y) (x<y?x:y)8 #defineINF 0x3f3f3f3f9 #defineLL Long LongTen using namespacestd; One Const intn=10050; A - structnode - { the int from, To,len,next; - node () {}; -Nodeint from,intTo,intLenintNext): from( from), to, Len (Len), next (next) {}; -}edge[n*2]; + - inthead[n],dp[n][ A], N, K, edge_cnt; + voidAdd_node (int from,intTo,intlen) A { atEdge[edge_cnt]=node ( from, to,len,head[ from]); -head[ from]=edge_cnt++; - } - - voidDFS (intTintFar ) - { in node E; - for(intI=HEAD[T]; i!=-1; I=e.next)//equivalent to enumeration ' group ' to { +E=Edge[i]; - if(e.to^Far ) the { * DFS (e.to, t); $ for(intJ=k; j>=0; j--)//Enumerate Backpack capacityPanax Notoginseng { -dp[t][j]+=dp[e.to][0]+e.len*2; the for(intu=1; u<=j; u++)//enumerate buy Dp[e.to][u] This product +Dp[t][j]=min (Dp[t][j], dp[t][j-u]+dp[e.to][u]+u*E.len); A } the } + } - } $ $ - - intMain () the { -Freopen ("Input.txt","R", stdin);Wuyi inta,b,c,s; the while(~SCANF ("%d%d%d",&n,&s,&K)) - { WuMemset (Head,-1,sizeof(head)); -Memset (DP,0,sizeof(DP)); AboutEdge_cnt=0; $ for(intI=1; i<n; i++) - { -scanf"%d%d%d",&a,&b,&c); - Add_node (a,b,c); A Add_node (b,a,c); + } theDFS (S,-1); -printf"%d\n", Dp[s][k]); $ } the return 0; the}
AC Code
HDU 4003 Find Metal Mineral (tree dp, tree Group backpack, classic)