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个机器人,求最小的消耗。
 
 #include<stdio.h>
#include<stdlib.h>
#include<string.h> int head[],len;
struct node
{
int to;
int next;
int val;
}f[*];
bool use[];
int dp[][],rong; int Min(int x,int y)
{
return x>y? y:x;
}
void add(int x,int y,int w)
{
f[len].to=x;
f[len].val=w;
f[len].next=head[y];
head[y]=len++;
} void dfs(int k)
{
int i,t,K,s;
use[k]=true;
for(i=head[k];i!=-;i=f[i].next)
{
t = f[i].to;
if(use[t]==true) continue;
dfs(t);
for(K=rong;K>=;K--)
{
dp[k][K]+=dp[t][]+*f[i].val;
for(s=;s<=K;s++)
{
dp[k][K]=Min(dp[k][K],dp[k][K-s]+dp[t][s]+(s*f[i].val) );
}
}
}
} int main()
{
int n,s,i;
int x,y,w;
while(scanf("%d%d%d",&n,&s,&rong)>)
{
memset(head,-,sizeof(head));
memset(dp,,sizeof(dp));
memset(use,false,sizeof(use));
len=; for(i=;i<n;i++)
{
scanf("%d%d%d",&x,&y,&w);
add(x,y,w);
add(y,x,w);
}
dfs(s);
printf("%d\n",dp[s][rong]);
}
return ;
}
上一篇:024 Android 自定义样式对话框(AlertDialog)


下一篇:AlertDialog具体解释