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>
using namespace std;
#define INFTY (1 << 30)

enum Color {
	WHITE,
	GRAY,
	BLACK,
};

void Prim(int verValWeight[][100], int verNum) {
	int distance[100];
	Color color[100];
	int parent[100];
	for (int i = 0; i < verNum; i++) {
		color[i] = WHITE;
		distance[i] = INFTY;
		parent[i] = -1;
	}
	distance[0] = 0;

	while (true) {
		int min_cost = INFTY;
		int subScript;
		for (int i = 0; i < verNum; i++) {
			if (color[i] != BLACK && distance[i] < min_cost) {
				min_cost = distance[i];
				subScript = i;
			}

			if (min_cost == INFTY)
				break; //end

			color[subScript] = BLACK;

			for (int i = 0; i < verNum; i++) {
				if (color[i] != BLACK && verValWeight[subScript][i] != INFTY) {
					if (verValWeight[subScript][i] < distance[i]) {
						distance[i] = verValWeight[subScript][i];
						parent[i] = subScript;
						color[i] = GRAY;
					}
				}
			}
		}
	}

	int weight_sum = 0;
	for (int i = 0; i < verNum; i++)
		if (parent[i] != -1)
			weight_sum += verValWeight[i][parent[i]];
	cout << weight_sum << endl;

}

int main() {
	int verNum;
	int verValWeight[100][100];
	int parent[100];

	cin >> verNum;
	for (int i = 0; i < verNum; i++)
		for (int j = 0; j < verNum; j++) {
			cin >> verValWeight[i][j];
			if (verValWeight[i][j] == -1) verValWeight[i][j] = INFTY;
		}

	Prim(verValWeight, verNum);

	return 0;
}

C++: #include <iostream>
//#include <string>
#include <algorithm>
using namespace std;
//#define int long long
//#define _DEBUG
class UnionFindTree{
    private:
    int P[10010];
    public:
    UnionFindTree(int N){
        for(int i=0; i<=N; i++) P[i] = i;
    }
    int root(int a){
        if (P[a] == a) return a;
        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);
    }
};
struct weight_edge{// (weight, (vert_a,vert_b))
    int w;
    int a;
    int b;
};
int n, k;
weight_edge we[10007];
bool com_we(weight_edge a, weight_edge b){
    return ((a.w == b.w)? a.a < b.a: a.w < b.w);
}
int solve(){
    if(n==0)return 0;
    sort(we, we+k, com_we);
    //for(int i=0; i<k; i++)printf("%d,%d,%d\n",we[i].w,we[i].a,we[i].b);
    auto uft = UnionFindTree(n);
    int i=0, r=0, l=0;
    while(l<n-1){
        if(!uft.is_same_set(we[i].a, we[i].b)){
            uft.unite(we[i].a, we[i].b);
            r += we[i].w;
            l++;
            //printf("%d;\tw:%d,\t(%d,%d)\n",l,we[i].w,we[i].a,we[i].b);
        }
        i++;
    }
    return r;
}
void get(){
    k = 0;
    for(int i=0; i<n; i++){
        for(int j=0; j<n; j++){
            int t;
            cin >> t;
            if(i<j && t>=0){
                we[k].w = t;
                we[k].a = i; we[k].b = j;
                k++;
            }
            //cout << t<<":"<<i<<","<<j<<"\t";
        }
        //cout << endl;
    }
    //cout << "k"<<k<<endl;
}
signed main(){    
    while(cin >> n){
        get();
        cout << solve() <<  endl;
    }
}

C++: #define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
//#include "MyMath.h"
//#include "MyDisjointset.h"
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <functional>
#include <stdio.h>
using namespace std;
typedef pair<int, int> P;
const int INF = 2000000000;
class Disjointset {
public:
	vector<int> rank, p;
	Disjointset(int n);
	int find(int x);
	bool same(int x, int y);
	void unite(int x, int y);
};

Disjointset::Disjointset(int n) {
	rank.resize(n, 0);
	for (int i = 0; i < n; i++)
		p.push_back(i);
}
int Disjointset::find(int x) {
	if (x != p[x])
		return p[x] = find(p[x]);
	else return x;
}
bool Disjointset::same(int x, int y) {
	return find(x) == find(y);
}
void Disjointset::unite(int x, int y) {
	int px = find(x);
	int py = find(y);
	if (rank[px] > rank[py]) {
		p[py] = px;
	}
	else {
		p[px] = py;
		if (rank[px] == rank[py]) rank[py]++;
	}
}
struct edge {
	int from, to, cost;
	bool operator < (const edge &e) const {
		return cost > e.cost;
	}
};
int main() {
	int n; cin >> n;
	priority_queue <edge> q;
	for (int i = 0; i < n; i++)for (int j = 0; j < n; j++) {
		int c; cin >> c;
		if (c != -1) { edge e; e.from = i; e.to = j; e.cost = c; q.push(e); }
	}
	Disjointset D(n);
	int res = 0;
	while (!q.empty()) {
		edge e = q.top(); q.pop();
		int f = e.from, t = e.to, c = e.cost;
		if (D.same(f, t)) continue;
		res += c;
		D.unite(f, t);
	}
	cout << res << endl;
	return 0;
}

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

int n, M[MAX][MAX];

int prim() {
    int d[n], p[n];
    int color[n];
    
    for(int i=0; i<n; i++) {
        color[i] = WHITE;
        d[i] = INFTY;
        p[i] = -1;
    }
    d[0] = 0;
    p[0] = -1;
    
    while(1) {
        int u;
        int mincost = INFTY;
        for (int i=0; i<n; i++) {
            if (color[i] != BLACK && d[i] < mincost) {
                mincost = d[i];
                u = i;
            }
        }
        
        if (mincost == INFTY) break;
        
        color[u] = BLACK;
        
        for (int v=0; v<=n; v++) {
            if (color[v] != BLACK && M[u][v] != -1) {
                if(M[u][v] < d[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() {
    scanf("%d", &n);
    for (int i=0; i<n; i++) {
        for (int j=0; j<n; j++) {
            scanf("%d", &M[i][j]);
        }
    }
    
    int sum = prim();
    printf("%d\n", sum);
    
    return 0;
}