01背包怎么不重复_带有重复物品的背包

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表是

knapsack with duplicate items (1)

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表如下所示:

knapsack with duplicate items (2)

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背包怎么不重复

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/377509.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

C++ 内存基本构件 placement new

用法以及编译器解释 placement new 允许我们将object构建于已经分配的内存上。(所以此时必须有个指针指向已经分配好的内存) 没有所谓的placement delete &#xff0c;因为placement new根本没有分配内存. 也有种说法&#xff0c;是将placement new对应的内存释放掉的操作为pl…

二维数组for遍历

<?php$conarray(array(1,高某,A公司,北京市,010,abc),array(2,罗某,B公司,天津市,020,bcd),array(3,冯某,C公司,上海市,021,cdf),array(4,书某,D公司,重庆市,022,dfg));echo <table border"1" width"600" align"center">;echo <cap…

Xcode调试相关小结

一.设置NSZombieEnabled 使用NSZombieEnabled功能,当代码中访问已经释放了内存的地方,会给你下面这样的提示,而不仅仅是EXEC_BAD_ACCESS: 2008-10-03 18:10:39.933 HelloWorld[1026:20b] *** -[GSFont ascender]: message sent to deallocated instance 0x126550 如果要查看上面…

ONGC的完整形式是什么?

ONGC&#xff1a;石油天然气公司 (ONGC: Oil and Natural Gas Corporation) ONGC is an abbreviation of Oil and Natural Gas Corporation. It is an Indian multinational corporation that is one of the leading producers of crude oil and natural gas in India. Its hea…

node 大写_大写Node.js模块

node 大写Today, lets see a third party module that helps us in working with upper-case letters without necessarily typing them in upper-case in our source code. 今天&#xff0c;让我们看一个第三方模块&#xff0c;它可以帮助我们处理大写字母&#xff0c;而不必在…

HDU嵌入式实验课程大作业分析报告

目录作业要求设计原理与思路扩展任务说明课程感受友情链接工程链接作业要求 体能测试记录仪设计 基于课程发放的实验板&#xff0c;设计一个带有计时和数据采集功能的体能测试记录仪。 基本设计内容 功能1&#xff1a;对应1000米体测场景&#xff0c;使用充电宝供电&#x…

html注释引用公共头部_HTML注释和引用

html注释引用公共头部HTML注释 (HTML Comments) To insert a comment in an HTML document, the comment tags are used. The comments are used to provide some information that could be useful for anyone who views the code of the webpage. The comments can be insert…

HDB3码的编码

编码规则 1、源码是1时&#xff0c;暂时不变&#xff1b; 2、连0不超过3个时不变&#xff0c;有4个或以上连0时把每4个0换为取代节&#xff0c;即B00V&#xff1b; 3、确定B是0还是1&#xff1a;第一个B一般取0&#xff0c;若两个取代节之间1的个数为偶&#xff0c;易推得后者…

批量去除文件空格

import osfilepath r"G:\picture" # 文件目录名 allfilepath os.listdir(filepath)for file in allfilepath: # 改目录下的文件名oldpath filepath \\ filenewname file.replace( , ) # 在原先文件名中去除空格&#xff0c;也就是用null替代空格newpath fil…

【DSP复习主要知识点】(大概)

目录第一章1、数字系统对比模拟系统2、冯诺依曼、哈佛架构3、CISC、RISC4、DSP特点5、cpu流水线作用6、DSP芯片优点第二章&#xff1a;DSP芯片结构原理1、ALU&#xff08;算数逻辑运算单元&#xff09;2、累加器A和B3、桶形移位器的功能4、乘法/加法单元5、CPU状态与控制寄存器…

Json转二值图像

Json文件通过labelme进行标识 image路径 G:\PyCharm\workspace\unet_42-master\datasets\label_bz\test\image label路径 G:\PyCharm\workspace\unet_42-master\datasets\label_bz\test\label 待转换路径 G:\PyCharm\workspace\unet_42-master\datasets\label_bz\test\mask …

矩形波傅里叶变换对以及三角波傅里叶变换

时域矩形波->频域sinc 时域三角波->频域sinc^2:

INTERNET的完整形式是什么?

互联网&#xff1a;互联网络 (INTERNET: Interconnected Network) INTERNET is an abbreviation of Interconnected Network of all the Web Servers Worldwide. It is also known as the World Wide Web or in simple terms the Web. INTERNET是全球所有Web服务器的互连网络的…

DMA三种方式以及DMA特点

博主联系方式&#xff1a; QQ:1540984562 QQ交流群&#xff1a;892023501 群里会有往届的smarters和电赛选手&#xff0c;群里也会不时分享一些有用的资料&#xff0c;有问题可以在群里多问问。 DMA三种方式&#xff1a;数据块传送方式、周期挪用方式、交替访存方式 数据块传送…

界面边框圆角

界面边框圆角的实现方式同样是在res/drawable中定义一个XML文件&#xff0c;corners.xml的代码如下&#xff1a; 1<?xml version"1.0" encoding"utf-8"?>2<shape xmlns:android"http://schemas.android.com/apk/res/android"> 3 …

CGPA的完整形式是什么?

CGPA&#xff1a;累积平均绩点 (CGPA: Cumulative Grade Point Average) CGPA is an abbreviation of Cumulative Grade Point Average. It is a grading system in education. It is used in measuring the overall academic performance average of a student in schools and…

二、页面布局

一、界面 我做的界面大概思路&#xff1a;点击 上传图片&#xff0c;显示待检验的照片 点击 U-net模型检测 会使用已经训练好的U-net模型进行预测 点击 OpenCV统计 会将预测结果进行轮廓检测&#xff0c;并统计所有轮廓的面积 在 处理结果 这个groupBox中进行显示 大致界面如…

GNU工具以及GCC对C语言程序的处理流程

GCC是一族工具的集合&#xff0c;包含预处理器、编译器、汇编器、链接器。 对于默认的文件名&#xff0c;GCC可以自动选择工具自动完成文件处理过程。 从C语言的源程序道可执行文件&#xff0c;实质上是依靠GCC调用一系列工具完成的。 GCC默认处理的文件 文件类型扩展名文件说…

请不要对我说“你要马上把这个小问题修改好”

“我需要”&#xff0c;“小问题”&#xff0c;“立刻”。你激怒我了。你的每个词都激怒我了。这种情绪很难翻译成英语表达&#xff0c;但在荷兰&#xff0c;有些人就是用这种方式要求你。翻译成英语&#xff0c;这有点像“你要几分钟内把这个东西修改好”。大家都知道的这样的…

c#异常处理_C#中的异常处理

c#异常处理What an exception is? 有什么例外&#xff1f; An exception is a runtime error; that means an abnormal situation which is created at run time and the program doesn’t execute successfully. Due to the exceptions, our program gets crash. 异常是运行…