1,01背包
有 N 件物品和一个容量是 V 的背包。每件物品只能使用一次。求最大价值。
方法1:
#include <bits/stdc++.h>
using namespace std;
const int N = 1010;
/*
f[i][j]:只看前i个物品,总体积是j的情况下,总价值最大是多少
1,不选第i个物品,f[i][j] = f[i - 1][j];
2,选第i个物品,f[i][j] = max(f[i][j], f[i - 1][j - v[i]] + w[i]);
*/
int f[N][N], v[N], w[N];
int main() {
int n, m;
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];
//如果当前的体积小于第i个物品的体积就不能选,所以j >= v[i]
if (j >= v[i])
f[i][j] = max(f[i][j], f[i - 1][j - v[i]] + w[i]);
}
cout << f[n][m] << endl;
return 0;
}
方法2:
#include <bits/stdc++.h>
using namespace std;
const int N = 1010;
int f[N], v[N], w[N];
int main() {
int n, m;
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,完全背包
有 N 件物品和一个容量是 V 的背包。每种物品都有无限件可用。求最大价值。
方法1:
#include <bits/stdc++.h>
using namespace std;
const int N = 1010;
int v[N], w[N], f[N][N];
int main() {
int n, m;
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++) {
//不选第i个
f[i][j] = f[i - 1][j];
//选第i个
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;
}
方法2:
#include <bits/stdc++.h>
using namespace std;
const int N = 1010;
int v[N], w[N], f[N];
int main() {
int n, m;
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 = v[i]; j <= m; j++) {
f[j] = max(f[j], f[j - v[i]] + w[i]);
}
cout << f[m] << endl;
return 0;
}
可以看出在代码方面,一维DP中01背包和完全背包的区别只是,01背包中的j是从大到小遍历,完全背包是从小到大遍历。
3,多重背包
有 N 种物品和一个容量是 V 的背包。第 i 种物品最多有 si 件。求最大价值。
#include <bits/stdc++.h>
using namespace std;
const int N = 110;
int f[N];
int n, m;
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
int v, w, s;
cin >> v >> w >> s;
for (int j = m; j >= 0; j--)
//跟01背包类似,只是加了一个体积限制
for (int k = 1; k <= s && k * v <= j; k++)
f[j] = max(f[j], f[j - k * v] + k * w);
}
cout << f[m] << endl;
return 0;
}
4,多重背包II
相比于多重背包,数据范围变大
#include <bits/stdc++.h>
using namespace std;
const int N = 11010;
int v[N], w[N], f[N];
int n, m;
int main() {
cin >> n >> m;
int cnt = 0;
for (int i = 1; i <= n; i++) {
int a, b, c;
cin >> a >> b >> c;
//将c划分为cnt份,然后转化为01背包
int k = 1;
while (k <= c) {
cnt++;
v[cnt] = k * a;
w[cnt] = k * b;
c -= k;
k *= 2;
}
if (c > 0) {
cnt++;
v[cnt] = c * a;
w[cnt] = c * b;
}
}
n = cnt;
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;
}
5,分组背包
有 N 组物品和一个容量是 V 的背包。每组物品有若干个,同一组内的物品最多只能选一个。求最大价值。
#include <bits/stdc++.h>
using namespace std;
const int N = 110;
int v[N], w[N], f[N];
int main() {
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i++) {
int s;
cin >> s;
for (int j = 0; j < s; j++) cin >> v[j] >> w[j];
for (int j = m; j >= 0; j--)
for (int k = 0; k < s; k++) {
if (j >= v[k])
f[j] = max(f[j], f[j - v[k]] + w[k]);
}
}
cout << f[m] << endl;
return 0;
}