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 <queue>
#include <vector>
#include <iostream>
using namespace std;

// ------ Class ------ //
class Graph2 {
public:
	// ------ Variables ------ //
	static const long long INF = 1000000000000000000;
	int V, E; vector<vector<pair<int, long long> > > G;

	// ------ Constructors ------ //
	Graph2() : V(0), E(0), G(vector<vector<pair<int, long long> > >()) {}
	Graph2(int v) : V(v), E(0), G(vector<vector<pair<int, long long> > >(v)) {}
	Graph2(vector<vector<pair<int, long long> > > g) : V(g.size()), G(g) { for (int i = 0; i < g.size(); i++) E += g[i].size(); }

	// ------ Basic Functions ------ //
	int size() { return V; }
	void add1(int v1, int v2, long long w) { G[v1].push_back(make_pair(v2, w)); E++; }
	void add2(int v1, int v2, long long w) { add1(v1, v2, w); add1(v2, v1, w); }

	// ------ Operators ------ //
	bool operator==(const Graph2& g) const { return G == g.G; }
	bool operator!=(const Graph2& g) const { return G != g.G; }
	vector<pair<int, long long> > operator[](int x) { return G[x]; }

	// ------ Algorithms ------ //
	long long minspan() {
		vector<long long> d(V, INF);
		vector<bool> used(V, false);
		priority_queue<pair<long long, int> > que;
		for (int i = 0; i < V; i++) d[i] = INF;
		d[0] = 0; que.push(make_pair(0, 0));
		while (!que.empty()) {
			pair<long long, int> pa = que.top(); que.pop();
			int u = pa.second; used[u] = true;
			for (int i = 0; i < G[u].size(); i++) {
				int v = G[u][i].first;
				if (d[v] > G[u][i].second && !used[v]) {
					d[v] = G[u][i].second; que.push(make_pair(-d[v], v));
				}
			}
		}
		long long sum = 0;
		for (int i = 0; i < V; i++) sum += d[i];
		return sum;
	}
};

// ------ Main ------ //
int n, a;
int main() {
	cin >> n; Graph2 g(n);
	for(int i = 0; i < n; i++) {
		for(int j = 0; j < n; j++) {
			cin >> a;
			if(a != -1) g.add1(i, j, a);
		}
	}
	cout << g.minspan() << endl;
}

C++: #include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
typedef pair<int,int> P;
typedef pair<int,P> T;

class uft {//union-find tree
public:
    int parent[101];
    int size[101];
    int root(int x);
    bool same(int x,int y);
    void unite(int x,int y);
};
int uft::root(int x){
    if(parent[x]==x){
        return x;
    }
    return parent[x] = root(parent[x]);
}
bool uft::same(int x,int y){
    if(root(x)==root(y)){
        return true;
    }else{
        return false;
    }
}
void uft::unite(int x,int y){
    x=root(x);
    y=root(y);
    if(x == y) return;
    parent[x] = y;
    size[y] = size[y]+size[x];
}

int solv(priority_queue<T, vector<T>, greater<T> > q,int N){
    uft u;
    for(int i=0;i<N;i++){
        u.parent[i]=i;
        u.size[i]=1;
    }
    int ret=0;
    while(u.size[u.root(0)] != N && !q.empty()){
        T edge = q.top(); q.pop();
        if(u.root((edge.second).first) != u.root((edge.second).second)){
            u.unite((edge.second).first,(edge.second).second);
            ret += edge.first;
        }
    }
    return ret;
}

int main(){
    int N;
    priority_queue<T, vector<T>, greater<T> > q;
    cin>>N;
    for(int i=0;i<N;i++){
        for(int j=0;j<N;j++){
            int a;
            cin>>a;
            if(a!=-1 && i<j){
                T d = T(a,P(i,j));
                q.push(d);
            }
        }
    }
    int ans=solv(q,N);
    cout<<ans<<endl;
    return 0;
}

C++: #include<iostream>


using namespace std;
int n;
int M[100][100];

int prim() {
	int u;
	int color[100];
	int d[100];
	int p[100];
	int mincost;
	for (int i = 0;i < n;i++) {
		color[i] = 0;
		d[i] =2001;
		p[i] = -1;
	}
	d[0] = 0;
	

	while (true) {
		mincost = 2001;
		for (int i = 0;i < n;i++) {
			if (color[i] != 2 && d[i] < mincost) {
				mincost = d[i];
				u = i;
			}
		}
		if (mincost== 2001) {
			break;
		}

		color[u] = 2;
		for (int v = 0;v < n;v++) {
			if (color[v] != 2 && M[u][v] != -1) {
				if (M[u][v] < d[v]) {
					d[v] = M[u][v];
					p[v] = u;
					color[v] = 1;
				}
			}
		}
	}
	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;
		}
	}
	cout << prim() << endl;

	return 0;
}

C++: 
#include <iostream>
using namespace std;
const int MAX = 100;
const int INFTY = 2000000;
const int WHITE = 0;
const int GRAY = 1;
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<bits/stdc++.h>
using namespace std;

#define MAX 101
#define BLACK 0
#define GRAY 1
#define WHITE 2
#define INF (1<<30)

int graph[MAX][MAX];
int n;

// プロトタイプ宣言
int prim();
// ここまで

int main(){
	cin>>n;

	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			cin>>graph[i][j];
		}
	}
	cout<<prim()<<endl;


	return 0;
}




//---------------------- 
int prim(){
	int from;
	int color[MAX];
	int d[MAX];
	int p[MAX];

	for(int i=0;i<MAX;i++){
		color[i]=WHITE;
		d[i]=INF;		
		p[i]=-1;
	}
	d[0]=0;

	while(1){
		int minCost=INF;
		from=-1;

		for(int i=0;i<n;i++){
			if(color[i]!=BLACK && d[i]<minCost){
				minCost=d[i];
				from=i;
			}
		}
		if(from == -1) break;

		color[from]=BLACK;
		for(int to=0;to<n;to++){
			if(color[to]!=BLACK && graph[from][to]!=-1){
				if(graph[from][to]<d[to]){
					d[to]=graph[from][to];
					p[to]=from;
					color[to]=GRAY;
				}
			}
		}
	}

	int sum=0;
	for(int i=0;i<n;i++){
		// if(p[i]!=-1){
		// 	sum += graph[i][ p[i] ];
		// }
		if(d[i]!=INF){
			sum+=d[i];
		}
	}

	return sum;
}