关闭

HDU 4003 Find Metal Mineral (树形dp)

标签: HDU4003Find Metal Mineral树形dp
264人阅读 评论(0) 收藏 举报
分类:

Find Metal Mineral

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

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;
Source
Recommend
lcy   |   We have carefully selected several similar problems for you:  4001 4007 4006 4009 4008 

题解:树形dp。设dp[ i ][ j ]表示对于以 i 结点为根结点的子树,放 j 个机器人所需要的权值和。先将dp[son][0]放进树中,因为dp[son][0]是表示用一个机器人去走完所有子树,最后又回到 pos这个节点,所以花费要乘以2。然后再找到最优的,这里类似于分组背包。

AC代码:
#include<bits/stdc++.h>
using namespace std;
struct Node
{
    int now,next,val;
}tree[20005];

int dp[10005][15];
int head[10005];
int n,s,k,len;

void addedge(int x,int y,int w)
{
    tree[len].now = y;
    tree[len].val = w;
    tree[len].next = head[x];
    head[x] = len++;
}

void dfs(int root,int fa)
{
    
    for(int i = head[root];i!=-1;i = tree[i].next)
    {
        int son = tree[i].now;
        if(son == fa)  continue;
        dfs(son,root);
        for(int j = k; j>=0;j--)
        {
            //先将dp[son][0]放进树中,因为dp[son][0]是表示用一个机器人去走完所有子树,最后又回到 pos这个节点,所以花费要乘以2
            dp[root][j]+=dp[son][0]+2*tree[i].val;
            for(int l = 1;l<=j;l++)//再找到更优的,就是分组背包 
            dp[root][j] = min(dp[root][j],dp[root][j-l]+dp[son][l]+l*tree[i].val);
        }
    }
}

int main()
{
    int i,x,y,w;
    while(~scanf("%d%d%d",&n,&s,&k))
    {
        len = 0;
        memset(head,-1,sizeof(head));
        memset(dp,0,sizeof(dp));
        for(i = 1;i<n;i++)
        {
            scanf("%d%d%d",&x,&y,&w);
            addedge(x,y,w);
            addedge(y,x,w);
        }
        dfs(s,0);
        printf("%d\n",dp[s][k]);
    }
    return 0;
}
WA:
#include<bits/stdc++.h>
#include<vector>
using namespace std;
vector<int> V[20010];
//dp[i][j]表示对于以i结点为根结点的子树,放j个机器人所需要的权值和。 
int dp[20010][30];
int val[20010];
int n,s,k;
void dfs(int root,int fa)
{
    
    for(int i=0; i< V[root].size();i++)
    {
        int son=V[root][i];
        if(son==fa)continue;
        dfs(son,root);
        for(int j=k;j>=0;--j)
        {
            //先将dp[son][0]放进树中,因为dp[son][0]是表示用一个机器人去走完所有子树,最后又回到 pos这个节点,所以花费要乘以2 
            dp[root][j]+=dp[son][0]+ 2*val[i];
            //再找到更优的
            for(int l=1;l<=j;l++) 
            {
                dp[root][j] = min(dp[root][j],dp[root][j-l]+dp[son][l]+l*val[i]);
            }
        }
    }
}

int main()
{
    int x,y;
    while(~scanf("%d%d%d",&n,&s,&k))
    {
        for(int i=0;i<n;i++) V[i].clear();
        memset(dp,0,sizeof(dp));
        for(int i=0;i<n-1;i++)
        {
            scanf("%d%d%d",&x,&y,&val[i]);
            V[x].push_back(y);
            V[y].push_back(x);
        }
        dfs(s,0);
        printf("%d\n",dp[s][k]);
    }
    return 0;
}


2
0
查看评论

哈尔滨理工大学软件学院ACM程序设计全国邀请赛

哈尔滨理工大学软件学院ACM程序设计全国邀请赛A GoldsDescriptionHumans have discovered a kind of new metal mineral on Mars which are distributed in point‐like with paths con...
  • DorMOUSENone
  • DorMOUSENone
  • 2016-12-04 16:44
  • 506

hdu 4003 Find Metal Mineral 树形dp+分组背包

Find Metal Mineral Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) Total Submission(s): 2503...
  • smz436487
  • smz436487
  • 2014-10-02 19:24
  • 250

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...
  • corncsd
  • corncsd
  • 2014-04-09 20:19
  • 324

(中等) 树形分组背包 HDU 4003 Find Metal Mineral

Find Metal MineralTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)Total Submission(s): 1952...
  • wsx1754175
  • wsx1754175
  • 2014-02-08 09:52
  • 304

hdu 4003 Find Metal Mineral (树形dp)

Find Metal Mineral Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) Total Submission(s): 2122...
  • u010228612
  • u010228612
  • 2014-03-15 17:03
  • 750

HDU 4003 Find Metal Mineral (树形dp)

Find Metal Mineral Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) Total Submission(s): 3463&#...
  • liangzhaoyang1
  • liangzhaoyang1
  • 2016-10-15 22:50
  • 344

HDU 4003 Find Metal Mineral 树形DP

题意:人类在火星上发现一个金属矿。金属矿的道路是个树形的。在每个叶子节点有矿。现在从根结点S派出K个机器人,去取所有叶子节点的矿,希望所有机器人走的距离之和最短。 思路:通过模拟可以发现,影响状态的是,有几个机器人最终留在了以u为根的子树中。 所以,定义状态dp[u][j]为以u为根的子树,最终...
  • u012139398
  • u012139398
  • 2015-02-22 10:37
  • 355

hdu 4003 Find Metal Mineral 树形DP

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4003 题意:有n个矿,矿之间有n-1条路,每条路有相应花费。有k个机器人,从s点出发,求遍历每个矿的最小花费。 对于一个根节点s,如果机器人足够遍历所有的子树,可以看做常规的树状dp,分配c...
  • zchahaha
  • zchahaha
  • 2016-05-12 22:23
  • 354

hdu 4003 Find Metal Mineral(树形DP)

题意:
  • zizaimengzhongyue
  • zizaimengzhongyue
  • 2014-05-09 15:13
  • 702

hdu 4003 Find Metal Mineral (树形背包dp)

题意 给一棵n个节点的树, 节点编号为1~n, 每条边都有一个花费值. 有k个机器人从S点出发, 问让机器人遍历所有边,最少花费值多少? 思路 很好的一题, 推荐! 前天看的这题, 今天才想出来的. 方法想出来后,代码很简单 最近做的几道dp,都是一开...
  • shuangde800
  • shuangde800
  • 2013-08-24 00:30
  • 2324
    个人资料
    • 访问:765602次
    • 积分:18445
    • 等级:
    • 排名:第604名
    • 原创:1032篇
    • 转载:8篇
    • 译文:0篇
    • 评论:123条
    博客专栏
    文章分类
    最新评论