当前位置:网站首页>【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;
}
边栏推荐
- Leetcode Hamming distance simple
- 12 error end event and terminateendevent of end event
- 零拷贝技术
- Minimum spanning tree (prim+kruskal) learning notes (template +oj topic)
- Concurrent programming -- countdownlatch combined with thread pool
- Introduction to USB learning (I) -- Dongfeng night flower tree
- Leetcode- string addition - simple
- Mobile end adaptation scheme
- FusionPBX 安装 —— 筑梦之路
- Audio stereo to mono (Audio Dual Channel to mono channel)
猜你喜欢
The Boys x PUBGMOBILE 联动火热来袭!来看最新游戏海报
[spark]spark introductory practical series_ 8_ Spark_ Mllib (upper)__ Introduction to machine learning and sparkmllib
Test logiciel - résumé des FAQ d'interface
智能数字资产管理助力企业决胜后疫情时代
arrayList && linkedList
中断处理过程
php 分布式事务 原理详解
Annotation only integration SSM framework
1 Introduction to drools rule engine (usage scenarios and advantages)
Concurrent programming -- source code analysis of thread pool
随机推荐
Lamda expression
Service fusing and degradation of Note Series
Config server configuration center of Nacos series
Mongodb Multi - field Aggregation group by
零拷贝技术
Tongweb card, tongweb card, tongweb card
[spark]spark introductory practical series_ 8_ Spark_ Mllib (lower)__ Machine learning library sparkmllib practice
2021-11-04 implementation of binary search
Tongweb adapts to openrasp
Leetcode- find a difference - simple
Leetcode- divide candy - simple
Leetcode- reverse string - simple
Vagrant virtual machine installation, disk expansion and LAN access tutorial
2021.9.29 learning log restful architecture
2 first experience of drools
11 signalthrowingevent and signalboundaryevent of flowable signal event
Working principle of sentinel series (concept)
Self summarizing
Test logiciel - résumé des FAQ d'interface
Basic application of sentinel series