Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

//util
var sc = bufio.NewScanner(os.Stdin)

func nextStr() string {
	sc.Scan()
	return sc.Text()
}

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func printList(list []int) {
	n := len(list)
	buf := bufio.NewWriter(os.Stdout)
	for _, v := range list[0 : n-1] {
		buf.Write([]byte(fmt.Sprintf("%d ", v)))
	}
	buf.Write([]byte(fmt.Sprintf("%d\n", list[n-1])))
	buf.Flush()
}

func max(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

func min(a, b int) int {
	if a > b {
		return b
	} else {
		return a
	}
}

//solve
const (
	MAX   = 100
	INFTY = (1 << 21)
	WHITE = 0
	GRAY  = 1
	BLACK = 2
)

//V: グラフの頂点全体の集合
//T: MSTに属する頂点の集合
var (
	n     int
	M     [MAX][MAX]int //重みを記録する隣接行列
	color [MAX]int
	p     [MAX]int //MSTにおける頂点の親を記録
	d     [MAX]int //Tに属する頂点と、V−Tに属する頂点を繋ぐ辺の中で、重みが最小のものを記録
)

func prim() int {
	var u, minv int
	//初期化
	for i := 0; i < n; i++ {
		d[i] = INFTY
		p[i] = -1
		color[i] = WHITE
	}

	d[0] = 0

	for {
		//頂点uを決めていく
		//つまり、Tに属する頂点と、V−Tに属する頂点を繋ぐ辺の中で、重みが最小のものを決める
		minv = INFTY
		u = -1
		for i := 0; i < n; i++ {
			//未訪問かつ、辺の重みが最小となるuを選ぶ
			if minv > d[i] && color[i] != BLACK {
				u = i
				minv = d[i]
			}
		}

		if u == -1 {
			break
		}

		color[u] = BLACK
		for 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
				}
			}
		}
	}

	sum := 0
	for i := 0; i < n; i++ {
		if p[i] != -1 {
			sum += M[i][p[i]]
		}
	}
	return sum
}

func main() {
	sc.Split(bufio.ScanWords)
	n = nextInt()

	for i := 0; i < n; i++ {

		for j := 0; j < n; j++ {
			tmp := nextInt()
			if tmp == -1 {
				tmp = INFTY
			}
			M[i][j] = tmp
		}

	}

	fmt.Println(prim())

}

C++: #include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <deque>
#include <bitset>
#include <numeric>
#include <sstream>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <stack>
#include <tuple>

using namespace std;

#define int long long

#define rep(i,n) for(int i = 0; i<n ; i++)
#define FOR(i,a,n) for(int i=a; i<n; i++)
#define REPSTR(i,s) for(int i=0; (s)[i]; i++)
#define REPITR(i,s) for(auto i=(s).begin(); i!=(s).end();i++)

#define RANGE(a,i,b) ((a)<=(i)&&(i)<=(b))

#define pb push_back
#define mp make_pair

#define ALL(a) begin(a), end(a)

#define EXIST(s,x) ((s).find(x)!=(s).end())

#define SET(a,c) memset(a,c,sizeof(a))
#define CLR(a) memset(a,0,sizeof(a))

#ifdef _DEBUG
#define DEBUG(x) cerr<<#x<<":"<<x<<endl
#else
#define DEBUG(x)
#endif

#define PRINTJOIN(x,n) rep(i,n){if(i)cout<<" ";cout<<x[i];}cout<<endl 

#define INF 1001001001
#define LLINF 1001001001001001001LL

typedef long long ll;
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef stringstream SS;

#define PQ priority_queue

#define FST first
#define SEC second

int dx[4] = { 1,0,-1,0 };
int dy[4] = { 0,1,0,-1 };

template<typename T>
class UnionFind {
    public:
    vector<T> parent;
    vector<T> rank;
    UnionFind() {}
    UnionFind(T n):parent(n), rank(n){
        rep(i, n) {
            parent[i]=i;
        }
        fill(ALL(rank), 0);
    }
    T root(T x) {
        if(parent[x] == x){
            return x;
        } else {
            parent[x] = root(parent[x]);
            return parent[x];
        }
    }
    bool same(T x, T y) {
        return root(x) == root(y);
    }
    void unite(T x, T y) {
        x = root(x);
        y = root(y);
        if (x == y) return;
        if (rank[x] < rank[y]) {
            parent[x] = y;
        } else {
            parent[y] = x;
            if(rank[x] == rank[y]) rank[x]++;
        }
    }
};

