多重背包裸题
$ 时间复杂度O(NMS),空间复杂度O(NM)$
该题只用到了i - 1上一层的状态,也可以优化掉一维空间,从大到小循环。
参考文献
算法提高课
AC代码
#include <iostream>
using namespace std;
const int N = 510, M = 6010;
int v[N], w[N], s[N];
int f[N][M];
int n, m;
int main(){
//读入
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 = 0 ; j <= m ; j ++){
f[i][j] = f[i - 1][j];
for (int k = 1 ; k <= s[i] && 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];
return 0;
}