hdu4003 Find Metal Mineral 树形DP

本文探讨了在火星上利用机器人收集金属矿物的问题,通过分析路径和能量成本,提出了优化路径规划的方法,以减少总能耗。具体而言,研究如何在已知起点的情况下,为多个机器人制定最优路线,确保每处矿物都能被有效收集,同时最小化能源消耗。

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


  一棵树,有K个机器人,一个机器人从一个点到另外一个点要消耗能量,机器人开始都在S点,问走遍所有的点最少消耗多少能量。

  树形DP的思想一般都是分组背包,每个子节点看成一个组,从每个组中再去dp每个状态。在这道题中状态就是在每个组中选k个机器人。

  dp[u][i]代表节点u有i个机器人遍历所有子树的最小消耗,这道题很特别的想法是dp[u][0]代表节点u有1个机器人遍历完子树后又回到节点u的消耗,每次算dp[u][c]的时候先把dp[v][0]+2*w加上,这样可以保证子树的所有点都被走到。接着再去更新dp[u][c]的最小值。

 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<cstdlib>
#define INF 0x3f3f3f3f
#define MAXN 10010
#define MAXM 15
#define MAXNODE 4*MAXN
#define pii pair<int,int>
using namespace std;
int N,S,K,vis[MAXN],dp[MAXN][MAXM];
vector<pii> V[MAXN];
void DFS(int u){
    vis[u]=1;
    int L=V[u].size();
    for(int i=0;i<L;i++){
        int v=V[u][i].first,w=V[u][i].second;
        if(vis[v]) continue;
        DFS(v);
        for(int c=K;c>=0;c--){
            dp[u][c]+=dp[v][0]+2*w;
            for(int j=1;j<=c;j++) dp[u][c]=min(dp[u][c],dp[u][c-j]+dp[v][j]+j*w);
        }
    }
}
int main(){
    freopen("in.txt","r",stdin);
    while(scanf("%d%d%d",&N,&S,&K)!=EOF){
        for(int i=1;i<=N;i++) V[i].clear();
        for(int i=0;i<N-1;i++){
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            V[u].push_back(make_pair(v,w));
            V[v].push_back(make_pair(u,w));
        }
        for(int i=0;i<=N;i++){
            vis[i]=0;
            for(int j=0;j<=K;j++) dp[i][j]=0;
        }
        DFS(S);
        printf("%d\n",dp[S][K]);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值