HDU-4003 Find Metal Mineral (树上背包)

Find Metal Mineral

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 3786    Accepted Submission(s): 1772


Problem 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;


#include <stdio.h>
#include <vector>
#include <string.h>
using namespace std;
vector<vector<int> > g(10001);
int dp[10001][11], dep[10001], c[10001], n, k;
struct point{
	int from, to, val;
}a[10001];
void dfs(int x, int fa){
	int cur;
	for(int i = 0; i < g[x].size(); ++i){
		cur = g[x][i];
		if(cur == fa) continue;
		dfs(cur, x);
		for(int j = k; j >= 0; --j){
			dp[x][j] += dp[cur][0] + 2 * c[cur];
			for(int s = 1; s <= j; ++s){
				dp[x][j] = min(dp[x][j], dp[x][j - s] + dp[cur][s] + s * c[cur]);
			}
		}
	}
}
void dfs1(int x, int fa, int d){
	dep[x] = d;
	int cur;
	for(int i = 0; i < g[x].size(); ++i){
		cur = g[x][i];
		if(cur == fa) continue;
		dfs1(cur, x, d + 1);
	}
}
int main(){
	int rt, u, v, w;
	while(scanf("%d %d %d", &n, &rt, &k) != EOF){
		for(int i = 1; i <= n; ++i){
			g[i].clear();
		}
		for(int i = 1; i < n; ++i){
			scanf("%d %d %d", &u, &v, &w);
			g[u].push_back(v);
			g[v].push_back(u);
			a[i].from = u;
			a[i].to = v;
			a[i].val = w;
		}
		dfs1(rt, 0, 1);
		for(int i = 1; i < n; ++i){
			if(dep[a[i].from] > dep[a[i].to]){
				swap(a[i].from, a[i].to);
			}
			c[a[i].to] = a[i].val;
		}
		memset(dp, 0, sizeof(dp));
		dfs(rt, 0);
		printf("%d\n", dp[rt][k]);	
	}
}

/*
题意:
1棵树,10000个节点,机器人路过每条边都有一个代价,现在在树上给出一个起始位置,在该位置放一些机器人,
希望机器人访问所有的节点且花费最小的代价,问最小代价是多少。

思路:
最多10个机器人,可以用树上背包来做。dp[i][j]表示在i节点,此时我有j个机器人可以用,最少需要多少代价访问
完所有以i节点为根节点的所有子节点。注意这里我们从起始位置出发,对于每个节点,不考虑父亲节点的情况,只
考虑以其为根节点的情况。这样对于dp[i][j],我们对i节点的所有儿子跑背包,往下分配机器人,分配x个就会造成
x*当前边费用的代价,特别的,这里j为0时表示我们不会为该节点分配机器人,即用1个机器人跑完所有子节点后返回
i节点(这里这种情况很容易证明用1个机器人是花费最小的)。然后dfs对树上的点依次背包即可。
*/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值