Optimal way of filling 2 knapsacks?

后端 未结 3 445
天命终不由人
天命终不由人 2020-12-08 11:28

The dynamic programming algorithm to optimally fill a knapsack works well in the case of one knapsack. But is there an efficient known algorithm that will optimally fill 2 k

3条回答
  •  借酒劲吻你
    2020-12-08 12:10

    The recursive formula is anybody is looking:

    Given n items, such that item i has weight wi and value pi. The two knapsacks havk capacities of W1 and W2.

    For every 0<=i<=n, 0<=a<=W1, 0<=b<=W2, denote M[i,a,b] the maximal value.

    for a<0 or b<0 - M[i,a,b] = −∞ for i=0, or a,b=0 - M[i,a,b] = 0

    The formula: M[i,a,b] = max{M[i-1,a,b], M[i-1,a-wi,b] + pi, M[i-1,a,b-wi] + pi}

    Every solution to the problem with i items either has item i in knapsack 1, in knapsack 2, or in none of them.

提交回复
热议问题