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++: #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;
}

C++: #include <algorithm>
#include <climits>
#include <iostream>
#include <string>
#include<cstdio>
#include <vector>
#include<queue>
#include<limits.h>
#include<string.h>
#include<functional>
#include<map>
#include<set>
#include<list>
#include<unordered_map>
#include<queue>
#define P pair<int,int>
using namespace std;

int a[100][100];
bool used[100];
signed main(){
	int b; cin >> b;
	for (int c = 0; c < b; c++) {
		for (int d = 0; d < b; d++) {
			cin >> a[c][d];
			if (a[c][d] == -1) {
				a[c][d] = INT_MAX / 3;
			}
		}
	}
	int ans = 0;
	priority_queue<P,vector<P>, greater<P>>Q;
	Q.push({ 0,0 });
	while (Q.size()) {
		P t = Q.top(); Q.pop();
		if (!used[t.second]) {
			ans += t.first;
			used[t.second] = true;
			for (int i = 0; i < b; i++) {
				Q.push({ a[t.second][i],i });
			}
		}
	}
	cout << ans << endl;
}

C++: #include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;

int M[200][200];
string color[200];
int d[200];
int p[200];
int n;
const int INF = 1001001001;

void prim(){
    rep(i, n){
        d[i] = INF;
        color[i] = "WHITE";
    }
    d[0] = 0;
    p[0] = -1;

    int u;

    while(true){
        int mincost = INF;
        for (int i = 0; i < n; i++){
            if(color[i] != "BLACK" && d[i] < mincost){
                mincost = d[i];
                u = i;
            }
        }
        if(mincost == INF){
            break;
        }
        color[u] = "BLACK";

        for (int i = 0; i < n; i++){
            if(color[i] != "BLACK" && M[u][i] != -1){
                if(M[u][i] < d[i]){
                    d[i] = M[u][i];
                    p[i] = u;
                    color[i] = "GRAY";
                }
            }
        }
    }
}

int main() {
    cin >> n;
    rep(i, n){
        rep(j, n){
            cin >> M[i][j];
        }
    }
    prim();
    int ans = 0;
    rep(i, n){
        if(d[i] != INF){
            ans += d[i];
        }
    }
    cout << ans << endl;

    return 0;
}

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

class DisjointSet
{
  private:
    std::vector<int> P;
    int root(int a);

  public:
    DisjointSet(int N);
    ~DisjointSet();
    void unite(int a, int b);
    bool is_same_set(int a, int b);

    void show_all(int a);
};

DisjointSet::DisjointSet(int N)
{
    for (int i = 0; i <= N; ++i)
    {
        P.push_back(i);
    }
}

// a の木の root の値を返す
int DisjointSet::root(int a)
{
    if (P[a] == a)
        return a;
    return (P[a] = root(P[a]));
}

// a の属する木を b の木に合体する
void DisjointSet::unite(int a, int b)
{
    P[root(a)] = root(b);
    return;
}

// a と b が同じ集合に属しているかどうか
bool DisjointSet::is_same_set(int a, int b)
{
    return root(a) == root(b);
}

// デバッグ用：a から root までの値をすべて表示する
void DisjointSet::show_all(int a)
{
    if (P[a] != a)
        show_all(P[a]);
    std::cout << "P[" << a << "] = " << P[a] << std::endl;
    return;
}

//=========================

#include <iostream>
#include <vector>
#include <utility>
#include <algorithm>

using namespace std;
typedef pair<int, int> P;

int main()
{
    int n;
    cin >> n;
    vector<int> cost;
    vector<pair<int, P>> edges;

    for (int row = 0; row < n; row++)
    {
        for (int col = 0; col < n; col++)
        {
            int c;
            cin >> c;
            if (row > col && c > -1)
            {
                edges.push_back(
                    make_pair(
                        c, make_pair(row, col)));
            }
        }
    }

    sort(edges.begin(), edges.end());
    DisjointSet *tree = new DisjointSet(n);

    int ans = 0;
    for (int i = 0, size = edges.size(); i < size; i++)
    {
        int cost = edges[i].first;
        int src = edges[i].second.first;
        int dst = edges[i].second.second;

        if (!tree->is_same_set(src, dst))
        {
            tree->unite(src, dst);
            ans += cost;
        }
    }
    cout << ans << endl;
    return 0;
}