首页 > 代码库 > HDU4003Find Metal Mineral[树形DP 分组背包]

HDU4003Find Metal Mineral[树形DP 分组背包]

Find Metal Mineral

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


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,每个子节点是一个分组,不同的组是放的机器人不同
f[i][j]表示子树i放j个机器人(不用上来)的最小代价
f[i][0]表示放一个又上来   可以发现放多个有上来的不可能更优
因为每组至少选一个,所以先把f[v][0]放上
////  main.cpp//  hdu4003////  Created by Candy on 9/23/16.//  Copyright © 2016 Candy. All rights reserved.//#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>using namespace std;const int N=1e4+5,M=12;int read(){    char c=getchar();int x=0,f=1;    while(c<0||c>9){if(c==-)f=-1; c=getchar();}    while(c>=0&&c<=9){x=x*10+c-0; c=getchar();}    return x*f;}struct edge{    int v,w,ne;}e[N<<1];int h[N],cnt=0;void ins(int u,int v,int w){    cnt++;    e[cnt].v=v;e[cnt].w=w;e[cnt].ne=h[u];h[u]=cnt;    cnt++;    e[cnt].v=u;e[cnt].w=w;e[cnt].ne=h[v];h[v]=cnt;}int  n,s,m,u,v,w;int f[N][M];void dp(int u,int fa){    for(int i=h[u];i;i=e[i].ne){        int v=e[i].v,w=e[i].w;        if(v==fa) continue;        dp(v,u);        for(int j=m;j>=0;j--){  //ti ji            f[u][j]+=f[v][0]+w*2;//must choose            for(int k=1;k<=j;k++)//group                f[u][j]=min(f[u][j],f[u][j-k]+f[v][k]+w*k);        }    }}int main(int argc, const char * argv[]) {    while(cin>>n>>s>>m){        cnt=0;        memset(h,0,sizeof(h));        for(int i=1;i<=n-1;i++){            u=read();v=read();w=read();            ins(u,v,w);        }        memset(f,0,sizeof(f));        dp(s,0);        printf("%d\n",f[s][m]);    }    return 0;}

 

HDU4003Find Metal Mineral[树形DP 分组背包]