解题思路
代码
原始做法
#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;
}