简体   繁体   English

两个袋子的背包算法

[英]Knapsack algorithm for two bags

I've found thread which provides pseudo-code for knapsack algorithm with 2 knapsacks. 我发现了为2个背包提供背包算法的伪代码的线程 I've tried implement it in C++, but it doesn't work as suppose. 我试过用C ++实现它,但是它不能像预期的那样工作。 Here's code: 这是代码:

#include <cstdio>
#define MAX_W1 501
#define MAX_W2 501

int maximum(int a, int b, int c) {
    int max = a>b?a:b;
    return c>max?c:max;
}

int knapsack[MAX_W1][MAX_W2] = {0};

int main() {
    int n, s1, s2, gain, weight; // items, sack1, sack2, gain, cost

    scanf("%d %d %d", &n, &s1, &s2);

    // filing knapsack
    for (int i = 0; i < n; i++) {
        scanf("%d %d", &gain, &weight);

        for (int w1 = s1; w1 >= weight; w1--) {
            for (int w2 = s2; w2 >= weight; w2--) {
                knapsack[w1][w2] = maximum(
                    knapsack[w1][w2],                 // we have best option
                    knapsack[w1 - weight][w2] + gain, // put into sack one
                    knapsack[w1][w2 - weight] + gain  // put into sack two
                );
            }
        }
    }

    int result = 0;

    // searching for result
    for (int i = 0; i <= s1; i++) {
        for (int j = 0; j <= s2; j++) {
            if (knapsack[i][j] > result) {
                result = knapsack[i][j];
            }
        }
    }

    printf("%d\n", result);

    return 0;
}

For instance for following input: 例如,以下输入:

5 4 3
6 2
3 2
4 1
2 1
1 1

I have output: 我有输出:

13

Obviously it's wrong because I can take all items (1,2 into first bag and rest to second bag) and sum is 16. I would be grateful for any explanation where I get pseudo-code wrong. 显然这是错误的,因为我可以将所有物品(第一个袋子中的1,2个放进第二个袋子中,其余的全部放进去),总和为16。对于伪代码错误的任何解释,我将不胜感激。

I made little update since, some people have problem with understanding the input format: 我没有做太多更新,因为有些人在理解输入格式方面存在问题:

  1. First line contains 3 numbers as follows number of items, capacity of sack one, capacity of sack two 第一行包含3个数字,如下所示:项目数,袋的容量,袋的容量2
  2. Later on there are n lines where each contains 2 numbers: gain, cost of i-th item. 稍后有n行,每行包含2个数字:收益,第i个项目的成本。
  3. Assume that sacks cannot be larger than 500. 假设麻袋不能大于500。

Here is modification to code to make it work:- 这是对代码的修改以使其工作:-

#include <cstdio>
#define MAX_W1 501
#define MAX_W2 501

int maximum(int a, int b, int c) {
    int max = a>b?a:b;
    return c>max?c:max;
}

int knapsack[MAX_W1][MAX_W2] = {0};

int main() {
    int n, s1, s2, gain, weight; // items, sack1, sack2, gain, cost

    scanf("%d %d %d", &n, &s1, &s2);

    // filing knapsack
    for (int i = 0; i < n; i++) {
        scanf("%d %d", &gain, &weight);
    // need to fill up all the table cannot stop if one sack is full because item might fit in other
        for (int w1 = s1; w1 >= 0; w1--) {
            for (int w2 = s2; w2 >= 0; w2--) {
                 int val1=0,val2=0;
                 if(weight<=w1)
                   val1 = knapsack[w1 - weight][w2] + gain;
                 if(weight<=w2)
                   val2 = knapsack[w1][w2 - weight] + gain;

                 knapsack[w1][w2] = maximum(
                    knapsack[w1][w2],                   // we have best option
                     val1,              // put into sack one
                     val2               // put into sack two
                  );


            }
        }
    }


    // No need to search for max value it always be Knapsack[s1][s2]
    printf("%d\n", knapsack[s1][s2]);

    return 0;
}

The algorithm you're using appears incorrect, because it will only consider cases where the object happens to fit in both sacks. 您使用的算法似乎不正确,因为它只会考虑对象恰好适合两个麻袋的情况。 I made the following changes to your code and it operates correctly now: 我对您的代码进行了以下更改,并且现在可以正常运行:

#include <algorithm>

using std::max;

int max3(int a, int b, int c) {
    return max(a, max(b, c));
}

and

for (int w1 = s1; w1 >= 0; w1--) {
    for (int w2 = s2; w2 >= 0; w2--) {
        if (w1 >= weight && w2 >= weight) // either sack has room
        {
            knapsack[w1][w2] = max3(
                knapsack[w1][w2],                 // we have best option
                knapsack[w1 - weight][w2] + gain, // put into sack one
                knapsack[w1][w2 - weight] + gain  // put into sack two
            );
        }
        else if (w1 >= weight) // only sack one has room
        {
            knapsack[w1][w2] = max(
                knapsack[w1][w2],                 // we have best option
                knapsack[w1 - weight][w2] + gain  // put into sack one
            );
        }
        else if (w2 >= weight) // only sack two has room
        {
            knapsack[w1][w2] = max(
                knapsack[w1][w2],                 // we have best option
                knapsack[w1][w2 - weight] + gain  // put into sack two
            );
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM