1 条题解
-
0
传送门网络(Portals G) 题解
思路分析
按题目要求使用对应数据结构与算法模拟/递推求解,注意数据范围边界与时间复杂度。
参考代码
#include <iostream> #include <vector> #include <numeric> #include <algorithm> using namespace std; struct Edge { int u, v, w; bool operator<(const Edge& o) const { return w < o.w; } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; if (!(cin >> n)) return 0; int m = 2 * n; vector<int> p(m + 1); iota(p.begin(), p.end(), 0); auto find = [&](int x) { while (p[x] != x) x = p[x] = p[p[x]]; return x; }; vector<Edge> edges(n); for (int i = 0; i < n; ++i) { int c, p1, p2, p3, p4; cin >> c >> p1 >> p2 >> p3 >> p4; p[find(p1)] = find(p2); p[find(p3)] = find(p4); edges[i] = {p1, p3, c}; } sort(edges.begin(), edges.end()); long long ans = 0; for (const auto& e : edges) { int ru = find(e.u), rv = find(e.v); if (ru != rv) { p[ru] = rv; ans += e.w; } } cout << ans << "\n"; return 0; }
- 1
信息
- ID
- 8744
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- (无)
- 标签
- 递交数
- 0
- 已通过
- 0
- 上传者