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
	num := 0
	for len(edges) > 0 && num < n-1 {
		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
		num += 1
	}
	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<cstring>
#include<algorithm>
using namespace std;

int x[10000][10000];
int MIN[10000];
int MINX = 0;
int MINZ = 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;
		}
	}
	for(int h=0;h<n;h++){
		memset(MIN, 127, sizeof(MIN));
		MINX=0;
		MIN[h] = 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], x[j][k]);
					}
				}
			}
		}
		for (int i = 0; i < n; i++) {
			MINX += MIN[i];
		}
		MINZ=min(MINZ,MINX);
	}
	cout << MINZ << endl;
	return 0;
}

C++: #include <iostream>
using namespace std;
static const int MAX = 100;
static const int INFTY = (1 << 21);
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;

int n, M[MAX][MAX];

int prim() {
	int u, minv;
	int d[MAX], p[MAX], color[MAX];
	for (int i = 0; i < n; i++)
	{
		d[i] = INFTY;
		p[i] = -1;
		color[i] = WHITE;
	}
	d[0] = 0;
	while (1)
	{
		minv = INFTY;
		u = -1;
		for (int i = 0; i < n; i++)
		{
			if (minv > d[i] && color[i] != BLACK) {
				u = i;
				minv = d[i];
			}
		}
		if (u == -1) break;
		color[u] = BLACK;
		for (int v = 0; v < n; v++)
		{
			if (d[v] > M[u][v]) {
				d[v] = M[u][v];
				p[v] = u;
				color[v] = GRAY;
			}
		}
	}
		int sum = 0;
		for (int i = 0; i < n; i++)
		{
			if (p[i] != -1) sum += M[i][p[i]];
		}
		return sum;
	}

	int main() {
		cin >> n;

		for (int i = 0; i < n; i++)
		{
			for (int j = 0; j < n; j++) {
				int e; cin >> e;
				M[i][j] = (e == -1) ? INFTY : e;
			}
		}
		cout << prim() << endl;
		return 0;
	}

C++: #include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for(int i=0; i<(n); i++)
#define ALL(n) begin(n),end(n)
using Graph=vector<vector<int>>;

int n;
// プリムのアルゴリズム
vector<int> included;
int Prim(const Graph &G,int v,int &cost){
    // 頂点vは木に含まれた
    included.push_back(v);
    // n頂点全てが木に含まれていれば終了
    if(included.size()==n) return cost;

    int min_=2001;
    int to=-1;
    for(int x:included){
        for(int y=0; y<n; y++){
            if(G[x][y]<min_ && G[x][y]!=-1){
                // yがincludedに含まれている場合，何もしない
                if(find(included.begin(),included.end(),y)!=included.end()){
                    continue;
                }
                min_=G[x][y];
                to=y;
            }
        }
    }

    // 頂点toを追加して，Primを呼び出す
    cost+=min_;
    return Prim(G,to,cost);
}

int main(){
    cin>>n;
    
    // 隣接行列を受け取る
    Graph G(n,vector<int>(n));
    REP(i,n){
        REP(j,n){
            cin>>G[i][j];
        }
    }

    int cost=0;

    cout<<Prim(G,0,cost)<<endl;
}

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

static const int MAX = 100;
static const int INFTY = (1 << 21);
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;

int n, M[MAX][MAX];

int prim() {
    
    int u, minv;
    int d[MAX], p[MAX], color[MAX];
    
    for (int i = 0; i < n; i ++) {
        
        d[i] = INFTY;
        p[i] = -1;
        color[i] = WHITE;
        
    }
    
    d[0] = 0;
    
    while(1) {
        
        minv = INFTY;
        u = -1;
        
        for (int i = 0; i < n; i ++) {
            
            if (minv > d[i] && color[i] != BLACK) {
                
                u = i;
                minv = d[i];
                
            }
            
        }
        
        if (u == -1) {
            
            break;
            
        }
        
        color[u] = BLACK;
        
        for(int v = 0; v < n; v ++) {
            
            if (color[v] != BLACK && M[u][v] != INFTY) {
                
                if (d[v] > M[u][v]) {
                    
                    d[v] = M[u][v];
                    p[v] = u;
                    color[v] = GRAY;
                    
                }
                
            }
            
        }
        
    }
        
    int sum = 0;
    
    for (int i = 0; i < n; i ++) {
        
        if (p[i] != -1) {
            
            sum += M[i][p[i]];
            
        }
        
    }
    
    return sum;
    
}

int main() {
    
    cin >> n;
    
    for (int i = 0; i < n; i ++) {
        
        for (int j = 0; j < n; j ++) {
            
            int e;
            
            cin >> e;
            
            M[i][j] = (e == -1) ? INFTY : e;
            
        }
        
    }
    
    cout << prim() << endl;
    
    return 0;
    
}

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

int m,n,x,ans;
int P[100+10],edge1[10000+10],edge2[10000+10];


int root(int a){
	if (P[a] == a) return a;
	return (P[a] = root(P[a]));
}

bool same(int a, int b){
	return root(a)==root(b);
}
	
void unite(int a, int b){
	P[root(a)] = root(b);
}

int main() {
	pair<int,int> edges[10000+10];
	cin >> n;
	
	for (int i=1; i<=n; i++){
		P[i] = i;
	}
	
	m = 0;
	for (int i=1; i<=n; i++){
		for (int j=1; j<=n; j++){
			cin >> x;
			if (x>-1){
				edge1[m] = i;
				edge2[m] = j;
				edges[m] = make_pair(x, m);
				m+=1;
			}
		}
	}
	
	sort(edges, edges+m);
	n=1;
	for (int i=0; i<m; i++){
		if (n>=m) break;
		if (!same(edge1[edges[i].second], edge2[edges[i].second])){
			unite(edge1[edges[i].second], edge2[edges[i].second]);
			ans += edges[i].first;
			n+=1;
		}
	}
	
	cout << ans << endl;
	
}