Go: package main

import (
	"bufio"
	"fmt"
	"log"
	"math"
	"os"
	"strconv"
	"strings"
)

type color int

const (
	white color = iota // 未訪問
	mst                // MSTとして確定
)

// 頂点
type node struct {
	matrix   []int // 隣接行列
	minToMst int   // MSTへの重み最小値
	parent   int   // 最小値の相手
	color    color
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	n := eGetInt(scanner)

	nodes := make([]node, n)
	for i := 0; i < n; i++ {
		nodes[i].matrix = eAtoi((strings.Fields(eGetline(scanner))))
		nodes[i].parent = -1
		nodes[i].minToMst = math.MaxInt16
		nodes[i].color = white
	}

	// 初回はnode[0]がMSTに入るように,minToMstを設定
	nodes[0].minToMst = 0
	for { // 全部チェックするまで

		// どの頂点を新たにMSTに取り込むか
		min := math.MaxInt16
		u := -1
		for i := 0; i < n; i++ {
			if nodes[i].color == mst {
				continue
			}
			if nodes[i].minToMst < min {
				u = i
				min = nodes[i].minToMst
			}
		}

		if u == -1 {
			break
		}
		// MSTに追加する頂点決定
		nodes[u].color = mst

		// MSTに属していない頂点それぞれについて,MSTとの最小の重みを更新
		for i := 0; i < n; i++ {
			if nodes[i].color == mst {
				continue
			}
			if nodes[i].matrix[u] == -1 {
				// 接してない
				continue
			}
			if nodes[i].matrix[u] < nodes[i].minToMst {
				// node[u]との重みが最小
				nodes[i].minToMst = nodes[i].matrix[u]
				nodes[i].parent = u
			}
		}
	}

	sum := 0
	for _, v := range nodes {
		// node[0]はparentが入っていない
		if v.parent != -1 {
			sum += v.matrix[v.parent]
		}
	}
	fmt.Println(sum)
}

func eGetInt(scanner *bufio.Scanner) int {
	t := eAtoi(strings.Fields(eGetline(scanner)))
	return t[0]
}

func eGetline(p *bufio.Scanner) string {
	if !p.Scan() {
		if err := p.Err(); err != nil {
			log.Fatal(err.Error())
		}
		return ""
	}
	return p.Text()
}

func eAtoi(s []string) []int {
	ints := make([]int, len(s))
	var err error
	for i, e := range s {
		ints[i], err = strconv.Atoi(e)
		if err != nil {
			log.Fatal(err.Error())
		}
	}
	return ints
}

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;
}