7
13
2015
0

HDU4003:Find Metal Mineral

Find Metal Mineral

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

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

    感觉今天做的题目还是比较有意义的就写一下简略的题解

    这道题打做法是用DP[i][j]表示在第i个节点用j个机器人遍历他的子树的最小代价,当j==0是就表示有一个机器人遍历了这颗子树然后回到了他的父节点,然后做一下背包即可

#include <cstdio>
#include <cstring>
#include <algorithm>
#define N 100010
using namespace std;
struct data{int x,y,s,next;} e[3*N];
int head[N],cnt,x,y,c,s,k,dp[N][20],n;
void Insert(int x,int y,int s){
    e[++cnt].x = x;e[cnt].y = y;e[cnt].s = s;
    e[cnt].next = head[x];head[x] = cnt;
}
void dfs(int x,int fa){
    for (int i=head[x];i;i=e[i].next)
        if (e[i].y!=fa){
            dfs(e[i].y,x);
            for (int j=k;j>=0;j--){
                dp[x][j]+=dp[e[i].y][0]+e[i].s*2;
                for (int l=1;l<=j;l++)
                    dp[x][j] = min(dp[x][j],dp[x][j-l]+dp[e[i].y][l]+e[i].s*l);
            }
        }
}
int main(){
    while (~scanf("%d%d%d",&n,&s,&k)){
        memset(dp,0,sizeof(dp));
        memset(head,0,sizeof(head));
        cnt = 0;
        for (int i=1;i<n;i++){
            scanf("%d%d%d",&x,&y,&c);
            Insert(x,y,c);
            Insert(y,x,c);
        }
        dfs(s,-1);
        printf("%d\n",dp[s][k]);
    }
}
Category: HDU | Tags: | Read Count: 448

登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter

Host by is-Programmer.com | Power by Chito 1.3.3 beta | Theme: Aeros 2.0 by TheBuckmaker.com