本文介绍了两个袋子的背包算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了线程,它为背包算法提供了伪代码有2个背包.我试过用C ++实现它,但是它不能像预期的那样工作.这是代码:

I've found thread which provides pseudo-code for knapsack algorithm with 2 knapsacks. I've tried implement it in C++, but it doesn't work as suppose. 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;
}

例如用于以下输入:

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

我有输出:

13

显然这是错误的,因为我可以将所有物品(第一个袋子中的1,2放进第二个袋子中,剩下的放到第二个袋子中),总和为16.对于伪代码错误的任何解释,我将不胜感激.

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. 第一行包含3个数字,如下所示:项目数,一个袋子的容量,两个袋子的容量
  2. 以后有n行,每行包含2个数字:收益,第i个项目的成本.
  3. 假设麻袋不能大于500.

推荐答案

您使用的算法似乎不正确,因为它只会考虑对象恰好适合两个麻袋的情况.我对您的代码进行了以下更改,并且现在可以正常运行:

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));
}

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
            );
        }
    }
}

这篇关于两个袋子的背包算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-26 19:37