01背包怎么不重复

Problem statement:

问题陈述:

Weights and values are given for n items along with the maximum capacity allowed W. What is the maximum value we can achieve if we can pick any weights, any number of times for the total allowed capacity of W?

给出了n个项目的权重和值以及允许的最大容量W。 如果可以为W的总允许容量选择任意权重,任意次数,我们可以实现的最大值是多少?

Input:

输入:

First line contains two integers N and W denoting the number of items and the total allowed weight.

第一行包含两个整数NW,分别表示项目数和允许的总重量。

In the next two lines are space separated values of the array denoting values of the items val[n] and their weights weights[n] respectively.

在接下来的两行中,数组的空格分隔值分别表示项val [n]的值及其权重weight [n]

Output:

输出:

Output the maximum value which we can achieve if we can pick any weights any number of times for a total weight capacity of W.

如果我们可以选择任意数量的砝码来获得总重量W,则输出可以达到的最大值。

Constraints:

限制条件:

1 <= N,W <= 100
1 <= weight[i], val[i] <= 100

Example:

例:

Test case: 1
Input:
N=2,W= 3
val[]=1 1
weight[]=2 1
Output:
Maximum value that can be achieved: 3
Test case: 2
Input:
N=3,W= 4
val[]=2 5 3
weight[]=1 2 1
Output:
Maximum value that can be achieved: 12

Explanation:

说明:

For the first test case,
Will use the second item with weight 1 only.
We use will 3 such item to have maximum value 3.
Any other combinations will give less value
For the second test case,
Some possible combinations can be
Writing as (value, weight) pair
(2,1), (2,1), (2,1), (2,1)- total weight 4, value 8
(2,1), (2,1), (5,2) - total weight 4, value 9
...
(3,1),(3,1), (3,1),(3,1)- total weight 4, value 12
The last combination is the most optimal one
and that's produces the maximum.

Solution Approach:

解决方法:

The difference between this problem and the general knapsack problem is here we can use the same item several times. So the simple recursion about either choosing an item or leaving the item will not work here as using the same item may lead to the most optimized solution.

这个问题和一般背包问题之间的区别在于,我们可以多次使用同一项目。 因此,关于选择一个项目或离开该项目的简单递归在这里不起作用,因为使用同一项目可能会导致最优化的解决方案。

Hence, the algorithm will be like following,

因此,该算法将如下所示,

  1. Initialize a dp[w+1] to store for each sub-problem up to total capacity W.

    初始化dp [w + 1]为每个子问题存储直到总容量W。

  2. Reset all the values to zero initially.

    最初将所有值重置为零。

    memset(dp,0,sizeof(dp));

    memset(dp,0,sizeof(dp));

  3. Fill up the DP table with base value dp[0]=0,

    用基本值dp [0] = 0填充DP表,

    for sub-problem weight,i=1 to w
    for a weight,wj in the weight array
    if i>=wj
    dp[i] = std∷max⁡(dp[i],dp[i-wj]+valj)
    end if
    end for
    end for
    
    
  4. Maximum value that can be achieved is dp[w]

    可以达到的最大值是dp [w]

Let's compute the above for our test case two,

让我们为测试案例二计算以上内容,

N = 3, W = 4
val[] = 2 5 3
weight[] = 1 2 1

Initially the DP table is,

最初,DP表是

To compute dp[1],

要计算dp [1],

weight[0]<=1
So,
dp[1] = max(dp[1],dp[1-1]+val[0]) = 2
Weight[1]>1. So it can't have any role while computing DP[1]
Weight[2]<=1
So,
dp[1] = max(dp[1],dp[1-1]+val[2]) = 3

Similar way you can hand compute the DP table to understand how the algo is performing is reaching to the optimal answer.

您可以用类似的方法手工计算DP表,以了解算法的性能如何达到最佳答案。

The final DP table for the above test case would look like,

上述测试用例的最终DP表如下所示:

Where 12 is the final case.

最后的情况是12。

C++ Implementation:

C ++实现:

#include <bits/stdc++.h>
using namespace std;
void print(vector<int> a, int n)
{
for (int i = 0; i < n; i++)
cout << a[i] << " ";
cout << endl;
}
int my(vector<int> price, vector<int> weight, int n, int w)
{
int dp[w + 1];
memset(dp, 0, sizeof(dp));
for (int i = 1; i <= w; i++) {
for (int j = 1; j <= n; j++) {
if (i >= weight[j - 1])
dp[i] = std::max(dp[i], dp[i - weight[j - 1]] + price[j - 1]);
}
}
return dp[w];
}
int main()
{
int n, item, w;
cout << "enter number of item and total weights\n";
cin >> n >> w;
cout << "Enter the prices\n";
vector<int> price;
for (int j = 0; j < n; j++) {
scanf("%d", &item);
price.push_back(item);
}
cout << "Enter the weights\n";
vector<int> weight;
for (int j = 0; j < n; j++) {
scanf("%d", &item);
weight.push_back(item);
}
cout << "result is: " << my(price, weight, n, w) << endl;
return 0;
}

Output:

输出:

enter number of item and total weights
3 4
Enter the prices
2 5 3
Enter the weights
1 2 1
result is: 12

