简体   繁体   English

各种物品的背包

[英]knapsack with variety of items

Suppose a regular knapsack problem: you have a weight constraint, C , the number of items with Value and Weight (V, W) . 假设有一个常规的背包问题:您有一个重量约束C ,即具有ValueWeight (V, W) You want to maximize the V while W being under the C . 您想在W低于C时最大化V In this question, you can only have one of each item. 在这个问题中,每个项目只能有一个。

But there is an additional twist to the problem. 但是,这个问题还有一个额外的转折。 You want to have a variety of items. 您想要各种各样的物品。 Suppose the question states that you want to have at least 5 (or any number) of different items. 假设问题指出您希望拥有至少5个(或任意数量)不同的项目。 If a solution has any less than 5 different items, the answer is not valid. 如果一个解决方案的项目少于5个,则答案无效。 Is there an approach to this problem that solves this? 有解决这个问题的方法吗?

It's just another form of constraint, so lets see how Weight (at most C weight) and Diversity (at least 5 different items) differ: 这只是约束的另一种形式,因此让我们看一下权重(最多C权重)和多样性(至少5个不同的项)如何不同:

  • Weight starts as a valid (empty bag is below C), while Diversity starts as invalid (empty bag doesn't contain 5 different items). 重量从有效值开始(空袋子低于C),而分集从无效开始(空袋子中不包含5个不同的物品)。

The first thing to note is, that with the additional constraint you need a notion of invalid / unsolvable, because if there aren't 5 different items that satisfy the weight constraint, there is no solution. 首先要注意的是,有了附加约束,您需要一个无效/不可解决的概念,因为如果不存在5个满足权重约束的不同项目,那么就没有解决方案。

Once you defined a way to return some invalid result, it's really close to the standard knapsack problem. 一旦定义了一种返回invalid结果的方法,它实际上就接近标准背包问题。 In recursion, just pass remaining allowed Weight and current Diversity. 作为递归,只需传递剩余的允许权重和当前分集。 In the recursion anchor case, check Diversity and return invalid if Diversity does not meet requirements, otherwise return the result like in the normal knapsack problem. 在递归锚点情况下,检查分集,如果分集不符合要求,则返回invalid ,否则返回结果,如正常的背包问题。 Check recursion results for invalid and treat it accordingly. 检查递归结果是否invalid并对其进行相应处理。

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

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