当前位置:网站首页>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 .
边栏推荐
- [quantitative trading] permanent portfolio, turtle trading rules reading, back testing and discussion
- Flutter动态化 | Fair 2.5.0 新版本特性
- Spark practice 1: build spark operation environment in single node local mode
- 如何使用lxml判断网站公告是否更新
- 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
- RichView TRVStyle ListStyle 列表样式(项目符号编号)
- Setting up remote links to MySQL on Linux
- [redis] cache warm-up, cache avalanche and cache breakdown
- Leetcode-1175.Prime Arrangements
- JS 将伪数组转换成数组
猜你喜欢
PhpMyAdmin stage file contains analysis traceability
The latest BSC can pay dividends. Any B usdt Shib eth dividend destruction marketing can
KEIL5出现中文字体乱码的解决方法
Comprehensively develop the main channel of digital economy and digital group, and actively promote the utonmos digital Tibet market
Ocean CMS vulnerability - search php
Another industry has been broken by Chinese chips. No wonder the leading analog chip companies in the United States have cut prices and sold off
[quantitative trading] permanent portfolio, turtle trading rules reading, back testing and discussion
刚毕业的欧洲大学生,就能拿到美国互联网大厂 Offer?
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
Logback log sorting
随机推荐
Red hat satellite 6: better management of servers and clouds
8 Queen question
This math book, which has been written by senior ml researchers for 7 years, is available in free electronic version
MapReduce implements matrix multiplication - implementation code
Another industry has been broken by Chinese chips. No wonder the leading analog chip companies in the United States have cut prices and sold off
树的深入和广度优先遍历(不考虑二叉树)
SQL Injection (POST/Search)
Field problems in MySQL
Flutter动态化 | Fair 2.5.0 新版本特性
AI 考高数得分 81,网友:AI 模型也免不了“内卷”!
Go language web development series 26: Gin framework: demonstrates the execution sequence of code when there are multiple middleware
栈应用(平衡符)
Kivy教程之 盒子布局 BoxLayout将子项排列在垂直或水平框中(教程含源码)
Go language web development series 28: solve cross domain access of CORS with gin contrib / CORS
Asp. Net core1.1 without project JSON, so as to generate cross platform packages
SQL Injection (POST/Select)
Unity EmbeddedBrowser浏览器插件事件通讯
NFT新的契机,多媒体NFT聚合平台OKALEIDO即将上线
[how to earn a million passive income]
User and group command exercises