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>
#include <utility>
#include <algorithm>
using namespace std;

int p[100];
pair<int, int> a[10000];

int root( int m ) {
	if ( p[m] != m )
		p[m] = root( p[m] );
	return p[m];
}

void unit( int x, int y ) {
	p[root( y )] = root( x );
}

bool same( int x, int y ) {
	return root( x ) == root( y );
}

int main() {
	int n, ai, pn, ans;
	cin >> n;
	for ( int i = 0; i < n * n; i++ ) {
		cin >> ai;
		if ( ai == -1 || ai % n <= ai / n )
			ai = 2001;
		a[i] = make_pair( ai, i );
	}
	sort( a, a + ( n * n ) );
	pn = 0;
	ans = 0;
	for ( int i = 0; i < n; i++ )
		p[i] = i;
	for ( int i = 0; i < n * n; i++ ) {
		if ( ! same( a[i].second % n, a[i].second / n ) ) {
			ans += a[i].first;
			pn++;
			unit( a[i].second % n, a[i].second / n );
			if ( pn >= n - 1 ) break;
		}
	}
	cout << ans << endl;
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <deque>
#include <queue>
#include <list>
#include <stack>
#include <string>
#include <functional>
#include <numeric>
#define _USE_MATH_DEFINES
#include <math.h>
#include <iomanip>
#include <map>
#include <set>
#include <cstdlib>
#include <bitset>
using namespace std;
#define INT_MAX 2147483647
#define INT_MIN (-2147483647 - 1)
#define INF (int)1e8
#define _INF (int)-1e8
#define INFLL (long long)1e14
#define _INFLL (long long)-1e14
#define Loop(i, n) for(int i = 0; i < (int)n; i++)
#define Loop1(i, n) for(int i = 1; i <= (int)n; i++)
#define Loopr(i, n) for(int i = (int)n - 1; i >= 0; i--)
#define Loopr1(i, n) for(int i = (int)n; i >= 1; i--)
#define bitmanip(m,val) static_cast<bitset<(int)m>>(val)
typedef long long int ll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
typedef vector<char> vc;
typedef vector<vector<char>> vvc;
typedef pair<int, int> P;

class MST {
private:
	struct node {
		bool done;
		vi to, cost;
		int id, from;
		ll d;
		bool operator<(const node & another) const {
			return !(d != another.d ? d < another.d : id < another.id);
		}
	};
	vector<node> nodes;
	int n;
public:
	vll result;
	MST(vector<vector<P>> data) {
		n = data.size();
		result.resize(n);
		nodes.resize(n);
		Loop(i, n) {
			nodes[i] = { false,{},{}, i, -1, INFLL };
			Loop(j, data[i].size()) {
				nodes[i].to.push_back(data[i][j].first);
				nodes[i].cost.push_back(data[i][j].second);
			}
		}
	}
	void mainfunc(int start) {
		priority_queue<node> pq;
		nodes[start].d = 0;
		pq.push(nodes[start]);
		while (pq.size()) {
			node a = pq.top(); pq.pop();
			if (nodes[a.id].done) continue;
			nodes[a.id].done = true;
			Loop(j, a.to.size()) {
				node* b = &nodes[a.to[j]];
				if ((*b).done) continue;
				if (a.cost[j] < (*b).d) {
					(*b).d = a.cost[j];
					(*b).from = a.id;
					pq.push(*b);
				}
			}
		}
		Loop(i, n) {
			result[i] = nodes[i].d;
		}
	}
};

int main() {
	int n; cin >> n;
	vector<vector<P>> data(n);
	Loop(i, n) {
		Loop(j, n) {
			int x; cin >> x;
			if (x == -1) continue;
			else data[i].push_back({ j,x });
		}
	}
	MST mst(data);
	mst.mainfunc(0);
	ll ans = 0;
	Loop(i, n) {
		ans += mst.result[i];
	}
	cout << ans << endl;
	return 0;
}

C++: #include <iostream>
using namespace std;
static const int N = 100;
static const int INF = 100000;
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;

int n, M[N][N];


int prim() {
    int u, minv;
    int color[N], d[N], p[N];
    for (int i = 0; i < n; i++){
        color[i] = WHITE;
        d[i] = INF;
        p[i] = -1;
    }

    d[0] = 0;

    while(true) {
        minv = INF;
        u = -1;
        // Tからの距離がもっとも短いものを次のノードとして選ぶ
        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; // uをT側に取り込む
        // d[]を更新(Tからの距離を更新)
        for (int v = 0; v < n; v++){
            if (color[v] != BLACK && M[u][v] != INF){
                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() {
    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) ? INF : e;
        }
    }

    cout << prim() << endl;

    return 0;
}

C++: #include <stdio.h>
#include <iostream>
#include <algorithm>
using namespace std;

#define N 100

void searchIndex(int mat[][N], int n, int obj, int *row, int *col);
void changeGroup(int group[], int n, int g_old, int g_new);

int main(void)
{
	int n;
	cin >> n;

	int group[N];
	for (int i = 0; i < n; i++) group[i] = i;

	int a[N][N];	
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			int aij;
			cin >> aij;
			a[i][j] = aij;
		}
	}

	int e[N * N];
	int k = 0;
	for (int i = 0; i < n; i++) {
		for (int j = i + 1; j < n; j++) {
			e[k++] = a[i][j];
		}
	}

	sort(e, e + k);

	int p = 0;
	while (e[p] == -1) p++;

	int cnt = 0;
	for (int q = p; q < k; q++) {
		int i, j;
		searchIndex(a, n, e[q], &i, &j);
		if (group[i] == group[j]) continue;
		cnt += e[q];
		if (group[i] > group[j]) changeGroup(group, n, group[i], group[j]);
		else changeGroup(group, n, group[j], group[i]);
	}

	cout << cnt << endl;

	return 0;
}

void searchIndex(int mat[][N], int n, int obj, int *row, int *col)
{
	for (int i = 0; i < n; i++) {
		for (int j = i + 1; j < n; j++) {
			if (mat[i][j] == obj) {
				mat[i][j] = -1;
				*row = i;
				*col = j;
				return;
			}
		}
	}
}

void changeGroup(int group[], int n, int g_old, int g_new)
{
	for (int i = 0; i < n; i++) {
		if (group[i] == g_old) group[i] = g_new;
	}
}