翻译自: https://www.includehelp.com/icp/knapsack-with-duplicate-items.aspx

01背包怎么不重复

01背包怎么不重复_带有重复物品的背包相关推荐

  1. python不能创建字典的是_用Python创建带有重复键的字典

    用Python创建带有重复键的字典 我有以下列表,其中包含重复的具有不同值的汽车注册号. 我想将其转换为字典,该字典接受汽车登记号的多个键. 到目前为止,当我尝试将列表转换为字典时,它消除了键之一. ...

  2. python hello world重复_查找数组中重复的数字-python版

    题目一:找出数组中重复的数字 在一个长度为n的数组中,数字在0~n-1范围内,数组中某些数字是重复的,但是不知道重复几个,也不知道重复几次,请找出数组中任意一个重复的数字 解法一:可以先将数据进行排序 ...

  3. java背包算法回溯法_【算法分析】实验 4. 回溯法求解0-1背包等问题

    [TOC] 实验内容 本实验要求基于算法设计与分析的一般过程(即待求解问题的描述.算法设计.算法描述.算法正确性证明.算法分析.算法实现与测试),通过回溯法的在实际问题求解实践中,加深理解其基本原理和 ...

  4. 谷歌书签删除重复_如何删除Google表格中的重复项

    谷歌书签删除重复 Google Sheets lets you remove duplicates from your document with three methods ranging from ...

  5. 复杂的事情简单做,简单的事情重复做,重复的事情用心做!

    人们常说"复杂的事情简单做,简单的事情重复做,重复的事情用心做!",其中真正的内涵是什么呢? 所有事情想完成都是复杂的,复杂的事情简单做,只有将步骤简单化,理出脉络,才能着手去做. ...

  6. 【咕嘎文本对比助手】如何两份手机号或文本对比去重,新旧两批号码如何快速的对比重复,找出重复和不重复的部分,单个文件如何找出重复,单个文件如何找出不重复下面关于五种逻辑做详细解答

    在手机号码整理过程中,群发短信还有大数据分析等整理文件的过程中经常有号码重复 有人说excel有两列数据,怎么用vlookup查找两列数据是否有重复值?在SQL语句中就很好处理了not in 就完事了 ...

  7. Java查找数组重复元素,并打印重复元素、重复次数、重复元素位置

    面试题查找重复元素并打印重复次数和重复位置,一顿懵逼,回来死磕写下来,打印指定重复次数和最大次数,其他在此基础上可以再更新 package sort; import org.testng.annota ...

  8. JAVA取数两个数组交集,考虑重复和不重复元素

    1.考虑不重复元素,重复元素不添加 import java.awt.List; import java.util.ArrayList; import java.util.TreeSet; public ...

  9. 【组合数学】生成函数 ( 正整数拆分 | 重复有序拆分 | 不重复有序拆分 | 重复有序拆分方案数证明 )

    文章目录 一.重复有序拆分 二.不重复有序拆分 1.无序拆分基本模型 2.全排列 三.重复有序拆分方案数证明 参考博客 : 按照顺序看 [组合数学]生成函数 简要介绍 ( 生成函数定义 | 牛顿二项式 ...

最新文章

  1. centos系统盘满了 如何清理_教你5步把系统盘迁移至SSD,让你的电脑加速,瞬间提升10倍!...
  2. python爬虫分析大学排名_Python爬虫获得国内高校排名,python,获取,大学排名
  3. hdu 6406(思路+数据结构)
  4. abiword class list
  5. 对于公司,也是我对软件行业,软件项目的五想法
  6. mysql里面的页面筛选_【mysql】像很多网站(比如电商)里的筛选功能一般是如何实现的?...
  7. 24.Linux-Nand Flash驱动(分析MTD层并制作NAND驱动)
  8. 【muduo】TcpClient与TcpServer建立连接过程对比
  9. xp无法访问win7计算机提示无权限,解决WinXP无法访问Win7文件问题
  10. 广和通率先启动基于联发科技 T830 5G平台的5G模组开发,加速全球运营商5G FWA部署
  11. 积分商城消费系统定制
  12. nltk中文分句_如何使用nltk进行中文分词?
  13. 使用python做迷宫
  14. 从零开始学WEB前端——CSS基础
  15. [计算机网络][内容梳理]一、计算机网络概述
  16. 区块链在物联网中的应用
  17. 【一天一个挨打小技巧】大黄蜂云课堂在听课时候做笔记,无法截图!安排
  18. 比尔·盖茨最新分享:ChatGPT的发展,不止于此
  19. 使用PHP生成不重复的随机数
  20. 用户体验地图——互联网平台建设

热门文章

  1. CSS光标cursor
  2. mysql binlog空间维护
  3. 在Atlas服务器端实现中推荐使用Web Service而不是Page Method
  4. 异步导致UI句柄增加的解决办法
  5. element实现动态路由+面包屑
  6. 线索二叉树的C语言实现
  7. echarts如何显示在页面上
  8. hdu 4857 逃生 拓扑排序
  9. Effective Modern C++翻译(3)-条款2:明白auto类型推导
  10. VC.NET 字节对齐设置