Update CMakeLists.txt

This commit is contained in:
2026-09-05 11:05:50 +08:00
parent 94bdf44889
commit a7a78fbac8
22 changed files with 1339 additions and 52 deletions
@@ -0,0 +1,52 @@
#include <iostream>
using namespace std;
struct Item {
int weight, value;
};
constexpr int MAX_N = 105, MAX_CAPACITY = 1005;
int capacity, n;
Item items[MAX_N];
int dp[MAX_CAPACITY];
bool ping_pong;
void log_dp() {
// for (int i = 0; i <= 1; i++) {
for (int j = 0; j <= capacity; j++) {
clog << dp[j] << "\t";
}
clog << endl;
// }
}
void solve_dp() {
// for (int i = 0; i <= capacity; i++) { dp[0][i] = 0; }
// for (int i = 0; i <= n; i++) { dp[i][0] = 0; }
for (int stop_item = 1; stop_item <= n; stop_item++, ping_pong = !ping_pong) {
for (int w = capacity; w >= 1; w--) {
if (w >= items[stop_item - 1].weight) { // 负数
dp[w] = max(dp[w],
dp[w - items[stop_item - 1].weight] + items[stop_item - 1].value);
}
}
// log_dp();
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> capacity >> n;
for (int i = 0; i < n; i++) {
cin >> items[i].weight >> items[i].value;
}
solve_dp();
cout << dp[capacity] << endl;
return 0;
}
@@ -0,0 +1,48 @@
/*
* 背包 DP:考虑前 i 个物品的最大价值,在仅有 t 容量的情况下。
* 建议两个维度调换一下,方便乒乓/滚动。
*/
#include <iostream>
using namespace std;
struct Item {
int weight, value;
};
constexpr int MAX_N = 105, MAX_CAPACITY = 1005;
int capacity, n;
Item items[MAX_N];
int dp[MAX_CAPACITY][MAX_N];
void solve_dp() {
for (int i = 0; i <= capacity; i++) { dp[i][0] = 0; }
for (int i = 0; i <= n; i++) { dp[0][i] = 0; }
for (int stop_item = 1; stop_item <= n; stop_item++) {
for (int w = 1; w <= capacity; w++) {
dp[w][stop_item] = dp[w][stop_item - 1];
if (w >= items[stop_item - 1].weight) { // 负数
dp[w][stop_item] =
max(dp[w][stop_item],
dp[w - items[stop_item - 1].weight][stop_item - 1] + items[stop_item - 1].value);
}
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> capacity >> n;
for (int i = 0; i < n; i++) {
cin >> items[i].weight >> items[i].value;
}
solve_dp();
cout << dp[capacity][n] << endl;
return 0;
}
@@ -0,0 +1,57 @@
/*
* 背包 DP:考虑前 i 个物品的最大价值,在仅有 t 容量的情况下。
*/
#include <iostream>
using namespace std;
struct Item {
int weight, value;
};
constexpr int MAX_N = 105, MAX_CAPACITY = 1005;
int capacity, n;
Item items[MAX_N];
int dp[MAX_N][MAX_CAPACITY];
void log_dp() {
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= capacity; j++) {
clog << dp[i][j] << "\t";
}
clog << endl;
}
}
void solve_dp() {
for (int i = 0; i <= capacity; i++) { dp[0][i] = 0; }
for (int i = 0; i <= n; i++) { dp[i][0] = 0; }
for (int stop_item = 1; stop_item <= n; stop_item++) {
for (int w = 1; w <= capacity; w++) {
dp[stop_item][w] = dp[stop_item - 1][w];
if (w >= items[stop_item - 1].weight) { // 负数
dp[stop_item][w] =
max(dp[stop_item][w],
dp[stop_item - 1][w - items[stop_item - 1].weight] + items[stop_item - 1].value);
}
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> capacity >> n;
for (int i = 0; i < n; i++) {
cin >> items[i].weight >> items[i].value;
}
solve_dp();
log_dp();
cout << dp[n][capacity] << endl;
return 0;
}
@@ -0,0 +1,54 @@
#include <iostream>
using namespace std;
struct Item {
int weight, value;
};
constexpr int MAX_N = 105, MAX_CAPACITY = 1005;
int capacity, n;
Item items[MAX_N];
int dp[2][MAX_CAPACITY];
bool ping_pong;
void log_dp() {
// for (int i = 0; i <= 1; i++) {
for (int j = 0; j <= capacity; j++) {
clog << dp[ping_pong][j] << "\t";
}
clog << endl;
// }
}
void solve_dp() {
// for (int i = 0; i <= capacity; i++) { dp[0][i] = 0; }
// for (int i = 0; i <= n; i++) { dp[i][0] = 0; }
for (int stop_item = 1; stop_item <= n; stop_item++, ping_pong = !ping_pong) {
for (int w = 1; w <= capacity; w++) {
dp[ping_pong][w] = dp[!ping_pong][w];
if (w >= items[stop_item - 1].weight) { // 负数
dp[ping_pong][w] =
max(dp[ping_pong][w],
dp[!ping_pong][w - items[stop_item - 1].weight] + items[stop_item - 1].value);
}
}
// log_dp();
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> capacity >> n;
for (int i = 0; i < n; i++) {
cin >> items[i].weight >> items[i].value;
}
solve_dp();
cout << dp[!ping_pong][capacity] << endl;
return 0;
}
@@ -0,0 +1,86 @@
#include <iostream>
#include <vector>
using namespace std;
struct FlatItem {
int weight, value;
FlatItem operator+(const FlatItem& other) const {
return {.weight = weight + other.weight, .value = value + other.value};
}
};
struct OriginalItem {
FlatItem item{};
vector<FlatItem> sons;
};
constexpr int MAX_N = 65, MAX_CAPACITY = 32005;
int capacity, n;
vector<OriginalItem> items;
vector<FlatItem> flat_items;
int fathers_temp[MAX_N];
int dp[MAX_CAPACITY];
vector<FlatItem> generate_flat_items(OriginalItem item) {
switch (item.sons.size()) {
case 0:
return vector{item.item};
case 1:
return vector{
item.item,
item.sons[0] + item.item,
};
case 2:
return vector{
item.item,
item.sons[0] + item.item,
item.sons[1] + item.item,
item.sons[0] + item.sons[1] + item.item,
};
default:
throw logic_error("Invalid item size!");
}
}
void solve_dp() {
for (int i = 0; i < n; i++) {
if (items[i].item.value == -1) { continue; }
for (int w=capacity; w>=1; w--) {
// dp[w] = dp[w] // 不买
for (const auto& [weight, value] : generate_flat_items(items[i])) {
if (w >= weight) {
dp[w] = max(dp[w], dp[w - weight] + value);
}
}
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> capacity >> n;
items.resize(n);
for (int i = 0; i < n; i++) {
cin >> items[i].item.weight >> items[i].item.value >> fathers_temp[i];
items[i].item.value *= items[i].item.weight; // 题意
}
for (int i = 0; i < n; i++) {
if (fathers_temp[i] != 0) {
items[fathers_temp[i] - 1].sons.push_back(items[i].item);
items[i].item.value = -1; // 删掉这个元素!
}
}
solve_dp();
cout << dp[capacity] << endl;
return 0;
}