Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func main() {
	fp := os.Stdin
	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(os.Stdout)

	n := getNextInt(scanner)

	g := make([][]int, n)
	ans := make([][]int, n)

	inf := 1 << 30
	for i := 0; i < n; i++ {
		g[i] = make([]int, n)
		ans[i] = make([]int, n)
		for ii := 0; ii < n; ii++ {
			g[i][ii] = getNextInt(scanner)
			if g[i][ii] == -1 {
				g[i][ii] = inf
			}
			ans[i][ii] = -1
		}
	}

	im := make(IntMap, 1)

	im.increment(1)

	for len(im) < n {
		min := inf
		u := -1
		v := -1
		for i := range im {
			for ii := 0; ii < n; ii++ {
				if im.get(ii) != 0 {
					continue
				}
				if min > g[i][ii] {
					min = g[i][ii]
					u = i
					v = ii
				}
			}
		}
		im.increment(v)
		ans[u][v] = min
		ans[v][u] = min
	}

	sum := 0
	for i := 0; i < n-1; i++ {
		for ii := i + 1; ii < n; ii++ {
			if ans[i][ii] != -1 {
				sum += ans[i][ii]
			}
		}
	}

	fmt.Fprintln(writer, sum)
	writer.Flush()
}

// IntMap ...
type IntMap map[int]int

func (m IntMap) get(key int) int {
	if _, ok := m[key]; ok {
		return m[key]
	}
	return 0
}
func (m IntMap) increment(key int) {
	if _, ok := m[key]; ok == false {
		m[key] = 0
	}
	m[key]++
}

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

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 prim();

int n,M[MAX][MAX];

int main(){
  cin >> n;

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

  cout << prim() << endl;
  return 0;
}

int prim(){
  int u,minv,d[MAX],p[MAX],color[MAX];

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

  while(1){
    minv=INFTY;
    u = -1;
    for(int 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(int h=0;h<n;h++){
      if(color[h]!=BLACK && M[u][h]!=INFTY){
        if(d[h]>M[u][h]){
          d[h]=M[u][h];
          p[h]=u;
          color[h]=GRAY;
        }
      }
    }
  }
  int vv=0;
  for(int i=0;i<n;i++){
    if(p[i]!=-1) vv=vv+M[i][p[i]];
  }
  return vv;
}