#include<algorithm> #include<bitset> #include<cassert> #include<complex> #include<cstring> #include<cstdio> #include<iomanip> #include<map> #include<iostream> #include<queue> #include<set> #include<stack> #include<string> #include<vector> #define FOR(i,a,b) for(int i=(a); i<=(b); ++i) #define FORD(i,a,b) for(int i=(a); i>=(b); --i) #define REP(i,n) for(int i=0; i<(n); ++i) #define VAR(v,i) __typeof(i) v = (i) #define FORE(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define ALL(x) (x).begin(),(x).end() #define SZ(x) ((int)(x).size()) #define PB push_back #define MP make_pair #define X first #define Y second #define debug(x) { cerr << #x <<" = " << (x) << endl; } #define debugv(x) { cerr << #x << " = "; FORE(it, x) cerr << *it << ", "; cerr << endl; } #define dprintf(...) fprintf(stderr, __VA_ARGS__) using namespace std; typedef pair<int, int> PII;; typedef vector<int> VI; typedef long long LL; typedef long double LD; template<class C> void mini(C&a4, C b4) { a4 = min(a4,b4); } template<class C> void maxi(C&a4, C b4) { a4 = max(a4,b4); } template<class T1, class T2> ostream& operator<<(ostream &out, pair<T1, T2> pair) { return out << "(" << pair.X << ", "<< pair.Y << ")"; }; struct E { int u, v; int c; }; bool cmp(const E& a, const E& b) { return a.c < b.c; } const int N = 2005; E edges[N*N]; int parent[N]; int find(int u) { if (parent[u] == u) return u; return parent[u] = find(parent[u]); } void unionn(int u, int v) { parent[find(u)] = find(v); } int main() { ios_base::sync_with_stdio(); cout << fixed << setprecision(10); int k = 0; int n; scanf("%d", &n); FOR(i, 0, n-1) FOR(j, i+1, n) { edges[k].u = i; edges[k].v = j; scanf("%d", &edges[k++].c); } sort(edges, edges+k, cmp); REP(i, n+1) parent[i] = i; LL sum = 0; for(E& e: edges) { if (find(e.u) == find(e.v)) continue; sum += e.c; unionn(e.u, e.v); } printf("%lld\n", sum); return 0; }
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 | #include<algorithm> #include<bitset> #include<cassert> #include<complex> #include<cstring> #include<cstdio> #include<iomanip> #include<map> #include<iostream> #include<queue> #include<set> #include<stack> #include<string> #include<vector> #define FOR(i,a,b) for(int i=(a); i<=(b); ++i) #define FORD(i,a,b) for(int i=(a); i>=(b); --i) #define REP(i,n) for(int i=0; i<(n); ++i) #define VAR(v,i) __typeof(i) v = (i) #define FORE(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define ALL(x) (x).begin(),(x).end() #define SZ(x) ((int)(x).size()) #define PB push_back #define MP make_pair #define X first #define Y second #define debug(x) { cerr << #x <<" = " << (x) << endl; } #define debugv(x) { cerr << #x << " = "; FORE(it, x) cerr << *it << ", "; cerr << endl; } #define dprintf(...) fprintf(stderr, __VA_ARGS__) using namespace std; typedef pair<int, int> PII;; typedef vector<int> VI; typedef long long LL; typedef long double LD; template<class C> void mini(C&a4, C b4) { a4 = min(a4,b4); } template<class C> void maxi(C&a4, C b4) { a4 = max(a4,b4); } template<class T1, class T2> ostream& operator<<(ostream &out, pair<T1, T2> pair) { return out << "(" << pair.X << ", "<< pair.Y << ")"; }; struct E { int u, v; int c; }; bool cmp(const E& a, const E& b) { return a.c < b.c; } const int N = 2005; E edges[N*N]; int parent[N]; int find(int u) { if (parent[u] == u) return u; return parent[u] = find(parent[u]); } void unionn(int u, int v) { parent[find(u)] = find(v); } int main() { ios_base::sync_with_stdio(); cout << fixed << setprecision(10); int k = 0; int n; scanf("%d", &n); FOR(i, 0, n-1) FOR(j, i+1, n) { edges[k].u = i; edges[k].v = j; scanf("%d", &edges[k++].c); } sort(edges, edges+k, cmp); REP(i, n+1) parent[i] = i; LL sum = 0; for(E& e: edges) { if (find(e.u) == find(e.v)) continue; sum += e.c; unionn(e.u, e.v); } printf("%lld\n", sum); return 0; } |