hdu4003 树形dp+分组背包

文章介绍了一种使用树形dp和分组背包算法解决火星金属收集问题的方法,其中人类在火星上发现了一种新型金属矿物,并通过机器人进行收集。文章详细解释了输入和输出格式,并提供了示例输入输出来帮助理解解决方案。

http://acm.hdu.edu.cn/showproblem.php?pid=4003

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;
这道题我的解题报告如果看不懂,就结合kuangbin巨巨的一起来看,效果会好一些~

http://www.cnblogs.com/kuangbin/archive/2012/08/29/2661928.html

/**
hdu 4003  树形dp+分组背包
题目大意:有n个点构成的一棵树,要求用K个机器人跑最小的距离把每个点都遍历到
解题思路:
        对于其中一个顶点,对于它的所有子结点看做一个种类,每一类中包含价值分别为:dp[son][0],dp[son][1]....dp[son][k]重量分别为0~k的物品。
状态转移方程:dp[u][k]=min(dp[u][k],dp[u][k-j]+(dp[v][j]+j*edge[u].w));(与父节点建立联系必须有j*edge[u].w)
这样借助分组背包就可以搞定,但是这样仅仅能保证在每一类物品中取一种或不取时的最优解,而题目要求的是所有类中必须都选一个,因此我们首先用dp[u][0]
表示用一个机器人走遍全部顶点的解,后面状态转移即可。
值得一提的是:在dp进行顶点u时必须保证所有儿子结点的全部状态必须已经求出,因此要用到dfs
*/
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;

int head[10005],ip,dp[10005][11];
int n,s,K;

struct note
{
    int v,w,next;
}edge[10005*2];

void init()
{
    memset(head,-1,sizeof(head));
    ip=0;
}

void addedge(int u,int v,int w)
{
    edge[ip].v=v,edge[ip].w=w,edge[ip].next=head[u],head[u]=ip++;
}

void dfs(int u,int pre)
{
    for(int i=head[u];i!=-1;i=edge[i].next)///相当于枚举所有的种类
    {
        int v=edge[i].v;
        if(v==pre)continue;
        dfs(v,u);
        for(int k=K;k>=0;k--)///背包容量枚举
        {
            dp[u][k]+=dp[v][0]+2*edge[i].w;
            for(int j=1;j<=k;j++)///该种类下所有的物品
            {
                dp[u][k]=min(dp[u][k],dp[u][k-j]+(dp[v][j]+j*edge[i].w));
            }
        }
    }
}
int main()
{
    while(~scanf("%d%d%d",&n,&s,&K))
    {
        init();
        for(int i=0;i<n-1;i++)
        {
            int u,v,c;
            scanf("%d%d%d",&u,&v,&c);
            addedge(u,v,c);
            addedge(v,u,c);
        }
        memset(dp,0,sizeof(dp));
        dfs(s,-1);
        printf("%d\n",dp[s][K]);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值