hdu 4003 Find Metal Mineral

Find Metal Mineral
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 1986 Accepted Submission(s): 900


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
The 36th ACM/ICPC Asia Regional Dalian Site —— Online Contest


Recommend
lcy

 

参考:http://www.cnblogs.com/kuangbin/archive/2012/08/29/2661928.html

 

 

 1 //296MS    1132K    1206 B    C++
 2 /*
 3 
 4 树形DP: 
 5 
 6 dp[pos][num]表示以pos为根节点的子树下,用去num个机器人,所得到的最小值
 7 特别的是当num==0的时候,dp[pos][0]表示用一个机器人去走完所有子树,最后又回到pos这个节点
 8 状态转移:dp[pos][num]=min∑{dp[pos_j][num_j]+w_j},pos_j是pos的所有儿子,
 9 
10 使用一维数组的“分组背包”伪代码如下:
11 for 所有的组i
12 
13     for v=V..0
14 
15         for 所有的k属于组i
16 
17             f[v]=max{f[v],f[v-c[k]]+w[k]}
18  
19 */ 
20 #include<stdio.h>
21 #include<string.h>
22 #define N 10005
23 struct node{
24     int to;
25     int w;
26     int next; //指向直接子节点 
27 }e[2*N]; //无向树 
28 int dp[N][15];
29 int head[N];  //head[i] 保存第点i指向第几条边
30 int edgenum; //所有边数 
31 int n,s,k;
32 int Min(int a,int b)
33 {
34     return a<b?a:b;
35 }
36 void addedge(int u,int v,int w)
37 {
38     e[edgenum].to=v;
39     e[edgenum].w=w;
40     e[edgenum].next=head[u];
41     head[u]=edgenum++;
42     
43     e[edgenum].to=u;
44     e[edgenum].w=w;
45     e[edgenum].next=head[v];
46     head[v]=edgenum++;
47 }
48 void dfs(int u,int root)
49 {
50     //printf("u==%d head[%d]==%d root=%d\n",u,u,head[u],root); 
51     for(int i=head[u];i!=-1;i=e[i].next){
52         int v=e[i].to;
53         if(v==root) continue; //当前节点为父节点不往下走 
54         dfs(v,u);
55         for(int j=k;j>=0;j--){ //01背包思想 
56             dp[u][j]+=dp[v][0]+2*e[i].w; //派一个机器人去走再回来 
57             for(int k0=1;k0<=j;k0++) //子树放下k0个机器人 
58                 dp[u][j]=Min(dp[u][j],dp[u][j-k0]+dp[v][k0]+k0*e[i].w);
59             //printf("u==%d j==%d dp[u][j]==%d\n",u,j,dp[u][j]); 
60         }
61     }
62 }
63 int main(void)
64 {
65     int u,v,w;
66     while(scanf("%d%d%d",&n,&s,&k)!=EOF)
67     {
68         memset(dp,0,sizeof(dp));
69         memset(head,-1,sizeof(head));
70         edgenum=0;
71         for(int i=1;i<n;i++){
72             scanf("%d%d%d",&u,&v,&w);
73             addedge(u,v,w);
74         }
75         dfs(s,-1);
76         printf("%d\n",dp[s][k]);
77     }
78     return 0;
79 }
80 /*
81 7 1 1
82 1 2 1
83 1 3 1
84 2 4 1
85 2 5 1
86 3 6 1
87 3 7 1
88 
89 */

 

还是觉得C++的代码好理解:

 1 //453MS    1320K    993 B    C++    
 2 #include<iostream>
 3 #include<vector>
 4 #define N 10005
 5 using namespace std;
 6 struct node{
 7     int v,w;
 8     node(int a,int b){
 9         v=a;w=b;
10     }
11 };
12 int dp[N][15];
13 vector<node>V[N];
14 int n,s,k;
15 void dfs(int u,int root)
16 {
17     int n0=V[u].size();
18     for(int i=0;i<n0;i++){
19         int v=V[u][i].v;
20         int w=V[u][i].w;
21         if(v==root) continue;
22         dfs(v,u);
23         for(int j=k;j>=0;j--){
24             dp[u][j]+=dp[v][0]+2*w;
25             for(int k0=1;k0<=j;k0++)
26                 dp[u][j]=min(dp[u][j],dp[u][j-k0]+dp[v][k0]+k0*w);
27         }
28     }
29 }
30 int main(void)
31 {
32     int u,v,w;
33     while(scanf("%d%d%d",&n,&s,&k)!=EOF)
34     {
35         memset(dp,0,sizeof(dp));
36         for(int i=0;i<=n;i++) V[i].clear();
37         for(int i=1;i<n;i++){
38             scanf("%d%d%d",&u,&v,&w);
39             V[u].push_back(node(v,w));
40             V[v].push_back(node(u,w));
41         }
42         dfs(s,-1);
43         printf("%d\n",dp[s][k]);     
44     }
45     return 0;
46 }
View Code

 

posted @ 2013-10-06 19:58  heaventouch  阅读(104)  评论(0编辑  收藏  举报