当前位置:网站首页>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 .
边栏推荐
- 编程内功之编程语言众多的原因
- Realize the recognition and training of CNN images, and process the cifar10 data set and other methods through the tensorflow framework
- Flutter动态化 | Fair 2.5.0 新版本特性
- [技术发展-24]:现有物联网通信技术特点
- 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
- Go language unit test 5: go language uses go sqlmock and Gorm to do database query mock
- Start signing up CCF C ³- [email protected] chianxin: Perspective of Russian Ukrainian cyber war - Security confrontation and sanctions g
- SQL Injection (GET/Search)
- Flutter dynamic | fair 2.5.0 new version features
- 双链笔记 RemNote 综合评测:快速输入、PDF 阅读、间隔重复/记忆
猜你喜欢

Go language web development series 29: Gin framework uses gin contrib / sessions library to manage sessions (based on cookies)

NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon

Flutter dynamic | fair 2.5.0 new version features

Richview trvstyle liststyle list style (bullet number)

User and group command exercises

NFT新的契机,多媒体NFT聚合平台OKALEIDO即将上线

GoLand 2021.1.1: configure the multi line display of the tab of the open file

Resolved (error in viewing data information in machine learning) attributeerror: target_ names

HALCON联合C#检测表面缺陷——HALCON例程autobahn

8皇后问题
随机推荐
Flutter dynamic | fair 2.5.0 new version features
MapReduce实现矩阵乘法–实现代码
JS convert pseudo array to array
SVN添加文件时的错误处理:…\conf\svnserve.conf:12: Option expected
[556. Next larger element III]
Disruptor -- a high concurrency and high performance queue framework for processing tens of millions of levels
Libuv库 - 设计概述(中文版)
Kivy教程之 如何自动载入kv文件
The network card fails to start after the cold migration of the server hard disk
Heap structure and heap sort heapify
网上开户哪家证券公司佣金最低,我要开户,网上客户经理开户安全吗
windos 创建cordova 提示 因为在此系统上禁止运行脚本
静态链表(数组的下标代替指针)
ThreadPoolExecutor realizes multi-threaded concurrency and obtains the return value (elegant and concise way)
Shell timing script, starting from 0, CSV format data is regularly imported into PostgreSQL database shell script example
Students who do not understand the code can also send their own token, which is easy to learn BSC
Go language web development series 30: gin: grouping by version for routing
Spark practice 1: build spark operation environment in single node local mode
记录关于银行回调post请求405 问题
Depth and breadth first traversal of tree (regardless of binary tree)