题解
2023-08-20 14:29:12
发布于:广东
45阅读
0回复
0点赞
#include <iostream>
using namespace std;
const int inf = 0x3f3f3f3f;
const int MAX_N = 1000;
int weights[MAX_N];
int values[MAX_N];
int quantities[MAX_N];
int main() {
int C, n;
cin >> C >> n;
for (int i = 0; i < n; ++i) {
cin >> weights[i] >> values[i] >> quantities[i];
if(!quantities[i])
{
quantities[i] = inf;
}
}
int dp[C + 1] = {0};
for (int i = 0; i < n; ++i) {
int weight = weights[i];
int value = values[i];
int quantity = quantities[i];
for (int j = C; j >= weight; --j) {
for (int k = 1; k <= quantity && k * weight <= j; ++k) {
dp[j] = max(dp[j], dp[j - k * weight] + k * value);
}
}
}
cout << dp[C] << endl;
return 0;
}
不理解,这和多重背包有什么区别?是0直接覆盖成3f3f3f3f就行了
这里空空如也
有帮助,赞一个