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 <string>
#include <algorithm>
#include <functional>
#include <vector>
#include <utility>
#include <cstring>
#include <iomanip>
#include <numeric>
#include <cmath>
#include <queue>
#define MAX_N 110
using namespace std;
typedef long long ll;
const int INF = 1<<30; 
const int MOD = 1e9 + 7;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
int cost[MAX_N][MAX_N];
int mincost[MAX_N];
bool used[MAX_N];
int n;
int prim()
{
    int res = 0;
    mincost[0] = 0;
    while(true)
    {
        int v = -1;
        for(int u = 0; u < n; u++)
        {
            if(!used[u] && (v == -1 || mincost[u] < mincost[v])) v = u;
        }
        if(v == -1) break;
        used[v] = true;
        res += mincost[v];
        for(int u = 0; u < n; u++) mincost[u] = min(mincost[u], cost[v][u]);
    }
    return res;
}
int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    cin >> n;
    memset(used, false, sizeof(used));
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < n; j++)
        {
            cin >> cost[i][j];
            if(cost[i][j] == -1) cost[i][j] = INF;
        }
        mincost[i] = INF;
    }
    int ans = prim();
    cout << ans << endl;
    return 0;
}

C++: #include <iostream>
#include <queue>
#include <algorithm>
#include <vector>


using namespace std;


using ll = long long;


const ll MAX = 100;


ll adj[MAX][MAX];
ll d[MAX] = {};
bool finished[MAX] = {};
ll n;


void msp()
{
    priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> pq;
    pq.push(make_pair(0, 0));

    while (!pq.empty()) {
        auto p = pq.top(); pq.pop();
        if (finished[p.second])
            continue;

        for (ll j = 0; j < n; j++) {
            if (adj[p.second][j] == -1 || finished[j])
                continue;
            pq.push(make_pair(adj[p.second][j], j));
        }
        // cout << p.second << ": " << p.first << endl;
        d[p.second] = p.first;
        finished[p.second] = true;
    }
}


int main()
{
    cin >> n;
    for (ll i = 0; i < n; i++) {
        for (ll j = 0;j < n; j++) {
            cin >> adj[i][j];
        }
    }

    msp();

    ll acc = 0;
    for (ll i = 0; i < n; i++)
        acc += d[i];
    cout << acc << endl;
}

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

const int inf = 100000;
const int NIL = -1;

int G[100][100], d[100];
bool color[100];

int main(){
    int n, minc, tag, ans = 0;
    cin >> n;
    for(int i = 0; i < n; i++){
        d[i] = inf;
        color[i] = false;
        for(int j = 0; j < n; j++) cin >> G[i][j];
    }
    d[0] = 0;
    while(true){
        minc = inf;
        for(int i = 0; i < n; i++){
            if(color[i] == false && d[i] < minc){
                minc = d[i];
                tag = i;
            }
        }
        if(minc == inf) break;
        ans += minc;
        color[tag] = true;
        for(int v = 0; v < n; v++){
            if(G[tag][v] != -1){
                if(color[v] == false && G[tag][v] < d[v]){
                    d[v] = G[tag][v];
                }
            }
        }
    }
    cout << ans << endl;
    return 0;
}

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

#define NIL -2
#define INF 1000000001

using namespace std;

void prim(const vector<vector<int> > &link_weight,vector<int> &color, vector<int> &mincost_array,vector<int> &parents){
    int n = mincost_array.size();
    int mincost;
    int u;
    while(true){
        mincost = INF;
        for(int i = 0;i < n;i++){
            if(color[i] != 1 && mincost_array[i] < mincost){
                mincost = mincost_array[i];
                u = i;      
            }
        }
        if(mincost == INF) break;
        color[u] = 1;

        for(int v = 0;v < n;v++){
            if(color[v] != 1 && link_weight[u][v] < INF){
                if(link_weight[u][v] < mincost_array[v]){
                    mincost_array[v] = link_weight[u][v];
                    parents[v] = u;
                    color[v] = 0;
                }
            }
        }
    }
}

int main(){
    int mat_size;
    int temp;
    int cost = 0;

    scanf("%d",&mat_size);
    vector<vector<int> > link_weight(mat_size,vector<int>(mat_size,INF));
    vector<int> color(mat_size,-1);
    vector<int> mincost_array(mat_size,INF);
    vector<int> parents(mat_size,-1);

    for(int i = 0;i < mat_size;i++){
        for(int j = 0;j < mat_size;j++){
            scanf("%d",&temp);
            if(temp != -1) link_weight[i][j] = temp;
        }
    }
    mincost_array[0] = 0;
    prim(link_weight,color,mincost_array,parents);
    for(int i = 0;i < mat_size;i++) cost += mincost_array[i];
    printf("%d\n",cost);

    return 0;   
}

C++: #include <bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ull;
#define reps(i,s,n) for(int (i)=(s);(i)<(n);++(i))
#define rep(i,n) reps(i,0,n)
#define reptr(i,n) for(int (i)=(n);(i)>=0;--(i))
#define All(x) (x).begin(),(x).end()
#define mp make_pair
const ll MOD=1e9+7;
const int MAX = 100;
const int BLACK = 2;
const int GRAY = 1;
const int WHITE = 0;
using namespace std;

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

void prim(){
  int color[MAX];
  //d[i] : 自集合からの点iへの最短距離
  int d[MAX];
  //p[i]=s iの親はs
  int p[MAX];
  rep(i,n){
    color[i] = WHITE;
    d[i] = INT_MAX;
    p[i] = -1;
  }
  d[0] = 0;

  while(1){
    int u = -1,minv = INT_MAX;
    rep(i,n){
      if(color[i] != BLACK && minv > d[i]){
        u = i;
        minv = d[i];
      }
    }
    if(u == -1) break;
    color[u] = BLACK;
    rep(v,n){
      if(M[u][v] != INT_MAX && color[v] != BLACK){
        if(d[v] > M[u][v]){
          d[v] = M[u][v];
          p[v] = u;
          color[v] = GRAY;
        }
      }
    }
  }
  int sum = 0;
  rep(i,n){
    if(p[i] != -1) sum += M[i][p[i]];
  }
  cout << sum << endl;
}

int main(){
  cin >> n;
  rep(i,n){
    rep(j,n){
      int tmp;
      cin >> tmp;
      M[i][j] = (tmp == -1) ? INT_MAX : tmp;
    }
  }
  prim();
}