该问题对应于哪种背包问题变体?

发布于 2025-01-15 11:41:20 字数 654 浏览 4 评论 0原文

让我们想象一下,我必须在约束下装满我的背包:

  • 每个物品都有一个相关的重量 wi 和利润 pi
  • 最大总重量 Wmax

知道:

  • 物品有类别,我必须选择一个物品 从每个类别
  • 当然,目标是选择项目来最大化利润总和

示例:Wmax=400

书籍书籍重量书籍利润食品食品重量食品利润
圣经50025奶酪80120
小王子1505香蕉250200

这里,最好的解决方案是(小王子,香蕉)

我有一个类似的问题,我想找出最好的编码方式,但我无法弄清楚问题的版本/变体是什么,它是已知的变体吗?

Let us imagine that I have to fill my knapsack with items under constraints:

  • Each item has an associated weight wi and profit pi
  • With a maximum total weight Wmax

Knowing that:

  • There are categories of items and I have to choose exactly one item from each category
  • Of course, the aim is to choose items to maximise the sum of the profits

Example : Wmax=400

BooksBooks weightsBooks profitsFoodFood weightsFood profits
The Bible50025Cheese80120
The little prince1505Banana250200

Here, the best solution is (The little prince, Banana)

I have a similar problem and I'd like to find out the best way to code it but I can't figure out what version/ variation of the probleme this is, is it a known variation ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不可一世的女人 2025-01-22 11:41:20

我不确定是否存在与您的版本相匹配的现有变体,但很容易从经典变体中得出推论并解决这个问题。

经典变体具有 2D 动态规划(DP[N][W],其中 NW 是物品数量和最大重量)。

在此变体中,由于我们只能选择每个类别中的一个,因此您可以使用 3D DP,例如 dp[i][w][j],它表示从第一个 < 中可以获得的最大值重量为 wj 的 code>i 项是 0/1 int,表示类别号 j 中的项是否已被选择与否。

我将保留实现,因为递归关系相对简单并且与经典变体非常相似。

I’m not sure if there’s an existing variation that matches yours, but it’s easy to draw inference from the classical variant and solve this.

Classic variant has 2D dynamic programming (DP[N][W], where N and W are number of items and max weight).

In this variant, since we can only pick one of each category, you can use 3D DP like dp[i][w][j], which denotes the maximum value you can get from the first i items with weight w and j is a 0/1 int denoting whether an item from category number j has been selected or not.

I’ll leave the implementation, since the recursive relation is relatively simple and quite similar to the classic variant.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文