Go: package main

import (
	"fmt"
	"sort"
)

type Edge struct {
	I int
	J int
	A int
}

func main() {
	var n int
	fmt.Scan(&n)

	g := make([][]int, n)
	edges := make([]Edge, 0, n*(n-1)/2)
	parents := make([]int, n)
	for i := 0; i < n; i++ {
		g[i] = make([]int, n)
		for j := 0; j < n; j++ {
			fmt.Scan(&g[i][j])
			if g[i][j] > 0 {
				edges = append(edges, Edge{i, j, g[i][j]})
			}
		}
		parents[i] = i
	}
	sort.SliceStable(edges, func(i, j int) bool { return edges[i].A > edges[j].A })

	sum := 0
	for len(edges) > 0 {
		edge := edges[len(edges)-1]
		edges = edges[:len(edges)-1]

		if same(edge.I, edge.J, &parents) {
			continue
		}
		union(edge.I, edge.J, &parents)
		sum += edge.A
	}
	fmt.Println(sum)
}



func root(id int, parents *[]int) int {
	if id == (*parents)[id] {
		return id
	}
	return root((*parents)[id], parents)
}

func union(i, j int, parents *[]int) {
	(*parents)[root(j, parents)] = (*parents)[root(i, parents)]
}

func same(i, j int, parents *[]int) bool {
	return root(i, parents) == root(j, parents)
}

C++: #include<iostream>
#include<cstdio>
#define W 0
#define G 1
#define B 2
#define inf 5000
#define MAX 100
#define NIL -1
using namespace std;

int n;
int A[MAX][MAX];

int Prim(){
    int color[MAX],d[MAX],p[MAX];
    int min,u,sum = 0;
    for(int i = 0; i < n; i++){
        color[i] = W;
        d[i] = inf;
        p[i] = NIL;
    }
    
    d[0] = 0;
    
    while(1){
        u = -1;
        min = inf;
        for(int i = 0; i < n; i++){
            if(d[i] < min && color[i] != B){
                u = i;
                min = d[i];
            }
        }
        
        if(u == -1) break;
            
        color[u] = B;
            
        for(int i = 0; i < n; i++){
            if(color[i] != B && A[u][i] != NIL){
                if(A[u][i] < d[i]){
                    d[i] = A[u][i];
                    color[i] = G;
                    p[i] = u;
                }
            }
        }
    }
    
    for(int i = 0; i < n; i++) {
        
        if(p[i] != -1) sum += A[i][p[i]];
        
    }
    
    return sum;
     
}

int main(){
    int x;
    scanf("%d",&n);
    for(int i = 0; i < n; i ++){
        for(int j = 0; j < n; j++){
            scanf("%d",&x);
            if( x == -1) A[i][j] = NIL;
            else A[i][j] = x; 
        }
    }
    
    cout << Prim() << endl;
    return 0;
}

C++: #if 0
????????£????????????1.5h
??¢?????????????????§??????????????¢??°????????£????????????sum????¨??????¨????????????????????£????????§?????????
???????????¢??????????????§????????????????????????????????????"-1"???????????????????????????????????§???????????§????????¨???????°???????????????????
#endif

#include <iostream>
#include <algorithm>
#include <utility>
using namespace std;

int P[10000+10];

void init(int N){
	for(int i=0;i<=N;++i) P[i]=i;
}
int root(int a){
	if(P[a]==a)return a;
	else return(P[a]=root(P[a]));
}
bool is_same_set(int a,int b){
	return root(a)==root(b);
}
void unite(int a,int b){
	P[root(a)]=root(b);
}
int main(){
	int N;
	cin>>N;
	int A[N][N];
	
	for(int i=0; i<N;i++){
		for(int j=0;j<N;j++){
			cin>>A[j][i]; //???????????\???
		}
	}

	int M = 0; 
	int cost[N*N],src[N*N],dst[N*N];
	for (int r=0; r<N;r++)
		for (int c=0; c<r;c++) //r<c???????§?????????????
			if (A[r][c] > -1) {
				cost[M] = A[r][c];
				src[M] = r;
				dst[M] = c;
				M += 1;
			}
	pair<int,int> edges[2000+100];
	for(int i=0;i<M;i++)
		edges[i]=make_pair(cost[i],i); //first???????????????second???ID
	sort(edges,edges+M);
	
	//for(int i=0;i<M;++i)
		//cout<<edges[i].first<<endl;

	init(N);
	int sum=0;
	for(int i=0;i<M;i++){
		if(is_same_set(src[edges[i].second],dst[edges[i].second])==0){
			unite(src[edges[i].second],dst[edges[i].second]);
			sum+=edges[i].first;
		}
	}
		cout<<sum<<endl;
}

C++: #include <stdio.h>
#include <math.h>
#include <vector>
 
using namespace std;
 
int main(){
 
    int n,tmp,sum=0,tmp_min;
    scanf("%d",&n);
    int table[n][n],check[n],num_of_group=0,parent,child;
    vector<int> Group;
 
    for(int i=0;i<n;i++){
        for(int k=0;k<n;k++){
            scanf("%d",&tmp);
            table[i][k]=tmp;
        }
        check[i]=0;
    }
 
    Group.push_back(0);
    check[0]=1;
    num_of_group = 1;
    while(num_of_group<n){
        tmp_min=2001;
 
        for(int i=0;i<num_of_group;i++){
            for(int k=0;k<n;k++){
                if(table[Group[i]][k]!=-1&&check[k]==0&&table[Group[i]][k]<tmp_min){
                    tmp_min = table[Group[i]][k];
                    parent=Group[i];
                    child = k;
                }
            }
        }
        Group.push_back(child);
        sum+=table[parent][child];
        check[child]=1;
        num_of_group++;
    }
 
    printf("%d\n",sum);
 
    return 0;
}

C++: #include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;

int x[10000][10000];
int MIN[10000];
int MINX = 0;

int main() {
	memset(MIN, 127, sizeof(MIN));
	int n, a;
	cin >> n;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			cin >> a;
			x[i][j] = a;
		}
	}
	MIN[0] = 0;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			for (int k = 0; k < n; k++) {
				if (x[j][k] != -1) {
					MIN[k] = min(MIN[k], MIN[j] + x[j][k]);
				}
			}
		}
	}
	for (int i = 0; i < n; i++) {
		MINX = max(MINX, MIN[i]);
	}
	cout << MINX << endl;
	return 0;
}