hdu 4003 Find Metal Mineral 树形dp+分组背包

来源:互联网 发布:数据预测公式 编辑:程序博客网 时间:2024/04/25 18:34

Find Metal Mineral

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


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
 

Recommend
lcy   |   We have carefully selected several similar problems for you:  4001 4007 4006 4009 4008 
 

题意:要用k个机器人遍历一颗树,初始点为s,求最小花费。

思路:dp[i][j]表示以i为根节点时用j个机器人遍历的最小花费。

这里需要考虑的就是机器人走下去后是否回来,仔细想想这种情况只会发生于只有一个机器人的时候,所以用dp[i][0]存一下派一个机器人遍历所有子节点的花费。

然后每次dp的时候先加上派一个机器人去再回来的,这个花费一定最大,这样就就能保证一定遍历了这个点。

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cstdlib>#include <vector>#include <queue>#include <map>using namespace std;const int N=10020;const int inf=0x3f3f3f3f;struct node{    int v,next,val;}edge[2*N];int p=1;int n,k,s,u,v,val,t;int dp[N][15],head[N];int pow[N][55],prc[N][55];void addedge(int i,int j,int val){    edge[p].v=j;    edge[p].next=head[i];    edge[p].val=val;    head[i]=p++;    edge[p].v=i;    edge[p].next=head[j];    edge[p].val=val;    head[j]=p++;}void init(){    p=1;    memset(dp,0,sizeof(dp));    memset(head,-1,sizeof(head));    for(int i=1;i<n;i++){        scanf("%d%d%d",&u,&v,&val);        addedge(u,v,val);    }}void dfs(int u,int pre){    for(int i=head[u];i!=-1;i=edge[i].next){        int vt=edge[i].v;        if(vt==pre)continue;        dfs(vt,u);        for(int j=k;j>=0;j--){            dp[u][j]+=dp[vt][0]+2*edge[i].val;            for(int l=1;l<=j;l++){                dp[u][j]=min(dp[u][j],dp[u][j-l]+dp[vt][l]+l*edge[i].val);            }        }    }}int main(){    while(scanf("%d%d%d",&n,&s,&k)!=EOF){        init();        dfs(s,0);        printf("%d\n",dp[s][k]);    }    return 0;}




0 0
原创粉丝点击