Run ID | 作者 | 问题 | 语言 | 测评结果 | 分数 | 时间 | 内存 | 代码长度 | 提交时间 |
---|---|---|---|---|---|---|---|---|---|
193902 | luckypet | 网络连接 | C++ | 通过 | 100 | 0 MS | 244 KB | 1096 | 2025-05-27 14:45:31 |
#include <iostream> #include <vector> #include <algorithm> using namespace std; struct Edge { int u, v, w; bool operator<(const Edge& other) const { return w < other.w; } }; vector<int> parent; int find(int x) { if (parent[x] != x) { parent[x] = find(parent[x]); } return parent[x]; } bool unite(int x, int y) { x = find(x); y = find(y); if (x == y) return false; parent[y] = x; return true; } int main() { int N, S; cin >> N >> S; vector<Edge> edges(S); for (int i = 0; i < S; ++i) { cin >> edges[i].u >> edges[i].v >> edges[i].w; } sort(edges.begin(), edges.end()); parent.resize(N + 1); for (int i = 1; i <= N; ++i) { parent[i] = i; } int total = 0; int count = 0; for (const Edge& e : edges) { if (unite(e.u, e.v)) { total += e.w; count++; if (count == N - 1) break; } } cout << total << endl; return 0; }