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

C++: #include <iostream>
#include <string>
#include <vector>
#include <stdio.h>
#include <math.h>
#include <map>
#include <algorithm>

using namespace std;

#define FAR INT_MAX
#define N 100

static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;


int n, color[N], d[N], p[N];
int m[N][N];

int prim(){
	for (int i = 0; i < n; ++i){
		color[i] = WHITE;
		d[i] = FAR;
		p[i] = -1;
	}

	d[0] = 0;
	int dist = FAR;

	while (1)
	{
		int u = -1;
		dist = FAR;
		for (int i = 0; i < n; ++i) {
			if (color[i] != BLACK && d[i] < dist){
				dist = d[i];
				u = i;
			}
		}

		if (-1 == u) break;
		color[u] = BLACK;

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

int main(){
	cin >> n;
	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < n; ++j) {
			int val; cin >> val;
			m[i][j] = (val == -1) ? FAR : val;
		}
	}
 
	cout << prim() << endl;

	return 0;
}

C++: #include <iostream>
using namespace std;

static const int MAX = 100;
static const int INFTY = (1<<21);
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;

int n,M[MAX][MAX];

int prim() {
	int u,minv;
	int d[MAX],p[MAX],color[MAX];
	
	for(int i=0;i<n;i++){
		d[i] = INFTY;
		p[i] = -1;
		color[i]=WHITE;
	}
	
	d[0]=0;
	
	while(1){
		minv=INFTY;
		u=-1;
		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;
		for(int 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;
				}
			}
		}
	}
	
	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)?INFTY:e;
		}
	}
	
	cout<<prim()<<endl;
	
	return 0;
}