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): 2007    Accepted Submission(s): 909


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;
 
树形dp
 
题意:一棵有权树,从根结点中放入K个机器人,求用这K个机器人遍历所有的结点最少的权值和 
分析:dp[i][j]表示对于以i结点为根结点的子树,放j个机器人所需要的权值和。 
核心代码:dp[u][j] = min( dp[u][j], dp[u][j-k] + dp[v][k] );
 
#include <stdio.h>
#include <string.h>
#include <iostream>
#define maxn 10100
#define INF 1000000000
#define min(a, b) a < b ? a : b
using namespace std;
 
struct Edge{
    int v;
    int w;
    int next;
}e[maxn*2];
 
int pre[maxn];
__int64 dp[maxn][15];
int n, m, index;
 
void Add( int u, int v, int w ){
    e[index].v = v;
    e[index].w = w;
    e[index].next = pre[u];
    pre[u] = index++;
 
    e[index].v = u;
    e[index].w = w;
    e[index].next = pre[v];
    pre[v] = index++;
}
 
void DFS( int u, int p ){
    for ( int i = pre[u]; i != -1; i = e[i].next ){
        if ( e[i].v == p ){
            continue;
        }
        int v = e[i].v;
        if ( !(pre[v] == -1)  ){
            DFS(v, u);
        }
        for ( int j = 1; j <= m; j++ ){
            dp[v][j] += e[i].w * j;
        }
        dp[v][0] += e[i].w * 2;
        for ( j = m; j >= 0; j-- ){
            dp[u][j] += dp[v][0];
            for ( int k = 0; k <= m && k <= j; k++ ){
                dp[u][j] = min( dp[u][j], dp[u][j-k] + dp[v][k] );
            }
        }
    }
}
 
int main(){
    int s;
    while ( ~scanf("%d%d%d", &n, &s, &m) ){
        memset(dp, 0, sizeof(dp));
        index = 0;
        memset(pre, -1, sizeof(pre));
        for ( int i = 0; i < n-1; i++ ){
            int u, v, w;
            scanf("%d%d%d", &u, &v, &w);
            Add(u, v, w);
        }
 
        DFS(s, 0);
        printf("%I64d\n", dp[s][m]);
    }
    return 0;
}
 
posted @ 2013-10-21 21:14  /bin  阅读(153)  评论(0编辑  收藏  举报