朴素版本
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 1010,V = 110,M = 110;
int n,x,y;//个数 体积 重量
int v[N],m[N],w[N];//体积 重量 价值
int f[N][V][M];
int main(){
cin >> n >> x >> y;
for(int i = 1; i <= n; i++){
cin >> v[i] >> m[i] >> w[i];
}
for(int j = 0; j <= x; j++){
for(int k = 0; k <= y; k++){
f[0][j][k] = 0;
}
}
for(int i = 1; i <= n; i++){
for(int j = 0; j <= x; j++){
for(int k = 0; k <= y; 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][x][y] << endl;
return 0;
}
优化空间复杂度
#include <iostream>
#include <vector>
using namespace std;
const int N = 1010,M = 110,V = 110;
int n,v,m;
struct Good{
int v,m,w;
};
vector<Good> goods;
int f[V][M];
int main(){
cin >> n >> v >> m;
for(int i = 0; i < n; i++){
int a,b,c;
cin >> a >> b >> c;
goods.push_back({a,b,c});
}
for(int j = 0; j <= v; j++){
for(int k = 0; k <= m; k++){
f[j][k] = 0;
}
}
for(auto& good : goods){
for(int j = v; j >= good.v; j--){
for(int k = m; k >= good.m; k--){
f[j][k] = max(f[j][k],f[j - good.v][k - good.m] + good.w);
}
}
}
cout << f[v][m] << endl;
return 0;
}