hdu 4003 Find Metal Mineral 树形dp ,*****
Find Metal Mineral
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others) Total Submission(s): 2018 Accepted Submission(s): 913
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
题意:
机器人探索火星,出发点为s。有n的金属矿,k个机器人。
n-1条线路,满足 x y z 代表 x 到 y 或 y到x 都要花费z的钱。
求让你遍历所有的点,就是n个金属矿。花费最小。
这一题,学到了很多的东西。
题意: 遍历一棵树全部的点,有k个机器人,求最小的消耗。
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 5 int head[10002],len; 6 struct node 7 { 8 int to; 9 int next; 10 int val; 11 }f[10003*2]; 12 bool use[10003]; 13 int dp[10003][12],rong; 14 15 int Min(int x,int y) 16 { 17 return x>y? y:x; 18 } 19 void add(int x,int y,int w) 20 { 21 f[len].to=x; 22 f[len].val=w; 23 f[len].next=head[y]; 24 head[y]=len++; 25 } 26 27 void dfs(int k) 28 { 29 int i,t,K,s; 30 use[k]=true; 31 for(i=head[k];i!=-1;i=f[i].next) 32 { 33 t = f[i].to; 34 if(use[t]==true) continue; 35 dfs(t); 36 for(K=rong;K>=0;K--) 37 { 38 dp[k][K]+=dp[t][0]+2*f[i].val; 39 for(s=1;s<=K;s++) 40 { 41 dp[k][K]=Min(dp[k][K],dp[k][K-s]+dp[t][s]+(s*f[i].val) ); 42 } 43 } 44 } 45 } 46 47 int main() 48 { 49 int n,s,i; 50 int x,y,w; 51 while(scanf("%d%d%d",&n,&s,&rong)>0) 52 { 53 memset(head,-1,sizeof(head)); 54 memset(dp,0,sizeof(dp)); 55 memset(use,false,sizeof(use)); 56 len=0; 57 58 for(i=1;i<n;i++) 59 { 60 scanf("%d%d%d",&x,&y,&w); 61 add(x,y,w); 62 add(y,x,w); 63 } 64 dfs(s); 65 printf("%d\n",dp[s][rong]); 66 } 67 return 0; 68 }