多重背包问题是每个物品最多有s[i]个
多重背包问题有朴素版和优化版
dp[i][j]只从前i个物品中选,体积<=j的最大值
根据第i个物品选多少个来划分
三重循环的暴力做法
1 #include <bits/stdc++.h> 2 using namespace std; 3 const int N = 110; 4 int dp[N][N], v[N], w[N], s[N]; 5 int main() { 6 int n, m; 7 cin >> n >> m; 8 for (int i = 1; i <= n; i++) { 9 cin >> v[i] >> w[i] >> s[i]; 10 } 11 for (int i = 1; i <= n; i++) { 12 for (int j = 0; j <= m; j++) { 13 for (int k = 0; k <= s[i] && k * v[i] <= j; k++) { 14 dp[i][j] = max(dp[i][j], dp[i - 1][j - k * v[i]] + k * w[i]); 15 } 16 } 17 } 18 cout << dp[n][m] << endl; 19 return 0; 20 }