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 <stdio.h>
#define Max 100
#define Maxi 2147483647
#define W  0
#define X  1
#define Z  2

int n,m[Max][Max];
int prim(){
  int i,u,minv;
  int d[Max],p[Max],c[Max];

  for(i=0;i<n;i++){
    d[i] = Maxi;
    p[i] = -1;
    c[i] = W;
  }

  d[0] = 0;
  while(1){
    minv = Maxi;
    u = -1;
    for(i = 0;i < n; i++){
      if(minv > d[i] && c[i] != Z){
        u = i;
        minv = d[i];
      }
    }
    if( u == -1) break;
    c[u] = Z;
    for(i = 0;i<n;i++){
      if(c[i]!= Z && m[u][i]!= Maxi){
        if(d[i] > m[u][i]){
          d[i] = m[u][i];
          p[i] = u;
          c[i] = X;
        }
      }
    }
  }
  int sum = 0;
  for(i = 0; i < n; i++){
    if(p[i]!= -1){
      sum += m[i][p[i]];
    }
  }
  return sum;
}

int main(){

  scanf("%d",&n);
  int i,j,e;
  for(i = 0;i<n;i++){
    for(j = 0;j<n;j++){
      scanf("%d",&e);
      if(e == -1){
        m[i][j] = Maxi;
      }
      else m[i][j] = e;
    }
  }
  printf("%d\n", prim());
  return 0;

}

C++: #include <bits/stdc++.h>
using namespace std;
using Int = int_fast64_t;

class edge{
public:
	Int cost;
	size_t from, to;
	edge(Int c, size_t f, size_t t){
		cost = c;
		from = f;
		to = t;
	}
};
typedef vector<edge> edges;
typedef vector<edges> graph;
class unionfind{ // 0-indexed
public:
	vector<int> parent;
	vector<size_t> size;
	size_t num_of_disjointsets;
	unionfind(size_t n){
		parent.resize(n, -1);
		size.resize(n, 1);
		num_of_disjointsets = n;
	}

	size_t find(size_t x){
		if(parent[x] == -1) return x;
		return parent[x] = find(parent[x]);
	}

	size_t unite(size_t x, size_t y){
		x = find(x);
		y = find(y);
		if(x == y) return -1;
		if(size[x] < size[y]){
			parent[x] = y;
			size[y] += size[x];
			size[x] = 0;
			--num_of_disjointsets;
			return y;
		}else{
			parent[y] = x;
			size[x] += size[y];
			size[y] = 0;
			return x;
			--num_of_disjointsets;
		}
	}

	bool united(size_t x, size_t y){
		return find(x) == find(y);
	}
};
bool operator < (const edge &e, const edge &f){
	return e.cost != f.cost ? e.cost > f.cost : e.from != f.from ? e.from < f.from : e.to < f.to;
}
vector<edge> kruskal(graph &g){
	priority_queue<edge> pq;
	for(size_t i=0; i<g.size(); ++i)
		for(size_t j=0; j<g[i].size(); ++j)
			if(i < g[i][j].to)
				pq.push(g[i][j]);
	unionfind uf(g.size());
	vector<edge> res;
	while(!pq.empty()){
		edge e = pq.top(); pq.pop();
		if(!uf.united(e.from, e.to)){
			uf.unite(e.from, e.to);
			res.push_back(e);
		}
		if(uf.num_of_disjointsets == 1) break;
	}
	return res;
}

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	Int n; cin >> n;
	graph g(n);
	for(Int i=0; i<n; ++i)
		for(Int j=0; j<n; ++j){
			Int a; cin >> a;
			if(a != -1){
				g[i].push_back(edge(a, i, j));
				g[j].push_back(edge(a, j, i));
			}
		}
	vector<edge> mst = kruskal(g);
	Int ans = 0;
	for(auto i:mst) ans += i.cost;
	cout << ans << "\n";
}

C++: #include <iostream>
#include <vector>
#include <utility>
#include <algorithm>
using namespace std;
int n;
int A[110][110];
int P[10010] = {0};
void init(int N){
	for(int i=0;i<=N;++i) P[i] = i;
}

int root(int a){
	if(P[a] == a) return a;
	return (P[a] = root(P[a]));
}

bool is_same_set(int a,int b){
	return root(a) == root(b);
}

void unite(int a, int b){
	P[root(a)] = root(b);
}


int main(){
	while(cin >> n && n>0){
		init(n);
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				cin >> A[i][j];
			}
		}
		int cost[10000],src[10000],dst[10000];
		vector< pair<int,int> > edges;
		for(int r=0;r<n;r++){
			for(int c=r+1;c<n;c++){
				if(A[r][c] > -1){
					cost[M] = A[r][c];
					src[M] = r;
					dst[M] = c;
					//cout << cost[M] << endl;
					pair<int,int> edge = make_pair(cost[M],M);
					edges.push_back(edge);
					M++;
				}
			}
		}
		sort(edges.begin(),edges.end());
		long long  sum = 0;
		for(int i=0;i<M;i++){
			int n1 = src[edges[i].second],n2 = dst[edges[i].second];
			if(!is_same_set(n1,n2)){
				unite(n1,n2);
				sum += edges[i].first;
			}
		}
		cout << sum << endl;
	}
}

#if 0
所要時間は45分程度。クラスカル法を回す部分の実装に少し苦戦した。
#endif

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

using Edge = std::pair<uint32_t, uint32_t>;
using Graph = std::vector<std::vector<Edge>>;

int main() {
  uint32_t n = 0;
  std::cin >> n;
  Graph G(n);
  for (size_t i = 0; i < n; i++) {
    for (size_t j = 0; j < n; j++) {
      int32_t cost = 0;
      std::cin >> cost;
      if (cost != -1) G[i].push_back({j, cost});
    }
  }

  std::vector<uint32_t> min_cost(n, -1);
  min_cost[0] = 0;
  std::vector<bool> seen(n, false);
  while (!std::all_of(seen.begin(), seen.end(), [](bool v) { return v; })) {
    uint32_t current = -1, cost = -1;
    for (size_t i = 0; i < n; i++) {
      if (!seen[i] && (cost > min_cost[i])) {
        current = i;
        cost = min_cost[i];
      }
    }

    seen[current] = true;
    for (auto &&node : G[current]) {
      if (seen[node.first]) continue;
      min_cost[node.first] = std::min(node.second, min_cost[node.first]);
    }
  }

  std::cout << std::accumulate(min_cost.begin(), min_cost.end(), 0)
            << std::endl;

  return 0;
}