HDU - 4003 Find Metal Mineral(动态规划)

Find Metal Mineral
Time Limit: 1000MS   Memory Limit: 65768KB   64bit IO Format: %I64d & %I64u

Submit Status

Description

Humans have discovered a kind of new metal mineral on Mars which are distributed in point�like with paths connecting each of them which formed a tree. Now Humans launches k robots on Mars to collect them, and due to the unknown reasons, the landing site S of all robots is identified in advanced, in other word, all robot should start their job at point S. Each robot can return to Earth anywhere, and of course they cannot go back to Mars. We have research the information of all paths on Mars, including its two endpoints x, y and energy cost w. To reduce the total energy cost, we should make a optimal plan which cost minimal energy cost.
 

Input

There are multiple cases in the input.  
In each case:  
The first line specifies three integers N, S, K specifying the numbers of metal mineral, landing site and the number of robots.  
The next n�1 lines will give three integers x, y, w in each line specifying there is a path connected point x and y which should cost w.  
1<=N<=10000, 1<=S<=N, 1<=k<=10, 1<=x, y<=N, 1<=w<=10000.
 

Output

For each cases output one line with the minimal energy cost.
 

Sample Input

 
     
3 1 1 1 2 1 1 3 1 3 1 2 1 2 1 1 3 1
 

Sample Output

 
     
3 2

Hint

In the first case: 1->2->1->3 the cost is 3; In the second case: 1->2; 1->3 the cost is 2;         

/*
题意描述:火星上有很多矿山(n<=10000),人们发射k(k<=10)个机器人着陆在火星上的S矿山上,目的就是采取每座矿山上的资源。
一些矿山之间相互连接着,从一个矿山到另一个与其相连的矿山要消耗能量,问其最少的消耗能量是多少。
算法分析:树形DP,dp[u][i]定义为以u为根的子树中分配了i个机器人消耗的最少能量,特别的是,dp[u][0]表示为以u为树根的子树中
分配了一个机器人并且机器人要在走完这颗子树后要回到u点(就相当于没有给子树分配)的最少消耗能量。
那么我们可以列出式子:dp[u][i]=min(dp[u][i],dp[u][i-j]+dp[v][j]+j*cost)(v为u的相邻节点,w为这条路的能量消耗)。
*/
#include
#include
#include
#include
#include
#include
#include
#include
#define inf 0x7fffffff
using namespace std;
const int maxn = 10000 + 10;

int n, s, k;
int father[maxn], dp[maxn][12];
vector > G[maxn];       //first存邻接点,second存消耗

void dfs(int u, int f)
{
	father[u] = f;                     //标明父节点,着陆点的父节点为-1
	int num = G[u].size();
	for (int i = 0; i= 0; j--)
		{
			dp[u][j] += dp[v][0] + 2 * cost;   //初始化为只派一个机器人走完子树并回到u点的消耗,j=0时下面的循环不会执行,得到的dp[u][0]为走完子树并回到u点的消耗
			for (int q = 1; q <= j; q++)       //对不同的机器人派出个数,dp出u点到v点的最小消耗
				dp[u][j] = min(dp[u][j], dp[u][j - q] + dp[v][q] + q*cost); 
		}
	}
}

int main()
{
	while (scanf("%d%d%d", &n, &s, &k) != EOF)
	{
		memset(father, -1, sizeof(father));
		memset(dp, 0, sizeof(dp));
		for (int i = 1; i <= n; i++) G[i].clear();
		int a, b, c;
		for (int i = 0; i


你可能感兴趣的:(HDU - 4003 Find Metal Mineral(动态规划))