What's the fastest way to solve knapsack prob with two properties

后端 未结 1 398
不思量自难忘°
不思量自难忘° 2021-01-20 07:10

Let\'s say we\'ve got an input:

10 // saying 1st property should be 10(in total)
10 // saying 2d property should be 10 (in total)
5 // saying theres 5 record         


        
相关标签:
1条回答
  • 2021-01-20 07:35

    You can use the same approach of knapsack problem, but instead of 2D matrix, you will have a 3D table, a dimension for each parameter (2 constraint + index).

    The recursive formula will be similar, but of course will be done for both parameters.

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

    (Base clauses will be similar as well, but with an extra dimension.)

    0 讨论(0)
提交回复
热议问题