zoukankan      html  css  js  c++  java
  • HDU 4003

    Find Metal Mineral

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


    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个机器人从同一地点出发,求走完所有路的最小花费
    思路:
      分组背包,

    对于每个根节点root,有个容量为K的背包

    如果它有i个儿子,那么就有i组物品,价值分别为dp[son][0],dp[son][1].....dp[son][k] ,这些物品的重量分别为0,1,.....k

    现在要求从每组里选一个物品(且必须选一个物品)装进root的背包,使得容量不超过k的情况下价值最大。

    但是这里有一个问题,就是每组必须选一个物品。

    我们先将dp[son][0]放进背包,如果该组里有更好的选择,那么就会换掉这个物品,否则的话这个物品就是最好的选择。这样保证每组必定选了一个。

     

    dp[i][j] 表示以i为根节点子树,去j个机器人花费的最小值。

     

    知识

    分组背包:

    使用一维数组的“分组背包”伪代码如下:

    for 所有的组i

        for v = V..0

            for 所有的k属于组i

                f[v]=max{f[v],f[v-c[k]]+w[k]}

    AC代码:

     

     1 # include <bits/stdc++.h>
     2 using namespace std;
     3 const int MAX = 10010;
     4 struct node
     5 {
     6     int to;
     7     int val;
     8     int next;
     9 }tree[MAX * 2];
    10 int head[MAX];
    11 int tol;
    12 int dp[MAX][15];
    13 void add(int a, int b, int val)
    14 {
    15     tree[tol].to = b;
    16     tree[tol].val = val;
    17     tree[tol].next = head[a];
    18     head[a] = tol++;
    19 }
    20 int n, s, k;
    21 void dfs(int root, int f)
    22 {
    23     for(int i = head[root]; i != -1; i = tree[i].next)
    24     {
    25         int son = tree[i].to;
    26         if(son == f)
    27             continue;
    28         dfs(son, root);
    29         for(int j = k; j >= 0; j--)
    30         {
    31             dp[root][j] += dp[son][0] + 2 * tree[i].val; //先将dp[son][0]放进背包
    32             for(int l = 1; l <= j; l++)
    33             {
    34                 dp[root][j] = min(dp[root][j], dp[root][j - l] + dp[son][l] + l * tree[i].val);
    35                 // 更优的方案
    36             }
    37         }
    38     }
    39 }
    40 int main()
    41 {
    42     while(scanf("%d %d %d", &n, &s, &k) != EOF)
    43     {
    44         memset(dp, 0, sizeof(dp));
    45         memset(head, -1, sizeof(head));
    46         tol = 0;
    47         for(int i = 1; i < n; i++)
    48         {
    49             int a, b, val;
    50             scanf("%d %d %d", &a, &b, &val);
    51             add(a, b, val);
    52             add(b, a, val);
    53         }
    54         dfs(s, -1);
    55         printf("%d
    ", dp[s][k]);
    56     }
    57     return 0;
    58 }
    View Code

     

     

    生命不息,奋斗不止,这才叫青春,青春就是拥有热情相信未来。
  • 相关阅读:
    JobScheduler调度器过程(JobSchedulerService的启动过程)
    Android 9 新功能 及 API 介绍(提供了实用的模块化的功能支持,包括 人工智能)
    好用的在线工具汇总:Iconfont图标,数据mock,时间函数库,颜色查询 等
    前端编码规范小记
    android自定义控件 几种方式总结
    App开发如何利用Fidder,在api接口还没有实现的情况下模拟数据,继续开发
    WebView一般用法总结
    360等杀掉了app的主进程后 ,如何自动开启 如何防止被kill
    android内存优化
    dp跟px的互相转换
  • 原文地址:https://www.cnblogs.com/lyf-acm/p/5796625.html
Copyright © 2011-2022 走看看