Go: package main

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

const (
	WHITE int = iota
	GRAY
	BLACK
)

type graph struct {
	G [][]int
	C []int
	D []int
	P []int
}

var INFTY = 1 << 32

func constructGraph() *graph {
	sc := bufio.NewScanner(os.Stdin)
	sc.Scan()
	n, _ := strconv.Atoi(sc.Text())

	g := make([][]int, n)
	c := make([]int, n)
	d := make([]int, n)
	p := make([]int, n)

	for i := 0; i < n; i++ {
		sc.Scan()
		fields := strings.Fields(sc.Text())
		g[i] = toIntSlice(fields)
	}

	return &graph{g, c, d, p}
}

func (g *graph) initMst() {
	for i := 0; i < len(g.G); i++ {
		g.C[i] = WHITE
		g.D[i] = INFTY
	}
	g.D[0] = 0
	g.P[0] = -1
}

func (g *graph) primMst() int {
	g.initMst()

	var u int
	for {
		mincost := INFTY
		for i := 0; i < len(g.G); i++ {
			if g.C[i] != BLACK && g.D[i] < mincost {
				mincost = g.D[i]
				u = i
			}
		}

		if mincost == INFTY {
			break
		}

		g.C[u] = BLACK

		for i := 0; i < len(g.G); i++ {
			if g.C[i] != BLACK && g.existEdge(u, i) {
				if g.G[u][i] < g.D[i] {
					g.D[i] = g.G[u][i]
					g.P[i] = u
				}
			}
		}
	}

	totalCost := 0
	for _, cost := range g.D {
		totalCost += cost
	}
	return totalCost
}

func (g *graph) existEdge(u, v int) bool {
	return g.G[u][v] != INFTY && g.G[v][u] != INFTY
}

func toIntSlice(s []string) []int {
	a := make([]int, len(s))
	for i, v := range s {
		num, _ := strconv.Atoi(v)
		if num == -1 {
			num = INFTY
		}
		a[i] = num
	}
	return a
}

func main() {
	g := constructGraph()
	fmt.Println(g.primMst())
}

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