当前位置:网站首页>[DP 01 backpack]
[DP 01 backpack]
2022-06-13 06:08:00 【mango660】
01 knapsack
Why call 01 knapsack ?
Because each item can only be used once , Or don't have to .
State transition equation :
f [ i ] [ j ] = M a x ( f [ i − 1 ] [ j ] , f [ i − 1 ] [ j − v [ i ] ] + w [ i ] ) f[i][j] = Max(f[i-1][j], f[i - 1][j - v[i]] + w[i]) f[i][j]=Max(f[i−1][j],f[i−1][j−v[i]]+w[i])State escape equation after optimization :
f [ j ] = M a x ( f [ j ] , f [ j − v [ i ] ] + w [ i ] ) f[j] = Max(f[j], f[j - v[i]] + w[i]) f[j]=Max(f[j],f[j−v[i]]+w[i])The results of the analysis :
f[N][v]
Detailed analysis
This problem can be regarded as the problem of finding the extreme value of a finite set .
emm, Look at my messy handwritten notes .
Related topics
Yes NN Items and a capacity are VV The backpack . Each item can only be used once .
The first ii The volume of the item is vivi, The value is wiwi.
Find out which items to pack in your backpack , The total volume of these items can not exceed the capacity of the backpack , And the total value is the greatest .
Output maximum value .
Input format
The first line has two integers ,N,VN,V, Space off , They are the number of items and the volume of the backpack .
Next there is NN That's ok , Two integers per line vi,wivi,wi, Space off , Separate indication control ii The volume and value of items .
Output format
Output an integer , Represents the greatest value .
Data range
0<N,V≤10000<N,V≤1000
0<vi,wi≤10000<vi,wi≤1000
sample input
4 5
1 2
2 4
3 4
4 5
sample output :
8
Solution code
- Violent version
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 1010;
int n, m;
int f[N][N];
int v[N], w[N];
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i ++) {
cin >> v[i] >> w[i];
}
for (int i = 1; i <= n; i ++) {
for (int j = 0; j <= m; j ++) {
f[i][j] = f[i - 1][j];
// printf("i = %d, j = %d, f[%d][%d] = %d, f[%d][%d] = %d\n", i, j, i, j, f[i][j], i - 1, j, f[i - 1][j]);
if (j >= v[i]) {
f[i][j] = max(f[i][j], f[i - 1][j - v[i]] + w[i]);
}
}
}
int res = 0;
for (int i = 0; i <= m; i ++) {
res = max(res, f[n][i]);
}
cout << res << endl;
return 0;
}
- Optimized version
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 1010;
int n, m;
// int f[N][N];
int f[N];
int v[N], w[N];
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i ++) {
cin >> v[i] >> w[i];
}
for (int i = 1; i <= n; i ++) {
for (int j = m; j >= v[i]; j --) {
// It doesn't include i Items
f[j] = f[j];
if (j >= v[i]) {
// Including i Items
f[j] = max(f[j], f[j - v[i]] + w[i]);
}
}
}
cout << f[m] << endl;
return 0;
}
边栏推荐
- Shardingsphere JDBC exception: no table route info
- Leetcode- complement of numbers - simple
- 万能播放器 PotPlayer 的下载与安装,直播流 m3u8 导入
- You still can't remotely debug idea? Come and have a look at my article. It's easy to use
- 1016 part a+b (15 points)
- A brief analysis of the overall process of view drawing
- Missing tag identification in cots RFID systems: bringing the gap between theory and Practice
- Introduction to USB learning (I) -- Dongfeng night flower tree
- USB debugging assistant
- Echart柱状图:堆叠柱状图value格式化显示
猜你喜欢
Echart柱状图:x轴显示value,y轴显示类别
软件测试——接口常见问题汇总
What happens when the MySQL union index ABC encounters a "comparison operator"?
Software testing - Summary of common interface problems
Echart柱状图:echart实现堆叠柱状图
Shardingsphere JDBC exception: no table route info
[spark]spark introductory practical series_ 8_ Spark_ Mllib (upper)__ Introduction to machine learning and sparkmllib
Rk3399 hid gadget configuration
本地文件秒搜工具 Everything
Pod libwebp error reporting solution
随机推荐
Leetcode Timo attack - simple
2021-11-04 implementation of binary search
Echart histogram: X-axis displays value, Y-axis displays category
FusionPBX 安装 —— 筑梦之路
Leetcode guessing numbers game - simple
Leetcode- third largest number - simple
Echart柱状图:echart实现堆叠柱状图
Leetcode- first unique character in string - simple
二分查找
Annotation only integration SSM framework
MySQL stored procedure
Echart矩形树图:简单实现矩形树图
[turn] explain awk (2)_ Combining formatted output with built-in variables to realize requirements
Leetcode fizz buzz simple
After MySQL is installed, enter the "net start MySQL" command, and an error is reported that "net" is neither an internal or external command nor a runnable program
Leetcode- maximum average of subarray i- simple
Leetcode- intersection of two arrays - simple
Uni app disable native navigation bar
【美团笔试题】
万能播放器 PotPlayer 的下载与安装,直播流 m3u8 导入