# 刷透背包（01 背包，完全背包，多重背包，分组背包，混合背包，二维费用背包）

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^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;
}


# 分组背包

## 代码

### 原始做法

#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;
}



11-07
04-12 3556
07-31 43
04-20 1335
11-30 59
12-05 105
08-03 76
08-18 693
08-16 719
09-02