• 欢迎光临~

# 背包问题-极大极小值-空间恰好是j

### 一、01背包

#### 1、最小值

4 5
1 2
2 4
3 4
4 5

7

``````#include <bits/stdc++.h>

using namespace std;
const int N = 110;
const int INF = 0x3f3f3f3f;

int n, m;
int f[N][N];

int main() {
cin >> n >> m;
memset(f, INF, sizeof f);
f[0][0] = 0;

for (int i = 1; i <= n; i++) {
int v, w;
cin >> v >> w;
for (int j = 0; j <= m; j++) {
f[i][j] = f[i - 1][j];
if (j >= v) f[i][j] = min(f[i][j], f[i - 1][j - v] + w);
}
}
cout << f[n][m] << endl;
return 0;
}
``````

``````#include <bits/stdc++.h>

using namespace std;
const int N = 110;
const int INF = 0x3f3f3f3f;

int n, m;
int f[N];

int main() {
cin >> n >> m;
memset(f, INF, sizeof f);
f[0] = 0;

for (int i = 1; i <= n; i++) {
int v, w;
cin >> v >> w;
for (int j = m; j >= v; j--)
f[j] = min(f[j], f[j - v] + w);
}
cout << f[m] << endl;
return 0;
}

``````

#### 2、最大值

4 5
1 2
2 4
3 4
4 5

8

``````#include <bits/stdc++.h>

using namespace std;
const int N = 110;
const int INF = 0x3f3f3f3f;

int n, m;
int f[N][N];

int main() {
cin >> n >> m;

memset(f, -INF, sizeof f);
f[0][0] = 0;

for (int i = 1; i <= n; i++) {
int v, w;
cin >> v >> w;
for (int j = 0; 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);
}
}
cout << f[n][m] << endl;
return 0;
}
``````

``````#include <bits/stdc++.h>

using namespace std;
const int N = 110;
const int INF = 0x3f3f3f3f;

int n, m;
int f[N];

int main() {
cin >> n >> m;

memset(f, -INF, sizeof f);
f[0] = 0;

for (int i = 1; i <= n; i++) {
int v, w;
cin >> v >> w;
for (int j = m; j >= v; j--) {
f[j] = max(f[j], f[j - v] + w);
}
}
cout << f[m] << endl;
return 0;
}
``````

### 二、完全背包

#### 1、最小值

4 5
1 2
2 4
3 4
4 5

7

``````#include <bits/stdc++.h>

using namespace std;
const int N = 110;
const int INF = 0x3f3f3f3f;

int n, m;
int f[N][N];

int main() {
cin >> n >> m;

memset(f, INF, sizeof f);
f[0][0] = 0;

for (int i = 1; i <= n; i++) {
int v, w;
cin >> v >> w;
for (int j = 0; j <= m; j++) {
f[i][j] = f[i - 1][j];
if (j >= v) f[i][j] = min(f[i][j], f[i][j - v] + w);
}
}
cout << f[n][m] << endl;
return 0;
}
``````

``````#include <bits/stdc++.h>

using namespace std;
const int N = 110, INF = 0x3f3f3f3f;

int n, m;
int f[N];

int main() {
cin >> n >> m;

memset(f, INF, sizeof f);
f[0] = 0;

for (int i = 1; i <= n; i++) {
int v, w;
cin >> v >> w;
for (int j = v; j <= m; j++) {
f[j] = min(f[j], f[j - v] + w);
}
}
cout << f[m] << endl;
return 0;
}
``````

#### 2、最大值

4 5
1 2
2 4
3 4
4 5

10

``````#include <bits/stdc++.h>

using namespace std;
const int N = 110;
const int INF = 0x3f3f3f3f;

int n, m;
int f[N][N];

int main() {
cin >> n >> m;

memset(f, -INF, sizeof f);
f[0][0] = 0;

for (int i = 1; i <= n; i++) {
int v, w;
cin >> v >> w;
for (int j = 0; j <= m; j++) {
f[i][j] = f[i - 1][j];
if (j >= v) f[i][j] = max(f[i][j], f[i][j - v] + w);
}
}
cout << f[n][m] << endl;
return 0;
}
``````

``````#include <bits/stdc++.h>

using namespace std;
const int N = 110;
const int INF = 0x3f3f3f3f;

int n, m;
int f[N];

int main() {
cin >> n >> m;

memset(f, -INF, sizeof f);
f[0] = 0;

for (int i = 1; i <= n; i++) {
int v, w;
cin >> v >> w;
for (int j = v; j <= m; j++) {
f[j] = max(f[j], f[j - v] + w);
}
}
cout << f[m] << endl;
return 0;
}
``````