Go: package main

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

const (
	white int = iota
	gray
	black
)
const INFTY int = 2001

var G [][]int
var color []int
var d []int
var p []int
var N int

func prim() int {
	u := 0
	d[u] = 0
	for {
		minCost := INFTY
		for 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 v:=0; v<N; v++ {
			if color[v] != black && G[u][v] != -1 {
				if G[u][v] < d[v] {
					d[v] = G[u][v]
					p[v] = u
					color[v] = gray
				}
			}
		}
	}
	// sum
	sum := 0
	for i:=0; i<N; i++ {
		if p[i] != -1 {
			sum += G[i][p[i]]
		}
	}
	return sum
}




func initVariable(N int) {
	G = make([][]int, N)
	for i:=0; i<N; i++ {
		G[i] = make([]int, N)
	}
	color = make([]int, N)
	d = make([]int, N)
	p = make([]int, N)
	for i:=0; i<N; i++ {
		d[i] = INFTY
		p[i] = -1
	}
}

func nextInt(scanner *bufio.Scanner) int {
	scanner.Scan()
	num, _ := strconv.Atoi(scanner.Text())
	return num
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	N = nextInt(scanner)
	initVariable(N)
	for i:=0; i<N; i++ {
		for j:=0; j<N; j++ {
			G[i][j] = nextInt(scanner)
		}
	}
	fmt.Printf("%d\n", prim())
}

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