Find Metal Mineral HDU - 4003 (树行dp+分组背包)

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个机器人,要求用这些机器人把树上所有点都遍历一遍,求最小的花费

思路:设dp[i][j]表示以i节点为根的子数,有j个机器人花费的最小费用,转移就是考虑如何将j个机器人分配给它的子节点就行,但是有个坑点,就是dp[i][0]的意义,因为我们不可能让一个子树的跟节点没有机器人,因为这样我们无法分配给它的子节点,所以dp[i][0]表示分给i一个机器人,这个机器人又返回父亲节点的值。

当j=0时表示放了一个机器人下去,遍历完结点后又回到i结点了

代码:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <map>
#include <vector>
#include <set>
#include <string>
#include <math.h>
#include <stack>
using namespace std;
typedef long long ll;
const int maxn = 1e5+10;
#define inf 0x3f3f3f3f
int n,s,K;
int num;
int head[maxn];
int dp[10005][30];
struct Edge
{
    int u,v,next,w;
}edge[maxn<<1];
int h[maxn];
int w[maxn];
void addEdge(int u,int v,int w)
{
    edge[num].u=u;
    edge[num].v=v;
    edge[num].w=w;
    edge[num].next=head[u];
    head[u]=num++;
}
void init()
{
    num=0;
    memset(head,-1,sizeof(head));
    memset(dp,0,sizeof(dp));
}


void dfs(int u,int pre)
{
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        int v=edge[i].v,w=edge[i].w;
        if(v==pre) continue;
        dfs(v,u);
        for(int j=K;j>=0;j--)
        {
            dp[u][j]+=dp[v][0]+2*w;//先把dp[u][0]放进背包,保证至少选一个
            for(int k=1;k<=j;k++)
            {
                dp[u][j]=min(dp[u][j],dp[v][k]+k*w+dp[u][j-k]);
            }
        }
    }
}


int main(int argc, char const *argv[])
{
     #ifndef ONLINE_JUDGE
        freopen("in.txt","r",stdin);
        freopen("out.txt","w",stdout);
    #endif
    while(scanf("%d%d%d",&n,&s,&K)!=EOF)
    {
        init();
        for(int i=1;i<n;i++)
        {
            int x,y,w;
            scanf("%d%d%d",&x,&y,&w);
            addEdge(x,y,w);
            addEdge(y,x,w);
        }
        dfs(s,s);
        cout<<dp[s][K]<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40774175/article/details/81357836