【问题标题】:What's the fastest way to solve knapsack prob with two properties解决具有两个属性的背包问题的最快方法是什么
【发布时间】:2012-11-15 18:52:22
【问题描述】:

假设我们有一个输入:

10 // saying 1st property should be 10(in total)
10 // saying 2d property should be 10 (in total)
5 // saying theres 5 records below
// (1st property) (2nd property) (cost)
2 5 8 
7 3 10 
4 2 9
4 3 5
8 5 15

在这种情况下,输出将如下所示:

22 // lowest possible cost
1 3 4 // indexes of records, we've been using (indexing starts with 1)

 2  5  8
 4  2  9
 4  3  5
+---------
 10 10 22

如果无法将这些属性设为 10 和 10,程序将输出 -1; 我知道如何解决背包问题,但是我不知道如何解决这个问题。

【问题讨论】:

    标签: algorithm knapsack-problem


    【解决方案1】:

    您可以使用与背包问题相同的方法,但您将拥有一个 3D 表格,而不是 2D 矩阵,每个参数都有一个维度(2 个约束 + 索引)。

    递归公式将类似,但当然会为两个参数完成。

    f(item,cost1,cost2) = max {
                   f(item-1,cost1,cost2), 
                   f(item,cost1-firstCost[i],cost2-secondCost[i]) + profit[i]
                              }
    

    (基本子句也类似,但有一个额外的维度。)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-31
      • 2011-11-10
      • 2019-11-28
      相关资源
      最近更新 更多