现在的位置: 首页 > 综合 > 正文

HDOJ题目4003 Find Metal Mineral(树状dp)

2015年12月17日 ⁄ 综合 ⁄ 共 2108字 ⁄ 字号 评论关闭

Find Metal Mineral

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


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
 


Recommend
lcy   |   We have carefully selected several similar problems for you:  4001 4007 4006 4009 4008 
 
ac代码
#include<stdio.h>
#include<string.h>
#include<map>
#include<string>
#include<vector>
#define min(a,b) (a<b?a:b)
using namespace std;
vector<pair<int,int> >list[10010];
int dp[10010][20],m;
int dfs(int u,int fa)
{
	int i,j,k,v;
	for(i=0;i<list[u].size();i++)
	{
		v=list[u][i].first;
		if(v==fa)
			continue;
		dfs(v,u);
		for(k=m;k>=0;k--)
		{
			dp[u][k]+=dp[v][0]+2*list[u][i].second;//dp[v][0]是遍历v子树回来,遍历完后又回来了,其实就等于子树中边权值和的两倍 
			for(j=1;j<=k;j++)
			{
				dp[u][k]=min(dp[u][k],dp[u][k-j]+dp[v][j]+j*list[u][i].second);//派j个机器人给v子树就会经过u-v之间的边j次
			}
		}
	}
}
int main()
{
	int n,s;
	while(scanf("%d%d%d",&n,&s,&m)!=EOF)
	{
		int i,a,b,w;
		memset(dp,0,sizeof(dp));
		for(i=0;i<=n;i++)
			list[i].clear();
		for(i=0;i<n-1;i++)
		{
			scanf("%d%d%d",&a,&b,&w);
			list[a].push_back(make_pair(b,w));
			list[b].push_back(make_pair(a,w));
		}
		dfs(s,-1);
		printf("%d\n",dp[s][m]);
	}
}

抱歉!评论已关闭.