今天重新将动态规划中几种常见的背包问题重新温习了一下,代码重新理解了一下,将背包问题的模板记录在此。
1. 01背包问题
(1)朴素做法:
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 1010;
int n, m; // n表示物品个数,m表示背包的体积
int v[N], w[N]; // v[i]表示每件物品的体积,w[i]表示每件物品的价值
int f[N][N]; //f[N][N]表示物品数不超过N(第一个),总价值不超过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 = 1; j <= m; j++){
f[i][j] = f[i - 1][j];
if (j >= v[i]){
f[i][j] = max(f[i - 1][j], f[i - 1][j - v[i]] + w[i]);
}
}
}
cout << f[n][m] << endl;
return 0;
}
(2)优化版本
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 1010;
int n, m;
int v[N], w[N], f[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 --){
f[j] = max(f[j], f[j - v[i]] + w[i]);
}
}
cout << f[m] << endl;
return 0;
}
2. 完全背包问题
(1)朴素做法
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 1010;
int n, m, v[N], w[N], f[N][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++){
for(int k = 0; k * v[i] <= j; k++){
f[i][j] = max(f[i][j], f[i - 1][j - k * v[i]] + k * w[i]);
}
}
}
cout << f[n][m] << endl;
return 0;
}
(2)优化做法
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 1010;
int n, m, v[N], w[N], f[N][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 = 1; j <= m; j++){
f[i][j] = f[i - 1][j];
if (j >= v[i]){
f[i][j] = max(f[i][j], f[i][j - v[i]] + w[i]);
}
}
}
cout << f[n][m] << endl;
return 0;
}
(3)进一步优化
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 1010;
int n, m, v[N], w[N], f[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 = 1; j <= m; j++){
if (j >= v[i]){
f[j] = max(f[j], f[j - v[i]] + w[i]);
}
}
}
cout << f[m] << endl;
return 0;
}
3. 多重背包问题
(1)朴素做法
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 110;
int n, m, v[N], w[N], s[N], f[N][N];
int main(){
cin >> n >> m;
for(int i = 1; i <= n; i++){
cin >> v[i] >> w[i] >> s[i];
}
for(int i = 1; i <= n; i++){
for(int j = 0; j <= m; j ++){
f[i][j] = f[i - 1][j];
for(int k = 0; k * v[i] <= j && k <= s[i]; k++){
f[i][j] = max(f[i][j], f[i - 1][j - k * v[i]] + k * w[i]);
}
}
}
cout << f[n][m] << endl;
return 0;
}
(2)利用优化01背包的思想优化多重背包
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 110;
int n, m, v[N], w[N], s[N], f[N];
int main(){
cin >> n >> m;
for(int i = 1; i <= n; i++){
cin >> v[i] >> w[i] >> s[i];
}
for(int i = 1; i <= n; i++){
for(int j = m; j >= v[i]; j --){
for(int k = 0; k * v[i] <= j && k <= s[i]; k++){
f[j] = max(f[j], f[j - k * v[i]] + k * w[i]);
}
}
}
cout << f[m] << endl;
return 0;
}
(2)单调队列优化做法
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 11010;
int n, m, v[N], w[N], f[N];
int main(){
int cnt = 0;
cin >> n >> m;
for(int i = 1; i <= n; i++){
int a, b, s;
cin >> a >> b >> s;
int k = 1;
while(k <= s){
cnt ++;
v[cnt] = a * k;
w[cnt] = b * k;
s -= k;
k *= 2;
}
if (s){
cnt ++;
v[cnt] = s * a;
w[cnt] = s * b;
}
}
for(int i = 1; i <= cnt; i++){
for(int j = m; j >= v[i]; j--){
f[j] = max(f[j], f[j - v[i]] + w[i]);
}
}
cout << f[m] << endl;
return 0;
}
3. 分组背包问题
没有朴素的做法,直接利用优化后的01背包的思想来做。
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 110;
int n, m, v[N][N], w[N][N], s[N], f[N];
int main(){
cin >> n >> m;
for(int i = 1; i <= n; i++){
cin >> s[i];
for(int j = 1; j <= s[i]; j++){
cin >> v[i][j] >> w[i][j];
}
}
for(int i = 1; i <= n; i++){
for(int j = m; j >= 0; j --){
for(int k = 1; k <= s[i]; k++){
if (j >= v[i][k]){
f[j] = max(f[j], f[j - v[i][k]] + w[i][k]);
}
}
}
}
cout << f[m] << endl;
return 0;
}