Go: package main

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

type color int

const (
	white color = iota // 未訪問
	mst                // MSTとして確定
)

// 頂点
type node struct {
	matrix   []int // 隣接行列
	minToMst int   // MSTへの重み最小値
	parent   int   // 最小値の相手
	color    color
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	n := eGetInt(scanner)

	nodes := make([]node, n)
	for i := 0; i < n; i++ {
		nodes[i].matrix = eAtoi((strings.Fields(eGetline(scanner))))
		nodes[i].parent = -1
		nodes[i].minToMst = math.MaxInt16
		nodes[i].color = white
	}

	// 初回はnode[0]がMSTに入るように,minToMstを設定
	nodes[0].minToMst = 0
	for { // 全部チェックするまで

		// どの頂点を新たにMSTに取り込むか
		min := math.MaxInt16
		u := -1
		for i := 0; i < n; i++ {
			if nodes[i].color == mst {
				continue
			}
			if nodes[i].minToMst < min {
				u = i
				min = nodes[i].minToMst
			}
		}

		if u == -1 {
			break
		}
		// MSTに追加する頂点決定
		nodes[u].color = mst

		// MSTに属していない頂点それぞれについて,MSTとの最小の重みを更新
		for i := 0; i < n; i++ {
			if nodes[i].color == mst {
				continue
			}
			if nodes[i].matrix[u] == -1 {
				// 接してない
				continue
			}
			if nodes[i].matrix[u] < nodes[i].minToMst {
				// node[u]との重みが最小
				nodes[i].minToMst = nodes[i].matrix[u]
				nodes[i].parent = u
			}
		}
	}

	sum := 0
	for _, v := range nodes {
		// node[0]はparentが入っていない
		if v.parent != -1 {
			sum += v.matrix[v.parent]
		}
	}
	fmt.Println(sum)
}

func eGetInt(scanner *bufio.Scanner) int {
	t := eAtoi(strings.Fields(eGetline(scanner)))
	return t[0]
}

func eGetline(p *bufio.Scanner) string {
	if !p.Scan() {
		if err := p.Err(); err != nil {
			log.Fatal(err.Error())
		}
		return ""
	}
	return p.Text()
}

func eAtoi(s []string) []int {
	ints := make([]int, len(s))
	var err error
	for i, e := range s {
		ints[i], err = strconv.Atoi(e)
		if err != nil {
			log.Fatal(err.Error())
		}
	}
	return ints
}

C++: #include<iostream>
#include<algorithm>
#include<climits>
using namespace std;
enum Color{WHITE, GRAY, BLACK};

int n;
int **a;
Color *color;

int prim(){
        color[0] = BLACK;
        int sum=0;
        while(1){
                int p, minp=INT_MAX;
                for(int i=0;i<n;i++){
                        if(color[i]!=BLACK) continue;
                        for(int j=0;j<n;j++){
                                if(color[j]==BLACK||a[i][j]==-1) continue;
                                else if(color[j]==WHITE) color[j] = GRAY;
                                if(minp>a[i][j]){
                                        minp = a[i][j];
                                        p = j;
                                }
                        }
                }
                if(minp==INT_MAX) break;
                sum += minp;
                color[p] = BLACK;
        }
        return sum;
}

int main(){
        cin>>n;
        a = new int*[n];
        for(int i=0;i<n;i++) a[i] = new int[n];
        for(int i=0;i<n;i++){
                for(int j=0;j<n;j++){
                        cin>>a[i][j];
                }
        }
        color = new Color[n];
        for(int i=0;i<n;i++) color[i] = WHITE;

        cout<<prim()<<endl;

        delete a;
        return 0;
}

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

struct UnionFind {
	vector<int> data;
	UnionFind(int size) : data(size, -1) { }
	bool unionSet(int x, int y) {
		x = root(x); y = root(y);
		if (x != y) {
			if (data[y] < data[x]) swap(x, y);
			data[x] += data[y]; data[y] = x;
		}
		return x != y;
	}
	bool findSet(int x, int y) {
		return root(x) == root(y);
	}
	int root(int x) {
		return data[x] < 0 ? x : data[x] = root(data[x]);
	}
	int size(int x) {
		return -data[root(x)];
	}
};


struct Edge {
	int a, b, cost;

	Edge(int a, int b, int cost) : a(a), b(b), cost(cost) {}

	bool operator<(const Edge& o)const {
		return cost < o.cost;
	}
};

int main() {
	cin.tie(0); ios::sync_with_stdio(false);
	int n, Ans = 0; cin >> n;
	vector<Edge> es;
	for (int i = 1; i <= n;i++) {
		for (int j = 1; j <= n; j++) {
			int temp; cin >> temp;
			if (j < i)continue;
			if (temp != -1) {
				Edge e(i, j, temp);
				es.push_back(e);
			}
		}
	}
	sort(es.begin(), es.end());

	/*kruskal*/

	UnionFind uf(n+1);
	for (int i = 0; i < es.size();i++) {
		//cout << es[i].a << " " << es[i].b << " " << es[i].cost << endl;
		Edge& e = es[i];
		if (!uf.findSet(e.a, e.b)) {
			Ans += e.cost;
			uf.unionSet(e.a, e.b);
		}
	}

	cout << Ans << endl;

}

C++: #include <bits/stdc++.h>
#define rep(i,a) for(i=0;i<a;i++)
using namespace std;
static const int W = 0;
static const int G = 1;
static const int B = 2;

int prim(int n, vector<vector<int> > &M) {
    int i,u,min,total;
    vector<int> d(n);
    vector<int> p(n);
    vector<int> color(n);

    rep(i,n){
        d[i] = INT_MAX;
        p[i] = -1;
        color[i] = W;
    }
    d[0] = 0;
    while(true){
        min = INT_MAX;
        u = -1;
        rep(i,n){
            if(min > d[i] && color[i] != B) {
                u = i;
                min = d[i];
            }
        }
        if(u==-1) break;
        color[u] = B;
        rep(i,n){
            if(color[i] != B && M[u][i])
                if(d[i] > M[u][i]) {
                    d[i] = M[u][i];
                    p[i] = u;
                    color[i] = G;
                }
        }
    }
    rep(i,n){
        if(p[i] != -1) total += M[i][p[i]];
    }
    return total;
}

int main(void) {
    int i,j,n,total = 0;
    vector<vector<int> > M;
    cin >> n;
    M.resize(n);

    rep(i,n){
        M[i].resize(n);
    }
    rep(i,n){
        rep(j,n){
            cin >> M[i][j];
            if(M[i][j] == -1) M[i][j] = INT_MAX;;

        }
    }
    total = prim(n,M);
    cout << total << endl;
    return 0;
}

C++: #include <iostream>

using namespace std;

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

int M[MAX][MAX];
int n;

int prim(){
    int u,minv;
    int color[MAX],d[MAX],p[MAX];

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

    d[0]=0;

    while(true){
        minv=INFTY;
        u=-1;
        for(int i=0;i<n;i++){
            if(minv>d[i] and 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 and 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;
}