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

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

 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<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#define inf 0x7fffffff
using namespace std;
const int maxn = 10000 + 10;

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

void dfs(int u, int f)
{
	father[u] = f;                     //标明父节点,着陆点的父节点为-1
	int num = G[u].size();
	for (int i = 0; i<num; i++)
	{
		int v = G[u][i].first; 
		int cost = G[u][i].second;
		if (v == f) continue;         //只搜索子节点,父节点跳过
		dfs(v, u);
		for (int j = k; j >= 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<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));
		}
		dfs(s, -1);          
		printf("%d\n", dp[s][k]);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

这波lucio来全学了

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值