hdu4003 Find Metal Mineral

来源:互联网 发布:个性淘宝店名 编辑:程序博客网 时间:2024/05/17 01:18
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 2996    Accepted Submission(s): 1377


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 11 2 11 3 13 1 21 2 11 3 1
 

Sample Output
32
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
The 36th ACM/ICPC Asia Regional Dalian Site —— Online Contest
 
这题可以用树形背包做,和前面几题差不多,只是这题更复杂些,要保证所有点都走一遍,可以用dp[i][j]表示节点i派j个人走完i节点子树范围内的所有节点的最小代价。
其中j=0时,dp[i][j]表示用一个机器人去走完所有子树,最后又回到i这个节点所要的费用,j>0时表示用j个机器人走完所有子树要用的费用,然后分组背包就行了。


#include<iostream>#include<stdio.h>#include<stdlib.h>#include<string.h>#include<math.h>#include<vector>#include<map>#include<set>#include<queue>#include<stack>#include<string>#include<algorithm>using namespace std;typedef long long ll;#define inf 0x7fffffff#define maxn 10050int first[maxn];struct node{    int to,next,w;}e[2*maxn];int dp[maxn][15],vis[maxn];int n,k;void dfs(int u){    int i,j,v,l;    vis[u]=1;    for(i=first[u];i!=-1;i=e[i].next){        v=e[i].to;        if(vis[v])continue;        dfs(v);        for(j=k;j>=0;j--){            dp[u][j]+=dp[v][0]+2*e[i].w; //这里从父亲节点到子节点要加上两倍的路代价            for(l=1;l<=j;l++){           //这里用分组背包看看是不是有更好的替代0的方案                dp[u][j]=min(dp[u][j],dp[u][j-l]+dp[v][l]+l*e[i].w  );            }        }    }}int main(){    int m,i,j,s,f,tot,c,d;    while(scanf("%d%d%d",&n,&s,&k)!=EOF)    {        tot=0;        memset(first,-1,sizeof(first));        for(i=1;i<=n-1;i++){            scanf("%d%d%d",&c,&d,&f);            tot++;            e[tot].to=d;e[tot].next=first[c];e[tot].w=f;            first[c]=tot;            tot++;            e[tot].to=c;e[tot].next=first[d];e[tot].w=f;            first[d]=tot;        }        memset(dp,0,sizeof(dp));        memset(vis,0,sizeof(vis));        dfs(s);        printf("%d\n",dp[s][k]);    }    return 0;}





0 0