#include <cstdio>
#include <algorithm>
#include <vector>
#include <list>
#include <stack>
#include <queue>
#include <cmath>
#include <map>
#include <string>
#include <set>
#include <cstring>
#include <iostream>
#include <sstream>
#include <cassert>
using namespace std;
#define PB push_back
#define FORE(i,t) for(typeof(t.begin())i=t.begin();i!=t.end();++i)
#define SZ(x) int((x).size())
#define REP(i,n) for(int i=0,_=(n);i<_;++i)
#define FOR(i,a,b) for(int i=(a),_=(b);i<=_;++i)
#define FORD(i,a,b) for(int i=(a),_=(b);i>=_;--i)
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
struct Edge {
int cost, x, y;
bool operator < (const Edge &e) const {
return cost < e.cost;
}
};
const int INF = 1e9 + 9;
const int MAX_N = 2002;
int fu_parent[MAX_N];
void fu_makeset(int x) {
fu_parent[x] = -1;
}
int fu_find(int x) {
if (fu_parent[x] < 0)
return x;
return fu_parent[x] = fu_find(fu_parent[x]);
}
void fu_union(int x, int y) {
x = fu_find(x), y = fu_find(y);
if (fu_parent[x] < fu_parent[y])
fu_parent[y] = x;
else if (fu_parent[x] > fu_parent[y])
fu_parent[x] = y;
else if (x != y) {
fu_parent[y] = x;
--fu_parent[x];
}
}
void inline jeden() {
int n;
scanf("%d", &n);
vector<Edge> edges;
REP(i, n) {
FOR(j, i + 1, n) {
int cost;
scanf("%d", &cost);
edges.PB((Edge){cost, i, j});
}
}
REP (i, n + 1) {
fu_makeset(i);
}
sort(edges.begin(), edges.end());
ll result = 0;
FORE (edge, edges) {
if (fu_find(edge->x) != fu_find(edge->y)) {
fu_union(edge->x, edge->y);
result += edge->cost;
}
}
printf("%lld\n", result);
}
int main() {
//int z; scanf("%d", &z); while(z--)
jeden();
}
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 | #include <cstdio> #include <algorithm> #include <vector> #include <list> #include <stack> #include <queue> #include <cmath> #include <map> #include <string> #include <set> #include <cstring> #include <iostream> #include <sstream> #include <cassert> using namespace std; #define PB push_back #define FORE(i,t) for(typeof(t.begin())i=t.begin();i!=t.end();++i) #define SZ(x) int((x).size()) #define REP(i,n) for(int i=0,_=(n);i<_;++i) #define FOR(i,a,b) for(int i=(a),_=(b);i<=_;++i) #define FORD(i,a,b) for(int i=(a),_=(b);i>=_;--i) typedef long long ll; typedef vector<int> vi; typedef pair<int, int> pii; struct Edge { int cost, x, y; bool operator < (const Edge &e) const { return cost < e.cost; } }; const int INF = 1e9 + 9; const int MAX_N = 2002; int fu_parent[MAX_N]; void fu_makeset(int x) { fu_parent[x] = -1; } int fu_find(int x) { if (fu_parent[x] < 0) return x; return fu_parent[x] = fu_find(fu_parent[x]); } void fu_union(int x, int y) { x = fu_find(x), y = fu_find(y); if (fu_parent[x] < fu_parent[y]) fu_parent[y] = x; else if (fu_parent[x] > fu_parent[y]) fu_parent[x] = y; else if (x != y) { fu_parent[y] = x; --fu_parent[x]; } } void inline jeden() { int n; scanf("%d", &n); vector<Edge> edges; REP(i, n) { FOR(j, i + 1, n) { int cost; scanf("%d", &cost); edges.PB((Edge){cost, i, j}); } } REP (i, n + 1) { fu_makeset(i); } sort(edges.begin(), edges.end()); ll result = 0; FORE (edge, edges) { if (fu_find(edge->x) != fu_find(edge->y)) { fu_union(edge->x, edge->y); result += edge->cost; } } printf("%lld\n", result); } int main() { //int z; scanf("%d", &z); while(z--) jeden(); } |
English