解决 0/1 背包的变化(物品的多个来源,每个物品可以从其中一个来源中选择)
因此,对于练习题,我们应该设计一种动态规划算法,它是 0/1 背包问题的变体......基本上每个项目都来自 4 个不同的源,并且该项目只能从其中一个源中获取。也就是说
,
S1={(d_k, b_k) | 1 ≤ k ≤ n},
S2={(d_k, b_k) | n + 1 ≤ k ≤ 2n},
S3={(d_k, b_k) | 2n + 1 ≤ k ≤ 3n},
S4 = {(d_k, b_k) | 3n + 1 ≤ k ≤ 4n}
对于 n = 10
,如果您选择 i = 16
进行放置,则意味着您不会选择 6、26 或 36
...
你能帮我解决这个问题并设计递推方程吗?
So for a practice question, we are supposed to design a Dynamic Programming algorithm which is a variation of 0/1 knapsack problem... Basically each item comes from 4 different source, and that item can be taken from only one of the sources..
Namely,
S1={(d_k, b_k) | 1 ≤ k ≤ n},
S2={(d_k, b_k) | n + 1 ≤ k ≤ 2n},
S3={(d_k, b_k) | 2n + 1 ≤ k ≤ 3n},
S4 = {(d_k, b_k) | 3n + 1 ≤ k ≤ 4n}
for n = 10
, if you select i = 16
to put, that means you won't select 6, 26 or 36
...
Can you help me to solve this problem and devise the recurrence equation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我们有 4n 个元素。
表示法:
V[k]
- 元素 k 的值 (1 <= k <= 4n)W[k]
- 元素的权重element k (1 <= k <= 4n)B
- 边界f(k,B)
- 当边界为 B 且你有 4k 元素。对于第 k 个元素,我们有五种可能的选择:
f(k-1,B)
。为什么?因为我们还有 k-1 个元素,并且边界仍然是 B。V[k] + f(k-1, B - W[k])
。为什么?我们为第 k 个元素赢得了 V[k],并减少了 W[k]。因此,对于其余元素,我们将获得 f(k-1, B - W[k])。V[k+n] + f(k-1, B - W[k+n])
。V[k+2n] + f(k-1, B - W[k+2n])
。V[k+3n] + f(k-1, B - W[k+3n])
。您的目标是最大化 f。因此递推方程为:
剩下的就是求初始条件。
We have 4n elements.
Notation:
V[k]
- value of element k (1 <= k <= 4n)W[k]
- weight of element k (1 <= k <= 4n)B
- The boundf(k,B)
- The value of the optimal solution when the bound is B and you have 4k elements.For the kth element we have five possible choices:
f(k-1,B)
. Why? because we have k-1 more elements and the bound is still B.V[k] + f(k-1, B - W[k])
. Why? We've earned V[k] for the kth element and waisted W[k]. So for the rest of the elements we're going to earn f(k-1, B - W[k]).V[k+n] + f(k-1, B - W[k+n])
.V[k+2n] + f(k-1, B - W[k+2n])
.V[k+3n] + f(k-1, B - W[k+3n])
.Your objective is to maximize f. Hence the recurrence equation is:
What left is to find the initial conditions.
标准 0/1 背包问题:对于每件物品,要么不拿,要么拿。
您的问题:对于每件物品,要么不拿,要么拿从源 1 获取它,或者...,或者从源 4 获取它。
现在看看 0/1 背包问题的常用动态规划算法和递推关系。查看递归关系中RHS的每一位来自哪里;它对应于上面的第一条语句。改为使用上面的第二个语句。
(如果我有点神秘,那是因为这是作业,你应该学习:-)。)
Standard 0/1 knapsack problem: For each item, either you don't take it, or you do.
Your problem: For each item, either you don't take it, or you take it from source 1, or ..., or you take it from source 4.
Now look at the usual dynamic programming algorithm and recurrence relation for the 0/1 knapsack problem. Look at where each bit of the RHS in the recurrence relation comes from; it corresponds to the first statement above. Adapt to use the second statement above instead.
(If I'm being a little cryptic, it's because this is homework and you're meant to be learning :-).)