Files
python/C++专题课程/背包九讲/TestProject/2_wholePackage.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

34 lines
965 B
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
#define maxV 1000
/**
* 2. 完全背包
1)题目:
有n件物品和一个容量为v的背包,每种物品都有无限件可用。
第i种物品的费用是c[i],价值是w[i]。
求解将哪些物品装入背包可使这些物品的费用总和不超过背包容量,且价值总和最大。
2)输入:
测试用例数
物品数 背包容量
n个物品的ci和vi
*/
int main(void) {
int cases, n, v, ci, wi;
int f[maxV];
freopen("../2.txt", "r", stdin);
cin >> cases;
while (cases--) {
memset(f, 0, sizeof(f));
cin >> n >> v;
for (int i = 0; i < n; i++) {
cin >> ci >> wi;
for (int j = 0; j <= v; j++) {
if (j >= ci)
f[j] = max(f[j], f[j - ci] + wi);
}
}
cout << f[v] << endl;
}
}