signed main(void) {
	cin.tie(0);
	ios::sync_with_stdio(false);
    int n;
    cin>>n;
    priority_queue<tuple<int,int,int>, vector<tuple<int,int,int>>, greater<tuple<int,int,int>>> q;
    rep(i,n)rep(j,n){
        int t;cin>>t;
        if(t != -1) {
            q.push(make_tuple(t, i, j));
        }
    }
    UnionFind<int> uf(n);
    int ans = 0;
    while(!q.empty()) {
        tuple<int,int,int> t = q.top();q.pop();
        int c,x,y;
        c=get<0>(t);
        x=get<1>(t);
        y=get<2>(t);
        if(!uf.same(x,y)) {
            ans += c;
            uf.unite(x,y);
        }
    }
    cout<<ans<<endl;
	return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <vector>
#include <list>

#define INFTY 1000000001

class Graph{
public:
	std::vector< std::vector<int> > A;
	int n;

	Graph(int n);

	void SetEdge(int a, int b, int weight);
	void SortEdge();
	void SortEdge_Rec1(int left, int right);
	void SortEdge_Rec2(int left, int mid, int right);

	int Kruskal();

};

Graph::Graph(int n){;
	this->n = n;
}

void Graph::SetEdge(int a, int b, int weight){
	std::vector<int> x;
	x.push_back(a); x.push_back(b); x.push_back(weight);
	A.push_back(x);
	return;
}

void Graph::SortEdge(){
	SortEdge_Rec1(0, A.size());
}

void Graph::SortEdge_Rec1(int left, int right){
	if (left + 1 < right){
		int mid = (left + right) / 2;
		SortEdge_Rec1(left, mid);
		SortEdge_Rec1(mid, right);
		SortEdge_Rec2(left, mid, right);
	}
}

void Graph::SortEdge_Rec2(int left, int mid, int right){
	int n1 = mid - left;
	int n2 = right - mid;
	std::vector< std::vector<int> > L(n1 + 1);
	for (int i = 0; i < n1; i++) L[i] = A[left + i];
	std::vector< std::vector<int> > R(n2 + 1);
	for (int i = 0; i < n2; i++) R[i] = A[mid + i];
	std::vector<int> x;
	x.push_back(0); x.push_back(0); x.push_back(INFTY);
	L[n1] = x;
	R[n2] = x;
	int i = 0;
	int j = 0;
	for (int k = left; k < right; k++){
		if (L[i][2] <= R[j][2]){
			A[k] = L[i];
			i = i + 1;
		}
		else{
			A[k] = R[j];
			j = j + 1;
		}
	}
}

int Graph::Kruskal(){
	std::vector<int> f(n);
	for (int i = 0; i < n; i++) f[i] = i;
	int branch = 0;

	while (!A.empty()){
		std::vector<int> e = A[0];
		A.erase(A.begin());
		if (f[e[0]] != f[e[1]]){
			int tmp = f[e[1]];
			for (int i = 0; i < n; i++) if (f[i] == tmp) f[i] = f[e[0]];
			branch += e[2];
		}
	}
	return branch;
}

int main(){
	int n, e;
	scanf("%d", &n);
	Graph G(n);
	for (int i = 0; i < n; i++){
		for (int j = 0; j < n; j++){
			scanf("%d", &e);
			if (j >= i && e != -1) G.SetEdge(i, j, e);
		}
	}

	G.SortEdge();

	printf("%d\n", G.Kruskal());

	return 0;
}

C++: #include <iostream>
using namespace std;
int n,m[100][100];
void mins(){
    int u,min;
    int d[100],p[100],color[100];
    for(int i=0;i<n;i++){
        d[i]=2001;
        p[i]=-1;
        color[i]=1;
    }
    d[0]=0;
    while(1){
        min=2001;
        u=-1;
        for(int i=0;i<n;i++){
            if(color[i]!=3&&d[i]<min){
                u=i;
                min=d[i];
            }
        }
        if(u==-1){
            break;
        }
        color[u]=3;
        for(int v=0;v<n;v++){
            if(color[v]!=3&&m[u][v]!=-1){
                if(d[v]>m[u][v]){
                    p[v]=u;
                    d[v]=m[u][v];
                    color[v]=2;
                }
            }
        }
    }
    int ans=0;
    for(int i=0;i<n;i++){
        if(p[i]!=-1){
            ans+=d[i];
        }
    }
    cout<<ans<<endl;
}
int main(){
    cin>>n;
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            cin>>m[i][j];
        }
    }
    mins();
    return 0;
}