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 <bits/stdc++.h>
#define MOD 1000000007
#define INF 1<<30
#define rep(i,n) for(ll i=0; i<(n); i++)
#define REP(i,a,b) for(ll i=(a); i<(b); i++)
#define RREP(i,a,b) for(ll i=(a); i>=(b); i--)
#define all(x) (x).begin(),(x).end()
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef pair<ll, ll> p;
typedef vector<pair<ll, ll>> vpl;

vvl m(105, vl(105,INF));
ll n;

ll prim(){
    ll u, minv=INF;
    vl visit(105,0);
    vl d(105,INF);
    vl p(105,-1);

    d[0] = 0;
    while(1){
        minv = INF;
        rep(i,n){
            if(d[i] < minv && visit[i] == 0){
                minv = d[i];
                u = i;
            }
        }
        if(minv == INF) break;
        visit[u] = 1;
        rep(i,n){
            if(visit[i] == 0 && m[u][i] != INF){
                if(d[i] > m[u][i]){
                    d[i] = m[u][i];
                    p[i] = u;
                }
            }
        }
    }
    ll sum=0;
    rep(i,n){
        if(p[i] != -1) sum += d[i];
    }
    return sum;
}

int main(){
    ll t;
    cin >> n;
    rep(i,n){
        rep(j,n){
            cin >> t;
            m[i][j] = (t==-1) ? INF : t;
        }
    }

    cout << prim() << "\n";

    return 0;
}

C++: #include<iostream>
#include<vector>
#include<utility>
using namespace std;
enum COLOR{WHITE, GRAY, BLACK};

int main(){
    int n;
    cin >> n;
    vector<vector<int> > G(n, vector<int>(n));
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cin >> G[i][j];
            if (G[i][j] == -1) G[i][j] = 1e8;
        }
    }
    vector<COLOR> COL(n, WHITE);
    COL[0] = GRAY;
    int sum = 0;
    while (1) {
        pair<int, int> min_bridge = make_pair(0, 0);
        for (int i = 0; i < n; i++) {
            if (COL[i] != GRAY) continue;
            bool flag = true;
            for (int j = 0; j < n; j++) {
                if (COL[j] != WHITE) continue;
                flag = false;
                if (G[i][j] < G[min_bridge.first][min_bridge.second]) {
                    min_bridge.first  = i;
                    min_bridge.second = j;
                }
            }
            if (flag) COL[i] = BLACK;
        }
        if (min_bridge.first == 0 && min_bridge.second == 0) break;
        COL[min_bridge.second] = GRAY;
        sum += G[min_bridge.first][min_bridge.second];
    }
cout << sum << endl;
    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define int long long
#define double long double
#define SZ(x) ((int)(x).size())
#define rep(i,n) for(int i=0;i<(n);++i)
#define fore(i,a) for(auto &i:a)
#define min(...) min({__VA_ARGS__})
#define max(...) max({__VA_ARGS__})
#define Yes cout << "Yes" << endl;
#define No cout << "No" << endl;
#define ALL(x) (x).begin(),(x).end()
signed _main(); signed main() { cin.tie(0); ios::sync_with_stdio(false); _main(); }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> using v = vector<T>;
template<class T> using vv = vector<v<T>>;
const int MOD=1e9+7;
const long long INF = 1LL << 60;
using P=pair<int,int>;


signed _main(){
  int n; cin >> n;
  vv<int> M(n,v<int>(n));
  rep(i,n)rep(j,n) cin >> M[i][j];
  v<int> color(n,0),d(n,INF),p(n,-1);
  d[0]=0;//TとV-Tをつなぐ辺で最も重みが小さいものの大きさ
  int u,minv,sum=0;

  while(1){
    minv=INF;
    u=-1;//確定した頂点
    rep(i,n){
      if(minv > d[i] && color[i] != 2){
        u=i;
        minv = d[i];
      }
    }
    if(u == -1) break;//更新無し
    color[u]=2;
    rep(v,n){
      if(color[v]!=2 && M[u][v]!=-1){
        if(d[v]>M[u][v]){
          d[v]=M[u][v];
          p[v]=u;
          color[v]=1;
        }
      }
    }
  }

  rep(i,n){
    if(p[i]!=-1) sum+=M[i][p[i]];
  }

  cout << sum << endl;

  return 0;
}

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

#define WHITE 0
#define GRAY 1
#define BLACK 2
#define INF 2000000000

int x[10000][10000];
int color[10000];
int MIN[10000];
int sum;
int sum_min = INF;

int main() {
	int n, a, b, c;
	cin >> n;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			cin >> x[i][j];
		}
	}
	for (int i = 0; i < n; i++) {
		memset(MIN, 1, sizeof(MIN));
		b = INF;
		a = 1;
		color[i] = GRAY;
		MIN[i] = 0;
		while (a >= 1) {
			for (int j = 0; j < n; j++) {
				if (color[j] = GRAY) {
					if (b > MIN[j]) {
						b = MIN[j];
						c = j;
					}
				}
			}
			color[c] = BLACK;
			a -= 1;
			for (int j = 0; j < n; j++) {
				if (color[j] != BLACK && x[c][j] != -1) {
					if (color[i] == WHITE) {
						a += 1;
					}
					color[j] = GRAY;
					MIN[j] = min(x[c][j], MIN[j]);
				}
			}
		}
		sum = 0;
		for (int j = 0; j < n; j++) {
			sum += MIN[j];
		}
		sum_min = min(sum_min, sum);
	}
	cout << sum_min << endl;
	return 0;
}