Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=4003
humans has discovered a kind of new metal mineral on Mars which is distributed in point‐like with paths connecting E Ach of them which formed a tree. Now humans launches K robots in Mars to collect them, and due to the unknown reasons, the landing site S of all robots are Identified in advanced, in other word, all robot should start their job at point S. Each robot can return to Earth anywhere, and the course they cannot go back to Mars. We have the information of any paths on Mars, including it, endpoints X, Y and energy cost W. To reduce, we should make a optimal plan which cost minimal. Test Instructions Description: There are many mines on Mars (n<=10000), people launch K (K<=10) robot landed on Mars s mine, the purpose is to take each mine resources. Some mines connect with each other, from one mine to another mine that is connected to it consumes energy, asking how much energy it consumes at least. algorithm Analysis: Tree dp,dp[u][i] is defined as the root of the U-tree in the allocation of I robot consumes the least energy, in particular, dp[u][0] Represented as a tree with the root of U assigned a robot and the robot wants to return to the U point (equivalent to no sub-tree allocation) after walking through the subtree to the least energy consumed. then we can list the formula: Dp[u][i]=min (dp[u][i],dp[u][i-j]+dp[v][j]+j*cost) (V is the U's adjacent node, W is the energy consumption of this road).
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cstdlib>5#include <cmath>6#include <algorithm>7#include <vector>8#include <map>9 #defineINF 0x7fffffffTen using namespacestd; One Const intmaxn=10000+Ten; A - intn,s,k; - intfather[maxn],dp[maxn][ A]; thevector<pair<int,int> >G[MAXN]; - - voidDfsintUintf) - { +father[u]=F; - intnum=g[u].size (); + for(intI=0; i<num; i++.) A { at intv=G[u][i].first; - intcost=G[u][i].second; - if(v==f)Continue; - DFS (v,u); - for(intj=k; j>=0; j--) - { inDP[U][J] + = dp[v][0]+2*Cost ; - for(intq=1; q<=j; q++.) toDp[u][j]=min (dp[u][j],dp[u][j-q]+dp[v][q]+q*Cost ); + } - } the } * $ intMain ()Panax Notoginseng { - while(SCANF ("%d%d%d", &n,&s,&k)! =EOF) the { +memset (father,-1,sizeof(father)); AMemset (DP,0,sizeof(DP)); the for(intI=1; i<=n; i++.) g[i].clear (); + inta,b,c; - for(intI=0; i<n-1; i++) $ { $scanf"%d%d%d",&a,&b,&c); - G[a].push_back (Make_pair (b,c)); - G[b].push_back (Make_pair (a,c)); the } -DFS (s,-1);Wuyiprintf"%d\n", Dp[s][k]); the } - return 0; Wu}
HDU 4003 Find Metal Mineral tree DP