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
+84
View File
@@ -0,0 +1,84 @@
#include <algorithm>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
struct Node {
int mine_amount = -1, prev = -1;
};
constexpr int MAX_N = 25, INF = 0x3f3f3f3f;
int n, mine_amounts[MAX_N];
bool adj[MAX_N][MAX_N];
// int dp[MAX_N][MAX_N];
Node dp_route[MAX_N];
void solve_dp() {
for (int i=0; i<n; i++) {
dp_route[i] = {.mine_amount = mine_amounts[i]};
}
for (int current = 1; current < n; current++) {
for (int prev = 0; prev < current; prev++) {
if (adj[prev][current]) {
int new_ans = dp_route[prev].mine_amount + mine_amounts[current];
if (new_ans > dp_route[current].mine_amount) {
dp_route[current] = {.mine_amount = new_ans, .prev = prev};
}
}
}
}
}
void print_route() {
int max_ans = -1, max_idx = -1;
vector<int> final_route;
for (int i = 0; i < n; i++) {
if (dp_route[i].mine_amount > max_ans) {
max_ans = dp_route[i].mine_amount;
max_idx = i;
}
}
while (max_idx != -1) {
final_route.push_back(max_idx);
max_idx = dp_route[max_idx].prev;
}
reverse(final_route.begin(), final_route.end());
for (const int i : final_route) {
cout << i + 1 << " ";
}
cout << endl << max_ans << endl;
}
void log_dp() {
for (int i = 0; i < n; i++) {
// for (int j = 0; j < n; j++) {
// clog << dp[i][j] << " ";
// }
clog << "->" << dp_route[i].mine_amount << " " << dp_route[i].prev << endl;
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
for (int i = 0; i < n; i++) {
cin >> mine_amounts[i];
}
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
cin >> adj[i][j];
adj[j][i] = adj[i][j];
}
}
solve_dp();
log_dp();
print_route();
return 0;
}
+85
View File
@@ -0,0 +1,85 @@
#include <algorithm>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
struct Node {
int mine_amount = -1, prev = -1;
};
constexpr int MAX_N = 25, INF = 0x3f3f3f3f;
int n, mine_amounts[MAX_N];
bool adj[MAX_N][MAX_N];
int dp[MAX_N][MAX_N];
Node dp_route[MAX_N];
void solve_dp() {
memset(dp, -1, sizeof(dp));
for (int i=0; i<n; i++) {
dp_route[i] = {.mine_amount = mine_amounts[i]};
}
for (int current = 1; current < n; current++) {
for (int prev = 0; prev < current; prev++) {
if (adj[prev][current]) {
dp[current][prev] = dp_route[prev].mine_amount + mine_amounts[current];
if (dp[current][prev] > dp_route[current].mine_amount) {
dp_route[current] = {.mine_amount = dp[current][prev], .prev = prev};
}
}
}
}
}
void print_route() {
int max_ans = -1, max_idx = -1;
vector<int> final_route;
for (int i = 0; i < n; i++) {
if (dp_route[i].mine_amount > max_ans) {
max_ans = dp_route[i].mine_amount;
max_idx = i;
}
}
while (max_idx != -1) {
final_route.push_back(max_idx);
max_idx = dp_route[max_idx].prev;
}
reverse(final_route.begin(), final_route.end());
for (const int i : final_route) {
cout << i + 1 << " ";
}
cout << endl << max_ans << endl;
}
void log_dp() {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
clog << dp[i][j] << " ";
}
clog << "->" << dp_route[i].mine_amount << " " << dp_route[i].prev << endl;
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
for (int i = 0; i < n; i++) {
cin >> mine_amounts[i];
}
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
cin >> adj[i][j];
adj[j][i] = adj[i][j];
}
}
solve_dp();
log_dp();
print_route();
return 0;
}
+110
View File
@@ -0,0 +1,110 @@
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
struct Node {
int x, y, height;
};
constexpr int MAX_N = 105;
constexpr short DX[4] = {0, 1, 0, -1}, DY[4] = {1, 0, -1, 0};
int r, c;
int heights[MAX_N][MAX_N];
vector<int> adj_nodes[MAX_N * MAX_N];
vector<Node> nodes;
int dp[MAX_N * MAX_N];
int pack_coord(int x, int y) {
return x * c + y;
}
int pack_coord(Node node) {
return pack_coord(node.x, node.y);
}
bool within_board(int x, int y) {
return 0 <= x && x < r && 0 <= y && y < c;
}
void log_dp() {
for (int i = 0; i < r * c; i++) {
clog << dp[i] << "\t";
if (i % c == c - 1) {
clog << endl;
}
}
}
void log_adj_nodes() {
for (int i = 0; i < r * c; i++) {
clog << i << " -> ";
for (int adj_node : adj_nodes[i]) {
clog << adj_node << " ";
}
clog << endl;
}
}
void init_adj_nodes() {
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
nodes.push_back({i, j, heights[i][j]});
int x = i, y = j;
for (int k = 0; k < 4; k++) {
int nx = x + DX[k], ny = y + DY[k];
if (within_board(nx, ny) && heights[nx][ny] > heights[x][y]) {
// 反向建图,小 -> 大
adj_nodes[pack_coord(x, y)].push_back(pack_coord(nx, ny));
}
}
}
}
sort(nodes.begin(), nodes.end(), [](const Node& a, const Node& b) {
return a.height > b.height;
});
}
void solve_dp() {
dp[pack_coord(nodes[0])] = 1;
for (int i = 1; i < nodes.size(); i++) {
Node curr_node = nodes[i];
int max_dp = 0;
for (int adj_node : adj_nodes[pack_coord(curr_node)]) {
max_dp = max(max_dp, dp[adj_node]);
}
dp[pack_coord(curr_node)] = max_dp + 1;
}
}
int get_ans() {
int max_dp = -1;
for (int i = 0; i < r * c; i++) {
max_dp = max(max_dp, dp[i]);
}
return max_dp;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> r >> c;
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
cin >> heights[i][j];
}
}
init_adj_nodes();
solve_dp();
log_adj_nodes();
log_dp();
cout << get_ans() << endl;
return 0;
}
@@ -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;
}