Find Metal Mineral

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;

思路:
p[i][j]表示对于以i结点为根结点的子树,放j个机器人所需要的权值和。
当j=0时表示放了一个机器人下去,遍历完结点后又回到i结点了。状态转移方程类似背包
如果最终的状态中以i为根结点的树中有j(j>0)个机器人,那么不可能有别的机器人r到了这棵树后又跑到别的树中去 因为那样的话,一定会比j中的某一个到达i后跑与r相同的路径再回到i,再接着跑它的路径要差(多了一条i回去的边) 这样的话,如果最后以i为根结点的树中没有机器人,那么只可能是派一个机器人下去遍历完后再回来

#include<stdio.h>
#include<string.h>
#define Max 10005
int node[Max];
int dp[10010][13];
struct edge
{
	int next,v,val;
}edge[20010];
int cnt_edge;
int n,s,k;
void add_edge(int x,int y,int z)
{
	edge[cnt_edge].next=node[x];
	edge[cnt_edge].v=y;
	edge[cnt_edge].val=z;
	node[x]=cnt_edge++;
}
void clear()
{
	cnt_edge=0;
	memset(dp,0,sizeof(dp));
	memset(node,-1,sizeof(node));
}
inline int min(int a,int b)
{
	return a<b?a:b;
}
void dfs(int u,int p)
{
    int i;
    for(i=node[u];i!=-1;i=edge[i].next)
    {
        int v=edge[i].v;
        if(v==p)
            continue;
        dfs(v,u);
        int j,l;
        for(j=k;j>=0;j--)
        {
            dp[u][j]+=dp[v][0]+2*edge[i].val;
            for(l=1;l<=j;l++)
            {
                dp[u][j]=min(dp[u][j],dp[u][j-l]+dp[v][l]+l*edge[i].val);
            }
        }
    }
}
int main()
{
	//freopen("b.txt","r",stdin);
	int i,j,x,y,z;
	while(scanf("%d %d %d",&n,&s,&k)!=EOF)
	{
		clear();
		for(i=1;i<n;i++)
		{
			scanf("%d %d %d",&x,&y,&z);
			add_edge(x,y,z);
			add_edge(y,x,z);
		}
		dfs(s,-1);
		printf("%d\n",dp[s][k]);
	}
	return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值