当前位置:网站首页>Detailed explanation of knapsack problem
Detailed explanation of knapsack problem
2022-07-23 14:09:00 【qq_ twenty-five million four hundred and twenty-seven thousand 】

Focus on mastering 01 Backpack and full backpack
01 Backpack details :
https://blog.csdn.net/u013445530/article/details/40210587
dp[i][j]=max(dp[i-1][j], dp[i-1][j - w[i]] + v[i]);
explain :1) Put in the i An object , be dp[i][j]=dp[i-1][j - w[i]] + v[i];
2) Don't put it in the second i An object :dp[i][j]=dp[i-1][j];
Two dimensional array
for(int i = 1; i <= n; i++)
{
for(int j = 0; j <= W; j++)
{
if(j < w[i]) dp[i][j] = dp[i-1][j];
else dp[i][j] = max(dp[i-1][j], dp[i-1][j - w[i]] + v[i]);
}
}
To a one-dimensional array ,dp[i][j] As dp[j].
dp[i][j] By dp[i-1][j] and dp[i-1][j-w[i]] Derived from , be dp[j] from dp[j] and dp[j - w[i]] Derived from .
for(int i = 1; i <= n; i++)
{
for(int j = W; j >= w[i]; j--)
dp[j] = max(dp[j], dp[j - w[i]] + v[i]);
}
The following figure helps to understand why it is necessary to reverse the order when changing to one dimension :
Traverse horizontally , To make sure dp[j - w[i]] What is kept is dp[i-1][j - w[i]] Value , First i=1 when , Traverse from right to left

Here is traversal in reverse order , If you don't traverse back in reverse order, it will cause an item to be selected repeatedly .
Why repeat selection ? The maximum value of the back is from the value of the front , The first value is selected i Item , The following capacity can still be selected i Item , Lead to i Items selected repeatedly .
``
However, the complete knapsack problem happens to be an item n Pieces of , It just needs to be selected repeatedly , So the ergodic order is positive ergodic , as follows :
for(int i = 1; i <= n; i++)
{
for(int j = w[i]; j <= W; j++)
dp[j] = max(dp[j], dp[j - w[i]] + v[i]);
}
边栏推荐
- Notes on the sixth day
- 容器网络原理
- C #: in, out, ref keywords
- How hot is July? Crawl through the crawler to get the temperature information of the current month, and use Matplotlib to draw the temperature line chart
- MYSQL练习题:向CEO汇报的所有员工
- MGRE环境下的OSPF实验:
- Static comprehensive experiment (HCIA)
- Day 12 notes
- 记一次Vulnhub靶机练习成功拿下root权限
- ERP生产作业控制
猜你喜欢

Ansible first knowledge of learning one

背包问题详解

第五天笔记

记一次Vulnhub靶机练习成功拿下root权限

锐龙R7 PRO 5875U性能怎么样?相当于什么水平级别

kafka消费报错coordinator unavailable.Rediscovery will be attempt redisCovery

Creo 9.0 中几何对象的选取方法
![[laser principle and application -7]: semiconductor refrigeration sheet and Tec thermostat](/img/c8/e750ff7c64e05242eac7b53b84dbae.png)
[laser principle and application -7]: semiconductor refrigeration sheet and Tec thermostat

赛扬n5095处理器怎么样 英特尔n5095核显相当于什么水平

Day 12 notes
随机推荐
数千个数据库、遍布全国的物理机,京东物流全量上云实录 | 卓越技术团队访谈录
APP常用跨端技术栈深入分析
锐龙R7 PRO 5875U性能怎么样?相当于什么水平级别
Is it risky and safe to open an account?
第十一天笔记
Google Earth Engine——gee中的小bug跨洲边际的影像无法获取
Principle and implementation of Tencent mmkv
判断一个对象是否是空对象的处理办法
How can Creo 9.0 quickly modify CAD coordinate system?
OSPF experiment in mGRE environment:
BGP联邦实验
[机缘参悟-50]:鬼谷子-第十二符言篇-当好领导者的艺术:守其位,观四方,洞危险,广言路,虚谏言,定规则,明赏罚,符名实,得民心。
What level of rtx3070ti graphics card? What level of rtx3070ti graphics card? How about rtx3070ti graphics card
过程块和方法
第五天笔记
子组件向父组件传参的几种方法
200 lines of code, in-depth analysis of the principle and implementation of dynamic calculation diagram
第十天笔记
Which is the difference between iqoo 10 pro and vivo X80 pro? Detailed parameter configuration comparison
[laser principle and application -7]: semiconductor refrigeration sheet and Tec thermostat