当前位置:网站首页>【DP之01背包】
【DP之01背包】
2022-06-13 06:03:00 【mango660】
01 背包
为什么叫 01 背包?
因为每一个物品要么用一次,要么不用。
状态转移方程:
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])优化之后状态转义方程:
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])分析结果:
f[N][v]
详细分析
该题目可以看成是一个有限集合求极值的题目。
emm, 看我的乱糟糟手写笔记吧。
相关题目
有 NN 件物品和一个容量是 VV 的背包。每件物品只能使用一次。
第 ii 件物品的体积是 vivi,价值是 wiwi。
求解将哪些物品装入背包,可使这些物品的总体积不超过背包容量,且总价值最大。
输出最大价值。
输入格式
第一行两个整数,N,VN,V,用空格隔开,分别表示物品数量和背包容积。
接下来有 NN 行,每行两个整数 vi,wivi,wi,用空格隔开,分别表示第 ii 件物品的体积和价值。
输出格式
输出一个整数,表示最大价值。
数据范围
0<N,V≤10000<N,V≤1000
0<vi,wi≤10000<vi,wi≤1000
输入样例
4 5
1 2
2 4
3 4
4 5
输出样例:
8
题解代码
- 暴力版本
#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;
}
- 优化版本
#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 --) {
// 不包含第 i 个物品
f[j] = f[j];
if (j >= v[i]) {
// 包含第 i 个物品
f[j] = max(f[j], f[j - v[i]] + w[i]);
}
}
}
cout << f[m] << endl;
return 0;
}
边栏推荐
- Nacos series registry principle and source code analysis
- Leetcode- complement of numbers - simple
- 17 servicetask of flowable task
- Mobile end adaptation scheme
- 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
- Security baseline check script - the road to dream
- Working principle of sentinel series (source code analysis)
- Find out the missing numbers from the natural numbers arranged in order from 0 to 100, and the solution provides
- Leetcode- reverse vowels in string - simple
- Ffmpeg download suffix is Video files for m3u8
猜你喜欢
ffmpeg 下载后缀为.m3u8的视频文件
You still can't remotely debug idea? Come and have a look at my article. It's easy to use
2021.9.30 learning log -postman
软件测试——接口常见问题汇总
SPI primary key generation strategy for shardingsphere JDBC
Validation set: ‘flowable-executable-process‘ | Problem: ‘flowable-servicetask-missing-implementatio
How slow is the application system on tongweb? How dead is it?
Working principle of sentinel series (concept)
Mongodb Multi - field Aggregation group by
OpenGL马赛克(八)
随机推荐
Swift--function
MySQL stored procedure
Leetcode- keyboard line - simple
Introduction to USB learning (I) -- Dongfeng night flower tree
Audio stereo to mono (Audio Dual Channel to mono channel)
What happens when the MySQL union index ABC encounters a "comparison operator"?
ArrayList loop removes the pit encountered
@Detailed explanation of propertysource usage method and operation principle mechanism
Function and application scenario of field setaccessible() method
JNDI configuration for tongweb7
13 cancelendevent of a flowable end event and compensationthrowing of a compensation event
2021.9.30 learning log -postman
Concurrent programming -- what is threading?
为什么那么多人讨厌A-Spice
零拷贝技术
移动端适配方案
FusionPBX 安装 —— 筑梦之路
The problem of distinguishing and sharing sessions for multiple applications in tongweb
The reason why the process cannot be shut down after a spark job is executed and the solution
= = relation between int and integer