HDOJ 4003 Find Metal Mineral


题意:

一棵有权树,从根结点中放入 K 个机器人,求用这 K 个机器人遍历所有的结点最少的权值和。

思路:

1. dp[u][i] 表示给以 u 为根节点的子树放 i 个机器人,遍历其子树所需要的最小权值。

2. 关键在于 dp[u][0] 的理解,表示:最后停留在以 u 为根节点的子树下 0 个机器人,并且遍历了 u 子树的最小权值和。

3. 下面的步骤就变成和分组背包类似的情况了,根节点 u 给孩子 v 放多少个机器人。

4. dp[u][i] = min(dp[u][i], dp[u][j] + dp[v][i-j] + (i-j) * c); 可以理解成给 v 放了 i-j 个机器人,给 v 的其他兄弟放了 j 个,u 总共有 i 个

Find Metal Mineral

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


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;
 

Source
 



#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int maxn=11000;

struct Edge
{
	int to,next,w;
}edge[2*maxn];

int Adj[maxn],Size;

void init()
{
	memset(Adj,-1,sizeof(Adj)); Size=0;
}

void Add_Edge(int u,int v,int weight)
{
	edge[Size].to=v;
	edge[Size].next=Adj[u];
	edge[Size].w=weight;
	Adj[u]=Size++;
}

int n,S,K;
int dp[maxn][20];
bool vis[maxn];

void dfs(int u)
{
	vis[u]=true;
	for(int i=Adj[u];~i;i=edge[i].next)
	{
		int v=edge[i].to;
		int w=edge[i].w;
		if(vis[v]) continue;

		dfs(v);

		for(int j=K;j>=0;j--)
		{
			dp[u][j]+=dp[v][0]+2*w;
			for(int jj=0;jj<=j;jj++)
			{
				dp[u][j]=min(dp[u][j],dp[u][j-jj]+dp[v][jj]+jj*w);
			}
		}
	}

}

int main()
{
	while(scanf("%d%d%d",&n,&S,&K)!=EOF)
	{
		init();
		for(int i=1;i<n;i++)
		{
			int a,b,w;
			scanf("%d%d%d",&a,&b,&w);
			Add_Edge(a,b,w);
			Add_Edge(b,a,w);
		}
		memset(dp,0,sizeof(dp));
		memset(vis,0,sizeof(vis));
		dfs(S);
		printf("%d\n",dp[S][K]);
	}
 	return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值