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;
static const int MAX = 100;
static const int INFTY = (1<<21);
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;

int n,M[MAX][MAX];

int prim()
{
    int u,minv,i,v;
    int d[MAX],p[MAX],color[MAX];
    int sum = 0;

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

    d[0] = 0;

    while(1)
    {
        minv = INFTY;
        u = -1;

        for(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(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;
                }
            }
        }
    }

   

    for(i=0;i<n;i++)
    {
        if(p[i] != -1) sum += M[i][p[i]];
    }

    return sum;
}

int main()
{
    int i,j,e;
    cin >> n;

    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
        {
            cin >> e;
            M[i][j] = (e == -1) ? INFTY : e;
        }
    }

    cout << prim() << endl;

    return 0;
    
}

C++: #include <bits/stdc++.h>
#define rep(i, a, b) for(int i = a; i < b; i++)
#define Rep(i, a, b) for(int i = a; i <= b; i++)
#define _GLIBCXX_DEBUG
#define Vl vector<ll>
#define Vs vector<string>
#define Vp vector<pair<ll, ll>>
#define ll long long
#define ALL(v) (v).begin(),(v).end()
using namespace std;
const double pi = acos(-1.0);
const ll MOD = 1e9 + 7;
const ll INF = 1LL << 60;
using Graph = vector<vector<ll>>;

ll n;
ll M[110][110]; // kyori
const int white = 0, gray = 1, black = 2;

ll prim(){
  ll u, minv;
  ll d[10000], p[10000], color[10000];

  rep(i, 0, n){
    d[i] = INF;
    p[i] = -1; // parent
    color[i] = white;
  }
  d[0] = 0;
  while(1){
    minv = INF;
    u = -1;
    rep(i, 0, n){
      if(minv > d[i] && color[i] != black){
        u = i;
        minv = d[i];
      }
    }
    if(u == -1) break;
    color[u] = black;
    rep(v, 0, n){
      if(color[v] != black && M[u][v] != INF){
        if(d[v] > M[u][v]){
          d[v] = M[u][v];
          p[v] = u;
          color[v] = gray;
        }
      }
    }
  }
    ll sum = 0;
    rep(i, 0, n){
    if(p[i] != -1) sum += M[i][p[i]];
    }

    return sum;
}

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

  cout << prim() << endl;

  return 0;
}

C++: // ALDS1_12_A: Minimum Spanning Sort
#include <iostream>
#include <vector>
#include <algorithm>
#include <utility>
bool debug = false;
const int INF = 100000;
// const int BK = 0, GR = 1, WT = 2;

std::vector<std::vector<int>> adjacency_list;
std::vector<int> node_stats;
std::vector<std::pair<int, int>> weight_memo;

int PrimAlgorithm(int weight, int vertex) {
    node_stats.at(vertex) = 1;
    
    for (int i=0; i<adjacency_list.at(vertex).size(); i++) {
        int tmp_vertex = i;
        if (adjacency_list.at(vertex).at(tmp_vertex) != -1) {
            std::pair<int,int> tmp = std::make_pair(tmp_vertex, adjacency_list.at(vertex).at(tmp_vertex));
            weight_memo.push_back(tmp);
        }
    }
    
    int minimum_weight = INF, next_vertex = -1;
    for (int i=0; i<weight_memo.size(); i++) {
        int tmp_vertex = weight_memo.at(i).first, tmp_weight = weight_memo.at(i).second + weight;
        if (tmp_weight < minimum_weight) {    
            if (node_stats.at(tmp_vertex) == 0) {
                next_vertex = tmp_vertex;
                minimum_weight = tmp_weight;
            }
        }
    }
    if (debug) std::cout << "next_vertex = " << next_vertex << ", minimum_weight = " << minimum_weight << "\n";
    if (next_vertex == -1) {
        return weight;
    }
    return PrimAlgorithm(minimum_weight, next_vertex);
}

int main() {
    int total_num; std::cin >> total_num;
    adjacency_list.resize(total_num);
    for (int i=0; i<adjacency_list.size(); i++) adjacency_list.at(i).resize(total_num);
    for (int i=0; i<total_num; i++) {
        for(int j=0; j<total_num; j++) {
            std::cin >> adjacency_list.at(i).at(j);
        }
    }

    node_stats.resize(total_num);
    for (int i=0; i<node_stats.size(); i++) node_stats.at(i) = 0;

    int result = PrimAlgorithm(0, 0);
    std::cout << result << "\n";
}