当前位置:网站首页>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 .
边栏推荐
- Unity render streaming communicates with unity through JS
- Error running 'application' in idea running: the solution of command line is too long
- Spark practice 1: build spark operation environment in single node local mode
- json序列化时案例总结
- 挡不住了,国产芯片再度突进,部分环节已进到4nm
- Open PHP error prompt under Ubuntu 14.04
- Golang - command line tool Cobra
- Go language web development series 28: solve cross domain access of CORS with gin contrib / CORS
- Mysql:insert date:SQL 错误 [1292] [22001]: Data truncation: Incorrect date value:
- Unity embeddedbrowser browser plug-in event communication
猜你喜欢
Complete deep neural network CNN training with tensorflow to complete picture recognition case 2
Complete DNN deep neural network CNN training with tensorflow to complete image recognition cases
AI scores 81 in high scores. Netizens: AI model can't avoid "internal examination"!
TensorBoard可视化处理案例简析
[技術發展-24]:現有物聯網通信技術特點
Go language web development series 26: Gin framework: demonstrates the execution sequence of code when there are multiple middleware
Kivy教程之 如何自动载入kv文件
Ocean CMS vulnerability - search php
全面发展数字经济主航道 和数集团积极推动UTONMOS数藏市场
如何使用lxml判断网站公告是否更新
随机推荐
JS convert pseudo array to array
Mastering the cypress command line options is the basis for truly mastering cypress
MyCms 自媒体商城 v3.4.1 发布,使用手册更新
SQL Injection (GET/Search)
Golang - command line tool Cobra
Go 1.16.4: manage third-party libraries with Mod
记录关于银行回调post请求405 问题
Multi table query of MySQL - multi table relationship and related exercises
Golang — template
Ubuntu 14.04 下开启PHP错误提示
Logback log sorting
Spark实战1:单节点本地模式搭建Spark运行环境
【电脑插入U盘或者内存卡显示无法格式化FAT32如何解决】
Mobile phones and computers can be used, whole people, spoof code connections, "won't you Baidu for a while" teach you to use Baidu
如何使用lxml判断网站公告是否更新
The shortage of graphics cards finally came to an end: 3070ti for more than 4000 yuan, 2000 yuan cheaper than the original price, and 3090ti
TensorBoard可视化处理案例简析
When updating mysql, the condition is a query
Libuv库 - 设计概述(中文版)
The solution of Chinese font garbled code in keil5