1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
|
#include <algorithm> #include <iostream> #include <cstring> #include <cstdio> #include <vector>
#define FILE_IN(__fname) freopen(__fname, "r", stdin) #define FILE_OUT(__fname) freopen(__fname, "w", stdout) #define rap(a,s,t,i) for (int a = s; a <= t; a += i) #define basketball(a,t,s,i) for (int a = t; a > s; a -= i) #define countdown(s) while (s --> 0) #define IMPROVE_IO() std::ios::sync_with_stdio(false)
using std::cin; using std::cout; using std::endl;
typedef long long int lli;
int getint() { int x; scanf("%d", &x); return x; } lli getll() { long long int x; scanf("%lld", &x); return x; }
const int MAXN = 2000 + 10;
struct UnionFind { int u[MAXN]; UnionFind() { memset(u, 0, sizeof u); } void Init(int x) { for (int i = 1; i <= x; ++i) u[i] = i; } int Find(int x) { return u[x] == x ? x : (u[x] = Find(u[x])); } bool Union(int x, int y) { x = Find(x); y = Find(y); if (x == y) return false; u[x] = y; return true; } } U;
struct Edge { int u, v, w; Edge(int u = 0, int v = 0, int w = 0) : u(u), v(v), w(w) {} bool operator < (const Edge &that) const { return w < that.w; } };
int n, cnt; lli ans; std::vector<Edge> edge;
void Kruskal() { int tot = 0; std::sort(edge.begin(), edge.end()); for (int i = 0; i < cnt; ++i) { if (U.Union(edge[i].u, edge[i].v)) { ++tot; ans += 1ll * edge[i].w; } } }
int main() { n = getint(); U.Init(n + 1); for (int i = 0; i < n; ++i) { for (int j = i + 1; j <= n; ++j) { int x = getint(); edge.push_back(Edge(i, j, x)); } } cnt = (int) edge.size(); Kruskal(); printf("%lld\n", ans); return 0; }
|