zoukankan      html  css  js  c++  java
  • Bone Collector(01背包问题入门)

    Bone Collector

    原题连接:点击打开链接

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 15290    Accepted Submission(s): 6055

    Problem Description
    Many years ago , in Teddy’s hometown there was a man who was called “Bone Collector”. This man like to collect varies of bones , such as dog’s , cow’s , also he went to the grave …
    The bone collector had a big bag with a volume of V ,and along his trip of collecting there are a lot of bones , obviously , different bone has different value and different volume, now given the each bone’s value along his trip , can you calculate out the maximum of the total value the bone collector can get ?

     
    Input
    The first line contain a integer T , the number of cases.
    Followed by T cases , each case three lines , the first line contain two integer N , V, (N <= 1000 , V <= 1000 )representing the number of bones and the volume of his bag. And the second line contain N integers representing the value of each bone. The third line contain N integers representing the volume of each bone.
     
    Output
    One integer per line representing the maximum of the total value (this number will be less than 231).
     
    Sample Input
    1 5 10 1 2 3 4 5 5 4 3 2 1
     
    Sample Output
    14
     
    Author
    Teddy
     
    Source
     
    Recommend
    lcy
     
    Statistic | Submit | Discuss | Note

    此题的易错点在于,物品的体积可为0,于是当背包剩余空间为0时,还是可能装物品的。

    AC Code:
     1 #include <iostream>
     2 #include <string>
     3 #include <cstdio>
     4 #include <cmath>
     5 #include <cstring>
     6 #include <algorithm>
     7 #include <map>
     8 #include <vector>
     9 #include <queue>
    10 #include <stack>
    11 #define LL long long
    12 #define MAXI 2147483647
    13 #define MAXL 9223372036854775807
    14 #define eps (1e-8)
    15 #define dg(i) cout << "*" << i << endl;
    16 
    17 using namespace std;
    18 
    19 int vol[1001], val[1001], maxVal[1001][1001];
    20 
    21 int max (const int a, const int b) {return a > b ? a : b;}
    22 
    23 int main()
    24 {
    25     int T, N, V, ans;
    26     scanf("%d", &T);
    27     while(T--)
    28     {
    29         scanf("%d %d", &N, &V);
    30         for(int i = 1; i <= N; ++i) scanf("%d", &val[i]);
    31         for(int i = 1; i <= N; ++i) scanf("%d", &vol[i]);
    32         if(V == 0)
    33         {
    34             ans = 0;
    35             for(int i = 1; i <= N; ++i)
    36                if (!vol[i]) ans += val[i];
    37         }
    38         else if(N == 0)  ans = 0;
    39         else
    40         {
    41             for(int i = 1; i <= N; ++i)
    42             {
    43                 for(int j = 0; j <= V; ++j)
    44                 {
    45                     //if(i == 0 || j == 0)  maxVal[i][j] = 0;
    46                     //else……
    47                     /*上面的语句是错误的,当j等于0时,表明此时背包容量为0,按理说不能装任何东西,故而
    48                     maxVal[i][0] = 0,但是,在这题中,物品的体积可为0,故此背包容量为0但是还是可以装物品。
    49                     可以先设maxVal[i][0] = 0,但是由于使用if-else语句,设maxVal[i][0] = 0之后就不执行了,
    50                     无法更新maxVal[i][0] = 0*/
    51                     if(vol[i] > j)  maxVal[i][j] = maxVal[i - 1][j];
    52                     else  maxVal[i][j] = max(maxVal[i - 1][j], maxVal[i - 1][j - vol[i]] + val[i]);
    53                 }
    54             }
    55             ans = maxVal[N][V];
    56         }
    57         printf("%d\n", ans);
    58     }
    59     return 0;
    60 }

  • 相关阅读:
    一步步用新浪SAE免费教大家搭建个人博客(wordpress 3.0.4 for SAE )
    欢迎大家来访西北狼网络乌托邦
    教大家如何让新浪SAE上安装wordpress实现伪静态
    CSDN 600万用户数据信息泄露并道歉
    推荐5款好用的屏幕录像软件
    IPv6无法解决全部安全问题
    详解STP以及工作过程
    如何在WordPress中实现彩色标签云
    EIGRP和RIP的一个综合性很强的实验(变态实验之一)
    查看系统等待的sql
  • 原文地址:https://www.cnblogs.com/cszlg/p/2910437.html
Copyright © 2011-2022 走看看