Knapsack algorithm with an additional property

前端 未结 2 1096
隐瞒了意图╮
隐瞒了意图╮ 2021-02-05 20:53

When there\'s 1 property, I do understand what\'s going on in there. I\'m having a problem with understanding knapsack problem when there\'s more than 1 property.

2条回答
  •  隐瞒了意图╮
    2021-02-05 21:16

    Say you are using a three dimension table: A[x][y][z]=k, x: sum 1st property; y: sum 2nd property; z: sum 3rd property; k: minimal cost (maximal reward, which I prefer using reward)

    So you iterate over items. Say current item is (p1, p2, p3, reward) (reward = - cost). for each (x,y,z,k), your update formula:

    A[x+p1][y+p2][z+p3] = max(A[x+p1][y+p2][z+p3], A[x+p1][y+p2][z+p3] + reward)
    

    If the 1st term on RHS is greater, on slot A[x+p1][y+p2][z+p3], the configuration of knapsack is remain still; otherwise, you update the knapsack by that of A[x+p1][y+p2][z+p3] plus the current item.

    Hope this cut things clear.

提交回复
热议问题