1 #include<cstdio>
2 #include<iostream>
3 #include<algorithm>
4 using namespace std;
5
6 int cnt, f[1005];
7
8 int k, h;
9 struct node{
10 int x, y, l;
11 } a[110005];
12
13 inline int cmp(node i, node j){
14 return i.l < j.l;
15 }
16
17 inline int find(int x){
18 if(f[x] != x)
19 f[x] = find(f[x]);
20 return f[x];
21 }
22
23 int main(){
24 int n;
25 int z;
26 scanf("%d", &n);
27 for(int i = 1; i <= n; i++){
28 for(int j = 1; j <= n; j++){
29 scanf("%d", &z);
30 if(j > i){ //只读取上面一半有用的数据
31 a[++cnt].x = i;
32 a[cnt].y = j;
33 a[cnt].l = z;
34 }
35 }
36 }
37 for(int i = 1; i <= n; i++)
38 f[i] = i;
39 sort(a+1, a+cnt+1, cmp);
40 for(int i = 1; i <= cnt; i++){
41 int r1 = find(a[i].x);
42 int r2 = find(a[i].y);
43 if(r1 != r2){
44 f[r1] = r2;
45 h += a[i].l;
46 k++;
47 }
48 if(k == n-1) break;//跳出
49 }
50 printf("%d", h);
51 return 0;
52 }