简体   繁体   English

01 限制m件的背包问题

[英]01 knapsack problem with the limit of m items

There is a backpack with a capacity of C, n items to be selected, each item has one and only one, and their size and value are ci and vi (i=1,2,... ,n), how to select items from the items under the condition of loading at most m items to maximize the total value in the backpack?有一个容量为C的背包,需要选择n件物品,每件物品只有一件,大小和数值分别为ci和vi(i=1,2,...,n),如何选择在最多装载 m 件物品以最大化背包中的总价值的情况下,物品中的物品? Can someone tell me the specific idea of ​​this question, or is there any code to read?THS谁能告诉我这个问题的具体思路,或者有什么代码可以看?

You should define the following function f(i,j,k) which gives you the maximum value you can get by selecting exactly k items from the first i items (1,2..i) with maximum capacity of j.您应该定义以下函数 f(i,j,k),它通过从最大容量为 j 的前 i 个项目 (1,2..i) 中精确选择 k 个项目来为您提供最大值。

according to our definition the transitions will be:根据我们的定义,转换将是:

f(i , j , k) = max( t1 , t2 )

t1 = f(i-i , j , k) // here we did not pick the i-th item

t2 = vi + f(i-1 , j - ci , k-1)// here we picked the i-th item

the result to your question will be max( f(n,C,i) ) where i=1,2...n您的问题的结果将是 max( f(n,C,i) ) where i=1,2...n

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM