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)