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 <vector>
#include <string>
#include <algorithm>
#include <utility>

using namespace std;

#define REP(var, count) for(int var=0; var<count; var++)

struct unionf {
	
	vector<int> data;
	
	unionf(int n) {
		REP(i, n+1) { data.push_back(i); }
	}
	
	int root(int a) {
		if(data[a] == a) { return a; }
		return (data[a] = root(data[a]));
	}
	
	bool issame(int a, int b) {
		return root(a) == root(b);
	}
	
	void unite(int a, int b) {
		data[root(a)] = root(b);
	}

};

int main(void) {
	int N;
	cin >> N;
	
	vector<pair<int, pair<int, int> > > edge;
	
	REP(x, N) {
		REP(y, N) {
			int c;
			cin >> c;
			if ( x <= y ) { continue; }
			if ( c == -1 ) { continue; }
			edge.push_back( make_pair(c, make_pair(x,y)) );
		}
	}
	
	sort(edge.begin(), edge.end());
	
	unionf uf(N);
	
	int cnt = 0;
	int ret = 0;
	
	REP(i, edge.size() ) {
		
		pair<int, int> sd = edge[i].second;
		if ( ! uf.issame( sd.first, sd.second ) ) {
			cnt ++;
			ret += edge[i].first;
			uf.unite(sd.first, sd.second);
			//cout << "joint " << sd.first << " " << sd.second << " -> " << edge[i].first << endl;
		}
		
		if ( cnt == N-1 ) { break; }
	}
	
	cout << ret << endl;
}

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

// =======================
// ------ graph ------ //
using Weight = int;
using Flow = int;
struct Edge {
    int src, dst;
    Weight weight;
    Flow cap;
    Edge() : src(0), dst(0), weight(0) { }
    Edge(int s, int d, Weight w) : src(s), dst(d), weight(w) { }
};
bool operator < (const Edge &e, const Edge &f) {
    return e.weight != f.weight ? e.weight < f.weight :
        e.src != f.src ? e.src < f.src : e.dst < f.dst;
}
using Edges = vector<Edge>;
using Graph = vector<Edges>;

void add_edge1(Graph &g, int a, int b, Weight w = 1) {
    g[a].emplace_back(a, b, w);
}

// ------ disjoint set ------ //
struct UnionFind {
    vector<int> data;
    int __size;
    UnionFind(int size) : data(size, -1), __size(size) { }
    bool unionSet(int x, int y) {
        if ((x = root(x)) != (y = root(y))) {
            if (data[y] < data[x]) swap(x, y);  // par -> child とは限らない
            data[x] += data[y]; data[y] = x; __size--;
        }
        return x != y;
    }
    bool findSet(int x, int y) { return root(x) == root(y); }
    int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
    int size(int x) { return -data[root(x)]; }
    int size() { return __size; }
    vector<int> subset_roots() {
        vector<int> roots;
        for (int i = 0; i < data.size(); i++) if (data[i] < 0) roots.push_back(i);
        return roots;
    }
};

// ------ kruskal ------ //
pair<Weight, Edges> kruskal(const Graph &g) {
    UnionFind uf(g.size());
    Edges es;
    for (auto &adj : g) for (auto &e : adj) es.emplace_back(e);
    sort(es.begin(), es.end());
    Weight total = 0;
    Edges T;
    for (auto &e : es) if (uf.unionSet(e.src, e.dst)) {
        T.push_back(e);
        total += e.weight;
    }
    return make_pair(total, T);
}
// =======================

int main() {
    int n; cin >> n;
    Graph G(n);
    for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) {
        int w; cin >> w;
        if (w >= 0) add_edge1(G, i, j, w);
    }
    pair<Weight, Edges> res = kruskal(G);
    cout << res.first << endl;
    return 0;
}

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

const int MAX = 100;

int count(bool check[MAX], int graph[MAX][MAX], int n,int i) {
	check[i] = true;
	int sum = 0;
	for (int j = 0; j < n; j++) {
		if (graph[i][j] != -1 && check[j] == false) {
			sum += count(check,graph, n, j);
			sum += graph[i][j];
		}
	}
	return sum;
}

int main() {
	int n;
	int g;
	int graph[MAX][MAX];

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

	//?????±???
	for (int m = 0; m < n; m++) {
		//?§????
		for (int b = 0; b < n; b++) {
			if (graph[b][m] == -1)continue;
			//??????
			for (int e = 0; e < n; e++) {
				if (graph[m][e] == -1 || graph[b][e] == -1)continue;
				if (graph[b][e] >= graph[b][m] + graph[m][e]) {
					graph[b][e] = -1;
				}
			}
		}
	}

	bool check[MAX] = { false };
	cout << count(check, graph, n, 0) << endl;
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <stack>
#include <queue>
#include <cmath>
#include <algorithm>
using namespace std;

void solve(){
  int n; cin >> n;
  vector<vector<int> > v(n, vector<int>(n));
  for(int i = 0; i < n; i++){
    for(int j = 0; j < n; j++){
      cin >> v[i][j];
    }
  }
  vector<int> t, c(n);
  t.push_back(0);
  c[0] = 1;
  int ans = 0;
  while(t.size() != n){
    int mini, minj, mint = 100000;
    for(int i = 0; i < t.size(); i++){
      for(int j = 0; j < n; j++){
        if(v[t[i]][j] >= 0 && c[j] == 0){
          if(v[t[i]][j] < mint){
            mini = i;
            minj = j;
            mint = v[t[i]][j];
          }
        }
      }
    }
    ans += mint;
    t.push_back(minj);
    c[minj] = 1;
  }
  cout << ans << endl;
  return;
}

int main(){
  solve();
  return 0;
}