版本比较
假如 DS 参加 CSP-S 2025
正文
增加 133 行删除 0 行
1234567891011643 行未改动全部展开6556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937941,379 行未改动全部展开## 前情提要看到[此文](/article/4mxxedkq),突然想让 DS 做一下今年的 CSP-S,便写了这篇专栏。
打开深度思考,关闭联网搜索。
以洛谷作为唯一的评测方式。
提示词:```你是一位算法竞赛选手,请完成以下题目:[题面]```
并未给出题面以外的样例。643 行未改动全部展开
::::[record 20pts](https://www.luogu.com.cn/record/245846263)## T2::::info[code]```cpp#include <bits/stdc++.h>using namespace std;typedef long long ll;
struct Edge { int u, v; ll w; Edge(int u, int v, ll w) : u(u), v(v), w(w) {} bool operator<(const Edge& other) const { return w < other.w; }};
struct DSU { vector<int> parent; vector<int> rank; DSU(int n) { parent.resize(n+1); rank.assign(n+1, 0); for (int i = 1; i <= n; i++) parent[i] = i; } int find(int x) { while (parent[x] != x) { parent[x] = parent[parent[x]]; x = parent[x]; } return x; } bool unite(int a, int b) { a = find(a); b = find(b); if (a == b) return false; if (rank[a] < rank[b]) swap(a, b); parent[b] = a; if (rank[a] == rank[b]) rank[a]++; return true; }};
int main() { ios::sync_with_stdio(false); cin.tie(0); int n, m, k; cin >> n >> m >> k; vector<Edge> original_edges; for (int i = 0; i < m; i++) { int u, v; ll w; cin >> u >> v >> w; original_edges.push_back(Edge(u, v, w)); }
sort(original_edges.begin(), original_edges.end()); DSU dsu0(n); vector<Edge> MST0_edges; ll C0 = 0; for (const Edge& e : original_edges) { if (dsu0.unite(e.u, e.v)) { MST0_edges.push_back(e); C0 += e.w; } }
vector<ll> c(k); vector<vector<ll>> a(k, vector<ll>(n)); for (int j = 0; j < k; j++) { cin >> c[j]; for (int i = 0; i < n; i++) { cin >> a[j][i]; } }
vector<vector<Edge>> town_edges(k); for (int j = 0; j < k; j++) { for (int i = 0; i < n; i++) { town_edges[j].push_back(Edge(n + j + 1, i + 1, a[j][i])); } sort(town_edges[j].begin(), town_edges[j].end()); }
ll ans = C0;
for (int mask = 1; mask < (1 << k); mask++) { vector<Edge> current_edges = MST0_edges; ll current_cost = C0; int current_node_count = n; ll total_c = 0;
for (int j = 0; j < k; j++) { if (mask & (1 << j)) { total_c += c[j]; vector<Edge> new_edges; size_t idx1 = 0, idx2 = 0; while (idx1 < current_edges.size() && idx2 < town_edges[j].size()) { if (current_edges[idx1] < town_edges[j][idx2]) { new_edges.push_back(current_edges[idx1++]); } else { new_edges.push_back(town_edges[j][idx2++]); } } while (idx1 < current_edges.size()) new_edges.push_back(current_edges[idx1++]); while (idx2 < town_edges[j].size()) new_edges.push_back(town_edges[j][idx2++]);
DSU dsu(n + k); vector<Edge> selected_edges; ll new_cost = 0; int edges_needed = current_node_count; for (const Edge& e : new_edges) { if (dsu.unite(e.u, e.v)) { selected_edges.push_back(e); new_cost += e.w; if (selected_edges.size() == edges_needed) break; } } current_edges = selected_edges; current_cost = new_cost; current_node_count++; } } ll total_cost = current_cost + total_c; if (total_cost < ans) { ans = total_cost; } } cout << ans << endl; return 0;}```
::::## T3::::info[思考过程(783s/13min)]```首先,问题是关于谐音替换的。我们有n个字符串二元组(s_i1, s_i2),每个二元组中的两个字符串长度相等。然后有q个查询,每个查询给出两个字符串t1和t2,问有多少种方式通过一次替换(替换一个子串y为y',其中y必须等于某个s_i1,y'等于对应的s_i2)将t1变成t2。注意,替换方式不同当且仅当子串y的位置不同或使用的二元组i不同。1,379 行未改动全部展开