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<iostream>
#include<queue>
#include<algorithm>
using namespace std;
typedef pair<int,int> P;
typedef pair<int,P> T;

class uft {//union-find tree
public:
    int parent[101];
    int size[101];
    int root(int x);
    bool same(int x,int y);
    void unite(int x,int y);
};
int uft::root(int x){
    if(parent[x]==x){
        return x;
    }
    return parent[x] = root(parent[x]);
}
bool uft::same(int x,int y){
    if(root(x)==root(y)){
        return true;
    }else{
        return false;
    }
}
void uft::unite(int x,int y){
    x=root(x);
    y=root(y);
    if(x == y) return;
    parent[x] = y;
    size[y] = size[y]+size[x];
}

int solv(priority_queue<T, vector<T>, greater<T> > q,int N){
    uft u;
    for(int i=0;i<N;i++){
        u.parent[i]=i;
        u.size[i]=1;
    }
    int ret=0;
    while(u.size[u.root(0)] != N && !q.empty()){
        T edge = q.top(); q.pop();
        if(u.root((edge.second).first) != u.root((edge.second).second)){
            u.unite((edge.second).first,(edge.second).second);
            ret += edge.first;
        }
    }
    return ret;
}

int main(){
    int N;
    priority_queue<T, vector<T>, greater<T> > q;
    cin>>N;
    for(int i=0;i<N;i++){
        for(int j=0;j<N;j++){
            int a;
            cin>>a;
            if(a!=-1 && i<j){
                T d = T(a,P(i,j));
                q.push(d);
            }
        }
    }
    int ans=solv(q,N);
    cout<<ans<<endl;
    return 0;
}

C++: #include<iostream>


using namespace std;
int n;
int M[100][100];

int prim() {
	int u;
	int color[100];
	int d[100];
	int p[100];
	int mincost;
	for (int i = 0;i < n;i++) {
		color[i] = 0;
		d[i] =2001;
		p[i] = -1;
	}
	d[0] = 0;
	

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

		color[u] = 2;
		for (int v = 0;v < n;v++) {
			if (color[v] != 2 && M[u][v] != -1) {
				if (M[u][v] < d[v]) {
					d[v] = M[u][v];
					p[v] = u;
					color[v] = 1;
				}
			}
		}
	}
	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;
		}
	}
	cout << prim() << endl;

	return 0;
}

C++: 
#include <iostream>
using namespace std;
const int MAX = 100;
const int INFTY = 2000000;
const int WHITE = 0;
const int GRAY = 1;
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;
}

C++: #include<bits/stdc++.h>
using namespace std;

#define MAX 101
#define BLACK 0
#define GRAY 1
#define WHITE 2
#define INF (1<<30)

int graph[MAX][MAX];
int n;

// プロトタイプ宣言
int prim();
// ここまで

int main(){
	cin>>n;

	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			cin>>graph[i][j];
		}
	}
	cout<<prim()<<endl;


	return 0;
}




//---------------------- 
int prim(){
	int from;
	int color[MAX];
	int d[MAX];
	int p[MAX];

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

	while(1){
		int minCost=INF;
		from=-1;

		for(int i=0;i<n;i++){
			if(color[i]!=BLACK && d[i]<minCost){
				minCost=d[i];
				from=i;
			}
		}
		if(from == -1) break;

		color[from]=BLACK;
		for(int to=0;to<n;to++){
			if(color[to]!=BLACK && graph[from][to]!=-1){
				if(graph[from][to]<d[to]){
					d[to]=graph[from][to];
					p[to]=from;
					color[to]=GRAY;
				}
			}
		}
	}

	int sum=0;
	for(int i=0;i<n;i++){
		// if(p[i]!=-1){
		// 	sum += graph[i][ p[i] ];
		// }
		if(d[i]!=INF){
			sum+=d[i];
		}
	}

	return sum;
}

C++: #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <stack>
#include <vector>
#include <algorithm>
#include <cmath>
#include <climits>
#include <cstring>
#include <queue>
#include <string>

using namespace std;

const double PI = 3.14159265358979;

int G[101][101];
int visited[101];
int n;

int houxuan[101];
int father[101];

int prim() {

	for (int i = 0; i < n; i++)	{
		houxuan[i] = INT_MAX;
		father[i] = -1;
	}
	houxuan[0] = 0;

	int sum = 0;
	while (true) {

		int minx = INT_MAX;
		int u = -1;
		for (int i = 0; i < n; i++) {

			if (!visited[i] && houxuan[i] < minx) {
				minx = houxuan[i];
				u = i;
			}
		}
		if (u == -1) break;

		visited[u] = 1;
		sum += minx;

		for (int i = 0; i < n; i++) {
			if (!visited[i] && G[u][i] != -1) {
				if (houxuan[i] > G[u][i]) {
					houxuan[i] = G[u][i];
					father[i] = u;
				}
			}
		}
	}	

	return sum;
}

int main() {
	
	
	cin >> n;

	for (int i = 0; i < n; i++)	{
		for (int j = 0; j < n; j++)	{
			cin >> G[i][j];
		}
	}
	
	cout << prim() << endl;

	return 0;
}