knapsack 01 with twist

前端 未结 4 1800
太阳男子
太阳男子 2021-01-12 23:55

I\'m doing a Knapsack in Java where we only use weights no value. The weightlimit is 1000. We get 5 weights scanned from keyboard which we use. The twist is that you can ac

4条回答
  •  旧时难觅i
    2021-01-13 00:31

    I would evaluate this problem first as a classical knapsack problem taking
    value[i] = weight[i] ;
    , where i is the i'th item and maximum weight to be the given max_wt (1000 kg) and item[] be an array containing the items in ascending order of their weights .

    Let the answer of this problem be x , say 990 kg , now i would calculate the difference 'd' ,
    d = max_wt - x ;
    iterate over item[] till item[i] exceeds d :
    int i = 0 ; while(item[i] < d ) i++; ,lastly add the first item that exceeds 'd' to the answer you got through the classical knapsack problem :
    answer = dp[n-1][w-1] + item[i] \\dp[n-1][w-1] is the answer of the classical \\knapsack problem

提交回复
热议问题