当前位置:网站首页>Dynamic programming 01 knapsack and complete knapsack
Dynamic programming 01 knapsack and complete knapsack
2022-07-03 13:50:00 【yolo_ yyh】
Catalog
One 、01 knapsack
Problem description :
There's a backpack , The total carrying capacity of the backpack is Wkg, Yes n Items ( There is only one thing for each item ), The weight of each item varies , And indivisible . On the premise of not exceeding the weight of the backpack , How to maximize the total weight of items in the backpack ?
So it's called 01 Backpack is because items can be packed ( It can only be loaded with 1 Time ) Or not (0).
Their thinking :
Using the idea of dynamic programming , hypothesis dp[i][j] Before presentation i Items backpack capacity is j The maximum weight that can be loaded under the condition of ,v[i] It means the first one i The value of items ,w[i] It means the first one i The weight of each item , be
dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - w[i]] + v[i]) (i = 0, 1, ..., n; j = 0, 1, ... , W)
among dp[i - 1][j] It's the second i The total value of items not put in the backpack ,dp[i - 1][j - w[i]] + v[i] Is the total value of the current item in the backpack ; The reference code is :
dp[0][0] = 0;
for (int i = 1; i <= n; i++) {
for (int j = W; j >= w[i]; j--) {
dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - w[i]] + v[i]);
}
}
Usually, two-dimensional arrays can also be optimized into one-dimensional arrays , namely dp[j] = max(dp[j], dp[j - w[i]] + v[i])(i = 0, 1, ..., n; j = 0, 1, ..., W)
But at this time, it is required max Function dp[j] Must be The value of the previous state , Then cover with the maximum dp[j], So at this time, the inner circulation should The reverse :
dp[0] = 0;
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]);
}
}
Two 、 Completely backpack
Problem description :
Complete backpack with 01 The difference between backpacks is that each item has more than one , It is Countless pieces .
Their thinking :
For the number of single items , We can adopt the idea of greedy algorithm , The maximum weight limit of the backpack is not exceeded w that will do , So the maximum quantity of each item is W/c[i] The value of rounding down , At this point, the state transition equation becomes :
dp[i][j] = max(dp[i - 1][j - k * w[i]] + k * v[i]) (i = 0, 1, ..., n; j = 0, 1, ..., W; k = 0, 1, 2, ..., W/c[i])
The reference code is :
dp[0][0] = 0;
int maxtemp = 0;
for (int i = 1; i <= n; i++) {
for (int j = w[i]; j <= W; j++) {
for (int k = 1; k <= W / w[i]; k++)
maxtemp = max(maxtemp, dp[i - 1][j - k * w[i]] + v[i]);
dp[i][j] = max(maxtemp, dp[i][j]);
}
}
Convert to a one-dimensional array :dp[j] = max(dp[j], dp[j- w[i]] + v[i]), The code at this time is :
dp[0] = 0;
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]);
}
}
The inner circulation at this time is The order Of , therefore dp[j] It represents the maximum value of the current state , It can be seen from the state transition equation of two-dimensional array , The complete knapsack does not need to be compared with the self value of the previous state , So it's sequential .
边栏推荐
- 记录关于银行回调post请求405 问题
- The shadow of the object at the edge of the untiy world flickers, and the shadow of the object near the far point is normal
- Unity EmbeddedBrowser浏览器插件事件通讯
- Spark practice 1: build spark operation environment in single node local mode
- Unable to stop it, domestic chips have made another breakthrough, and some links have reached 4nm
- [技术发展-24]:现有物联网通信技术特点
- 双向链表(我们只需要关注插入和删除函数)
- Kivy教程之 盒子布局 BoxLayout将子项排列在垂直或水平框中(教程含源码)
- Replace the GPU card number when pytorch loads the historical model, map_ Location settings
- Bidirectional linked list (we only need to pay attention to insert and delete functions)
猜你喜欢
SQL Injection (POST/Search)
Flutter dynamic | fair 2.5.0 new version features
Unable to stop it, domestic chips have made another breakthrough, and some links have reached 4nm
User and group command exercises
Brief analysis of tensorboard visual processing cases
[技術發展-24]:現有物聯網通信技術特點
PhpMyAdmin stage file contains analysis traceability
Mycms we media mall v3.4.1 release, user manual update
Richview trvstyle liststyle list style (bullet number)
[bw16 application] instructions for firmware burning of Anxin Ke bw16 module and development board update
随机推荐
MapReduce implements matrix multiplication - implementation code
Disruptor -- a high concurrency and high performance queue framework for processing tens of millions of levels
Software testing is so hard to find, only outsourcing offers, should I go?
IBEM 数学公式检测数据集
Setting up remote links to MySQL on Linux
The principle of human voice transformer
Unity embeddedbrowser browser plug-in event communication
Which securities company has the lowest Commission for opening an account online? I want to open an account. Is it safe for the online account manager to open an account
json序列化时案例总结
常见的几种最优化方法Matlab原理和深度分析
挡不住了,国产芯片再度突进,部分环节已进到4nm
实现CNN图像的识别和训练通过tensorflow框架对cifar10数据集等方法的处理
Kivy教程之 盒子布局 BoxLayout将子项排列在垂直或水平框中(教程含源码)
Unity render streaming communicates with unity through JS
Ocean CMS vulnerability - search php
Shell timing script, starting from 0, CSV format data is regularly imported into PostgreSQL database shell script example
项目协作的进度如何推进| 社区征文
软件测试工作那么难找,只有外包offer,我该去么?
Go language web development series 29: Gin framework uses gin contrib / sessions library to manage sessions (based on cookies)
Go 1.16.4: manage third-party libraries with Mod