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>
using namespace std;
#include<cstdio>
#include<queue>

class union_find  // union find.
{
    int* U;
public:
    union_find(int n){ U = new int [n]; for(int i = 0; i < n; i++) U[i] = i; }
    int root(int a){ if(U[a] == a){ return a; } U[a] = root(U[a]); return U[a]; }
    bool same(int a, int b){ return root(a) == root(b); }
    void join(int a, int b){ int x = root(a), y = root(b); U[x] = y; }
    ~union_find(){ delete [] U; }
};

struct edge{
    int v1, v2, w;
    bool operator < (const edge &e) const{ return w > e.w; }  // const?????????????????????
};

edge init(int ver_1, int ver_2, int weight)
{
	edge e;
	e.v1 = ver_1, e.v2 = ver_2, e.w = weight;
	return e;
}

int main()
{
	int i, j, w, n;
	scanf("%d", &n);

	priority_queue<edge> ES;
	union_find VS(n);

	for(i = 0; i < n; i++){
		for(j = 0; j < n; j++){
			scanf("%d", &w);
			if(i >= j || w < 0) continue;
			ES.push(init(i, j, w));
		}
	}
	int size = 0, sum = 0;
	edge e;
	while(!ES.empty() && size < n - 1){
		e = ES.top();
		ES.pop();
		if(!VS.same(e.v1, e.v2)){
			VS.join(e.v1, e.v2);
			sum += e.w;
			size++;
		}
	};
	printf("%d\n", sum);

	return 0;
}

C++: #include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a), i##_max = (b); i < i##_max; ++i)
#define RFOR(i, a, b) for (int i = (b)-1, i##_min = (a); i >= i##_min; --i)
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define RREP(i, n) for (int i = (n)-1; i >= 0; --i)
#define ALL(obj) (obj).begin(), (obj).end()

using namespace std;
using vi = vector<int>;
using vd = vector<double>;
using i64 = int64_t;

constexpr int INF = 1 << 30;
constexpr int MOD = 1000000007;

template <typename T>
void print(vector<T> &v) {
  REP(i, v.size()) {
    if (i) cout << " ";
    cout << v[i];
  }
  cout << endl;
}

enum Color { White, Black };

int prim(vector<vi> &M) {
  auto n = M.size();
  vi d(n, INF), p(n, -1);
  vector<Color> color(n, White);
  d[0] = 0;
  while (true) {
    auto minv = INF;
    auto u = -1;
    REP(i, n) {
      if (color[i] == White && d[i] < minv) {
        minv = d[i];
        u = i;
      }
    }
    if (u == -1) break;
    color[u] = Black;
    REP(i, n) {
      if (color[i] == White && M[u][i] != INF && M[u][i] < d[i]) {
        d[i] = M[u][i];
        p[i] = u;
      }
    }
  }
  int sum = 0;
  REP(i, n) if (p[i] != -1) sum += M[i][p[i]];
  return sum;
}

int main() {
  int n, e;
  cin >> n;
  vector<vi> M(n, vi(n, 0));
  REP(i, n) {
    REP(j, n) {
      cin >> e;
      M[i][j] = (e == -1) ? INF : e;
    }
  }

  auto ans = prim(M);
  cout << ans << endl;
}

C++: #include <cstdio>
using namespace std;
#define MAX_N (105)
#define INF (10000)
int n;
int G[MAX_N][MAX_N];
bool MST[MAX_N];
int DIST[MAX_N];		// MST??¨????????¢
int NEIGH[MAX_N];		// ??£??????

int main()
{
    scanf("%d", &n);
    for ( int i = 0; i < n; i++ ) {
	for ( int j = 0; j < n; j++ ) {
	    int d;
	    scanf("%d", &d);
	    G[i][j] = ( d == -1 ) ? INF : d;
	}
    }

    int r = 0;
    int size_mst = 1;

    MST[r] = true;
    
    for ( int i = 0; i < n; i++ ) {
	if ( i == r ) continue;
	DIST[i] = G[r][i];
	NEIGH[i] = r;
    }

    int ans = 0;

    while ( size_mst < n ) {
	int u = 0;
	int min_dist = INF;
	// ????°????????????¢???
	for ( int i = 0; i < n; i++ ) {
	    if ( !MST[i] ) {
		if ( DIST[i] < min_dist ) {
		    u = i;
		    min_dist = DIST[i];
		}
	    }
	}

	ans += min_dist;
	
	MST[u] = true;
	size_mst++;

	// MST??¨????????¢?????´??°
	for ( int i = 0; i < n; i++ ) {
	    if ( !MST[i] ) {
		if ( G[u][i] < DIST[i] ) {
		    DIST[i] = G[u][i];
		    NEIGH[i] = u;
		}
	    }
	}
    }

    printf("%d\n", ans);

    return 0;
}

C++: 
#include<bits/stdc++.h>

using namespace std;

const int maxn=1e2+5;
const int inf=(1<<21);
const int white=0;
const int gray=1;
const int black=2;

int n,m[maxn][maxn];

int prim()
{
    int d[maxn],p[maxn],book[maxn];
    for(int i=0;i<n;i++){
        d[i]=inf;
        p[i]=-1;
        book[i]=0;
    }
    d[0]=0;
    int u,minv,sum=0;
    while(1){
        minv=inf;
        u=-1;
        for(int i=0;i<n;i++){
            if(minv>d[i]&&book[i]==0){
                u=i;
                minv=d[i];
            }
        }
        if(u==-1){
            break;
        }else{
            sum+=d[u];
        }
        book[u]=1;
        for(int v=0;v<n;v++){
            if(book[v]==0&&d[v]>m[u][v]){
                d[v]=m[u][v];
                p[v]=u;
            }
        }
    }
//    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++){
            int e;
            cin>>e;
            m[i][j]=(e==-1)?inf:e;
        }
    }
    cout<<prim()<<endl;
    return 0;
}
/*
5
-1 2 3 1 -1
2 -1 -1 4 -1
3 -1 -1 1 1
1 4 1 -1 3
-1 -1 1 3 -1
*/