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 ?
Bone Collector(01背包问题入门)_第1张图片
 

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 2 31).
 

Sample Input
   
   
   
   
1 5 10 1 2 3 4 5 5 4 3 2 1
 

Sample Output
   
   
   
   
14
 

Author
Teddy
 

Source
HDU 1st “Vegetable-Birds Cup” Programming Open Contest
 

Recommend
lcy
 

Statistic |  Submit |  Discuss |  Note
此题的易错点在于,物品的体积可为0,于是当背包剩余空间为0时,还是可能装物品的。
AC Code:
#include <iostream>
#include <string>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <map>
#include <vector>
#include <queue>
#include <stack>
#define LL long long
#define MAXI 2147483647
#define MAXL 9223372036854775807
#define eps (1e-8)
#define dg(i) cout << "*" << i << endl;

using namespace std;

int vol[1001], val[1001], maxVal[1001][1001];

int max (const int a, const int b) {return a > b ? a : b;}

int main()
{
    int T, N, V, ans;
    scanf("%d", &T);
    while(T--)
    {
        scanf("%d %d", &N, &V);
        for(int i = 1; i <= N; ++i) scanf("%d", &val[i]);
        for(int i = 1; i <= N; ++i) scanf("%d", &vol[i]);
        if(V == 0)
        {
            ans = 0;
            for(int i = 1; i <= N; ++i)
               if (!vol[i]) ans += val[i];
        }
        else if(N == 0)  ans = 0;
        else
        {
            for(int i = 1; i <= N; ++i)
            {
                for(int j = 0; j <= V; ++j)
                {
                    //if(i == 0 || j == 0)  maxVal[i][j] = 0;
                    //else……
                    /*上面的语句是错误的,当j等于0时,表明此时背包容量为0,按理说不能装任何东西,故而
                    maxVal[i][0] = 0,但是,在这题中,物品的体积可为0,故此背包容量为0但是还是可以装物品。
                    可以先设maxVal[i][0] = 0,但是由于使用if-else语句,设maxVal[i][0] = 0之后就不执行了,
                    无法更新maxVal[i][0] = 0*/
                    if(vol[i] > j)  maxVal[i][j] = maxVal[i - 1][j];
                    else  maxVal[i][j] = max(maxVal[i - 1][j], maxVal[i - 1][j - vol[i]] + val[i]);
                }
            }
            ans = maxVal[N][V];
        }
        printf("%d\n", ans);
    }
    return 0;
}


你可能感兴趣的:(Bone Collector(01背包问题入门))