01 背包和完全背包是重点,分组背包、二维费用是 01 背包的扩展,多重背包是受限制的完全背包
01 背包
解题思路
代码
原始做法
#include <iostream>
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 = 1; j <= m; j++) {
f[i][j] = f[i - 1][j];
if (j >= v[i]) f[i][j] = max(f[i - 1][j - v[i]] + w[i], f[i][j]);
}
}
cout << f[n][m] << endl;
return 0;
}
优化空间
#include <iostream>
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 = m; j >= v[i]; j--) f[j] = max(f[j - v[i]] + w[i], f[j]);
}
cout << f[m] << endl;
return 0;
}
完全背包
解题思路
代码
原始做法 O(N^3)
#include <iostream>
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 = 1; j <= m; j++) {
for (int k = 0; j >= k * v[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;
}
优化时间 O(N^2)
#include <iostream>
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 = 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][j - v[i]] + w[i]);
}
}
cout << f[n][m] << endl;
return 0;
}
优化空间
#include <iostream>
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 - v[i]] + w[i], f[j]);
}
}
cout << f[m] << endl;
return 0;
}
多重背包
解题思路
朴素的多重背包,在朴素的完全背包的基础,加一个次数限制,即可
朴素做法:一个一个拆 O ( N ) O(N) O(N)
二进制优化:类似于快速幂的拆法 O ( l o g N ) O(logN) O(logN),拆成多组
比如说:a 物品有 13 个
朴素做法就是一个一个的试,最终试到 13 个
二进制优化,则是拆成 5 组,{1, 2, 4, 8, 1} 尝试。因为由 {1, 2, 4, 8, 1} 这个集合里面的子集能凑 0 ~ 13 的任意一个数
代码
原始做法 O(N^3)
#include <iostream>
using namespace std;
const int N = 110;
int v[N], w[N], s[N], f[N][N];
int main() {
int n, m;
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 = 1; j <= m; j++) {
for (int k = 0; j >= k * v[i] && 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;
}
二进制优化 O(N^2logN)
#include <iostream>
using namespace std;
const int N = 10000;
int v[N], w[N], f[N][N];
int main() {
int n, m;
cin >> n >> m;
int cnt = 1;
for (int i = 1; i <= n; i++) {
int a, b, s;
cin >> a >> b >> s;
for (int k = 1; k <= s; s -= k, k <<= 1) {
v[cnt] = a * k, w[cnt] = b * k;
cnt++;
}
if (s) {
v[cnt] = a * s, w[cnt] = b * s;
cnt++;
}
}
n = cnt;
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 - 1][j - v[i]] + w[i]);
}
}
cout << f[n][m] << endl;
return 0;
}
在二进制优化上再优化空间
#include <iostream>
using namespace std;
const int N = 10000;
int v[N], w[N], f[N];
int main() {
int n, m;
cin >> n >> m;
int cnt = 1;
for (int i = 1; i <= n; i++) {
int a, b, s;
cin >> a >> b >> s;
for (int k = 1; k <= s; s -= k, k <<= 1) {
v[cnt] = a * k, w[cnt] = b * k;
cnt++;
}
if (s) {
v[cnt] = a * s, w[cnt] = b * s;
cnt++;
}
}
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;
}
分组背包
解题思路
分组背包就是扩展了的 01 背包,状态转移与 01 一致。只是拓展了分组内物品的枚举
代码
原始做法
#include <iostream>
using namespace std;
const int N = 110;
int v[N][N], w[N][N], s[N], f[N][N];
int main() {
int n, m;
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 = 1; j <= m; j++) {
f[i][j] = f[i - 1][j];
for (int k = 1; k <= s[i]; k++) {
if (j >= v[i][k]) f[i][j] = max(f[i][j], f[i - 1][j - v[i][k]] + w[i][k]);
}
}
}
cout << f[n][m] << endl;
return 0;
}
优化空间
#include <iostream>
using namespace std;
const int N = 110;
int v[N][N], w[N][N], s[N], f[N];
int main() {
int n, m;
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;
}
混合背包
解题思路
01 背包与分组背包和完全背包混合,这三者的状态表示是相同的,根据判断背包的类型,分别做状态转移即可
代码
原始做法 O(N^3)
#include <iostream>
using namespace std;
const int N = 1010;
int f[N][N];
int main() {
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i++) {
int v, w, s;
cin >> v >> w >> s;
if (s == -1) { // 01 背包
for (int j = 1; j <= m; j++) {
f[i][j] = f[i - 1][j];
if (j >= v) f[i][j] = max(f[i][j], f[i - 1][j - v] + w);
}
} else if (s == 0) { // 完全背包
for (int j = 1; j <= m; j++) {
f[i][j] = f[i - 1][j];
if (j >= v) f[i][j] = max(f[i - 1][j], f[i][j - v] + w);
}
} else { // 多重背包
for (int j = 1; j <= m; j++) {
for (int k = 0; j >= k * v && k <= s; k++) {
f[i][j] = max(f[i][j], f[i - 1][j - k * v] + k * w);
}
}
}
}
cout << f[n][m] << endl;
return 0;
}
优化 O(N^2logN)
#include <iostream>
using namespace std;
const int N = 1010;
int f[N];
int main() {
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i++) {
int v, w, s;
cin >> v >> w >> s;
if (s == -1) { // 01 背包
for (int j = m; j >= v; j--) {
if (j >= v) f[j] = max(f[j], f[j - v] + w);
}
} else if (s == 0) { // 完全背包
for (int j = v; j <= m; j++) {
f[j] = max(f[j], f[j - v] + w);
}
} else { // 多重背包
for (int k = 1; k <= s; s -= k, k <<= 1) {
for (int j = m; j >= k * v; j--) f[j] = max(f[j], f[j - k * v] + k * w);
}
if (s) {
for (int j = m; j >= s * v; j--) f[j] = max(f[j], f[j - s * v] + s * w);
}
}
}
cout << f[m] << endl;
return 0;
}
二维费用背包
解题思路
代码
原始做法
#include <iostream>
using namespace std;
const int N = 1010, M = 110;
int v[N], m[N], w[N];
int f[N][M][M];
int main() {
int n, a, b;
cin >> n >> a >> b;
for (int i = 1; i <= n; i++) cin >> v[i] >> m[i] >> w[i];
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= a; j++) {
for (int k = 1; k <= b; k++) {
f[i][j][k] = f[i - 1][j][k];
if (j >= v[i] && k >= m[i])
f[i][j][k] = max(f[i][j][k], f[i - 1][j - v[i]][k - m[i]] + w[i]);
}
}
}
cout << f[n][a][b] << endl;
return 0;
}
优化空间
#include <iostream>
using namespace std;
const int N = 1010, M = 110;
int v[N], m[N], w[N];
int f[M][M];
int main() {
int n, a, b;
cin >> n >> a >> b;
for (int i = 1; i <= n; i++) cin >> v[i] >> m[i] >> w[i];
for (int i = 1; i <= n; i++) {
for (int j = a; j >= v[i]; j--) {
for (int k = b; k >= m[i]; k--) {
f[j][k] = max(f[j][k], f[j - v[i]][k - m[i]] + w[i]);
}
}
}
cout << f[a][b] << endl;
return 0;
}