Code:
// multiple knapsack problem / * F [i] of the total volume of the case of i, the maximum value is the number for (int i = 0; i <n-; i ++) { for (int J = V; J> = V [i ]; J,) { F [J] = max (F [J], F [JV [I]] + W [I], F [J-2 * V [I]] + 2 * W [I] ...) } } 1.f [I] = 0 F [V] 2.f [0] = 0, F [I] = INF, I! 0 = max (F [0 ... m]) * / #include <the iostream> #include <CString> #include <algorithm> the using namespace STD; int N, V; // number of items and the capacity of the knapsack const int NUM = 1005 ; int DP [NUM]; int main() { cin >> N >> V; for (int i = 0; i < N; i++) { int v, w, n;//输入每件物品的体积 价值 和 个数 cin >> v >> w >> n; for (int j = V; j >= 0; j--)//枚举所有的体积 { for (int k = 0; k <= n && k*v<=j; k++)//看看每个体积下的价值最大是多少 { dp[j] = max(dp[j], dp[j - k*v] + k*w);//在计算的过程中每次存储最大价值 } } } cout << dp[V] << endl; return 0; }