Go: package main

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

const (
	MAX_N      = 200
	MAX_WEIGHT = 2000
)

type Edge struct {
	start  int
	end    int
	weight int
}

type ShortestInfo struct {
	edges    []Edge
	shortest int
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	n, err := nextInt(sc)
	if err != nil {
		panic(err)
	}

	shortestList := make([]ShortestInfo, n)
	for i := 1; i < n; i++ {
		shortestList[i] = ShortestInfo{edges: []Edge{}, shortest: MAX_N*MAX_WEIGHT + 1}
	}

	adjacentMatrix := make([][]int, n)
	for i := 0; i < n; i++ {
		adjacentMatrix[i] = make([]int, n)
		for j := 0; j < n; j++ {
			v, err := nextInt(sc)
			if err != nil {
				panic(err)
			}
			adjacentMatrix[i][j] = v
		}
	}

	mst([]int{1}, shortestList, adjacentMatrix)
	fmt.Println(calculateMinWeight(shortestList))
}

func calculateMinWeight(shortestList []ShortestInfo) int {
	visitedList := make([]bool, len(shortestList))
	visitedList[0] = true
	sumWeight := 0
	for !visitedAll(visitedList) {
		shortestInfo := shortestList[0]
		shortestList = shortestList[1:]
		edges := shortestInfo.edges
		for _, edge := range edges {
			if !visitedList[edge.end-1] {
				visitedList[edge.end-1] = true
				sumWeight += edge.weight
			}
		}
	}
	return sumWeight
}

func visitedAll(visitedList []bool) bool {
	for _, v := range visitedList {
		if !v {
			return false
		}
	}
	return true
}

func mst(nodeStack []int, shortestList []ShortestInfo, adjacentMatrix [][]int) {
	for len(nodeStack) > 0 {
		currentNode := nodeStack[0]
		nodeStack = nodeStack[1:]
		currentShortest := getShortest(currentNode, shortestList)
		adjacents := findAdjacents(currentNode, adjacentMatrix)
		for i := 0; i < len(adjacents); i++ {
			adj := adjacents[i]
			s := getShortest(adj.end, shortestList)
			if currentShortest.shortest+adj.weight < s.shortest || (currentShortest.shortest+adj.weight == s.shortest && len(currentShortest.edges)+1 > len(s.edges)) {
				setShortest(adj.end, adj, currentShortest, shortestList)
				nodeStack = append(nodeStack, adj.end)
			}
		}
	}
}

func getShortest(node int, shortestList []ShortestInfo) ShortestInfo {
	return shortestList[node-1]
}

func setShortest(node int, edge Edge, currentShortest ShortestInfo, shortestList []ShortestInfo) {
	newEdges := make([]Edge, len(currentShortest.edges)+1)
	for i := 0; i < len(newEdges)-1; i++ {
		newEdges[i] = currentShortest.edges[i]
	}
	newEdges[len(newEdges)-1] = edge
	shortestInfo := ShortestInfo{edges: newEdges, shortest: currentShortest.shortest + edge.weight}
	shortestList[node-1] = shortestInfo
}

func findAdjacents(currentNode int, adjacentMatrix [][]int) []Edge {
	adjacents := adjacentMatrix[currentNode-1]
	edges := []Edge{}
	for i, v := range adjacents {
		if v > 0 {
			edges = append(edges, Edge{start: currentNode, end: i + 1, weight: v})
		}
	}
	return edges
}

func nextInt(sc *bufio.Scanner) (int, error) {
	sc.Scan()
	return strconv.Atoi(sc.Text())
}

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

using namespace std;

/* UnionFind：素集合系管理の構造体(union by rank)
	isSame(x, y): x と y が同じ集合にいるか。 計算量はならし O(α(n))
	unite(x, y): x と y を同じ集合にする。計算量はならし O(α(n))
*/
struct UnionFind {  // The range of node number is u 0 v n-1
	vector<int> rank, parents;
	UnionFind() {}
	UnionFind(int n) {  // make n trees.
		rank.resize(n, 0);
		parents.resize(n, 0);
		for (int i = 0; i < n; i++) {
			makeTree(i);
		}
	}
	void makeTree(int x) {
		parents[x] = x;  // the parent of x is x
		rank[x] = 0;
	}
	bool isSame(int x, int y) { return findRoot(x) == findRoot(y); }
	void unite(int x, int y) {
		x = findRoot(x);
		y = findRoot(y);
		if (rank[x] > rank[y]) {
			parents[y] = x;
		}
		else {
			parents[x] = y;
			if (rank[x] == rank[y]) {
				rank[y]++;
			}
		}
	}
	int findRoot(int x) {
		if (x != parents[x]) parents[x] = findRoot(parents[x]);
		return parents[x];
	}
};

// 辺の定義
struct Edge {
	long long u;
	long long v;
	long long cost;
};

bool comp_e(const Edge &e1, const Edge &e2) { return e1.cost < e2.cost; } // 辺を直接比較するための関数

/* Kruskal :クラスカル法で minimum spanning tree を求める構造体
	入力: 辺のvector, 頂点数V
	最小全域木の重みの総和: sum
	計算量: O(|E|log|V|)
*/

struct Kruskal {
	UnionFind uft;
	long long sum;  // 最小全域木の重みの総和
	vector<Edge> edges;
	int V;
	Kruskal(const vector<Edge> &edges_, int V_) : edges(edges_), V(V_) { init(); }
	void init() {
		sort(edges.begin(), edges.end(), comp_e); // 辺の重みでソート
		uft = UnionFind(V);
		sum = 0;
		for (auto e : edges) {
			if (!uft.isSame(e.u, e.v)) { // 閉路にならなければ加える
				uft.unite(e.u, e.v);
				sum += e.cost;
			}
		}
	}
};

int main() {

	int n;
	cin >> n;
	vector<vector<int>> VE(n, vector<int>(n));
	int counter=0;

	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			cin >> VE[i][j];
			if (VE[i][j] != -1) { counter++; }
		}
	}

	int V, E;
	V = n;
	E = counter;
//	cin >> V >> E;
	vector<Edge> edges(E);
//	for (int i = 0; i < E; i++) {
	counter = 0;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			long long s, t, w;
			//		cin >> s >> t >> w;
			if (VE[i][j] != -1) {
				s = i;
				t = j;
				w = VE[i][j];

				Edge e = { s, t, w };
				edges[counter] = e;
				counter++;
			}
		}
	}
//	}


	Kruskal krs(edges, V);
	cout << krs.sum << endl;
	return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
#define NIL -1
static const int INFTY=(1<<21);
int color[102];
int d[102][102];
int n,di,sum=0;
vector<int> searched;
void count(int start){
    int min=INFTY,minp,a,b,c;
    color[start]=2;
    searched.push_back(start);
    for(int i=0;i<n;i++){
        a=d[start][i];
        if(a!=NIL&&color[i]==0) color[i]++;
    }
    for(int i=0;i<searched.size();i++){
        b=searched[i];
        for(int j=0;j<n;j++){
            c=d[b][j];
            if(color[j]==1&&c<min&&c!=NIL){
                min=c; 
                minp=j;
                }
        }
    }
    if(min!=INFTY){
            sum+=min;
            count(minp);
    }

}
int main(){
    cin>>n;
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            d[i][j]=NIL;
            cin>>di;
            d[i][j]=di;
        }
        color[i]=0;
    }
    count(0);
    cout<<sum<<endl;
    return 0;
}

C++: #include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <cmath>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <stdlib.h>
#include <stdio.h>
#include <bitset>
#include <cstring>
#include <deque>
#include <iomanip>
#include <limits>
#include <fstream>
using namespace std;
#define FOR(I,A,B) for(int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;
// union - find tree !!!!!!!!!!!!!!!!!!!!!!!!!
vector<int> par; //oya
vector<int> rnk; //ki no hu ka sa
// n要素で初期化
void init(int n){
    par.resize(n);rnk.resize(n);
    FOR(i, 0, n){par[i] = i;rnk[i] = 0;}
}
//木の根を求める
int find(int x){
    if(par[x] == x) return x;
    else return par[x] = find(par[x]);
}
//xとyの属する集合を併合
void unite(int x, int y){
    x = find(x);y = find(y);
    if(x == y) return;
    if(rnk[x] < rnk[y])par[x] = y;
    else {
      par[y] = x;
      if(rnk[x] == rnk[y]) rnk[x]++;
    }
    return;
}
bool isSame(int x, int y){return find(x) == find(y);}
int main()
{
  ios::sync_with_stdio(false);
  cin.tie(0);
  priority_queue<PP, vector<PP>, greater<PP> > que;
  int n;
  cin >> n;
  init(n+1);
  FOR(i,1,n+1){
    FOR(j,1,n+1){
      int cost;
      cin >> cost;
      if(cost == -1) cost = 1e9;
      que.push(PP(cost, P(i, j)));
    }
  }
  int ans = 0;
  while(!que.empty()){
    PP pp = que.top();
    que.pop();
    P p = pp.second;
    if(!isSame(p.first, p.second)){
      ans += pp.first;
      unite(p.first, p.second);
    }
  }
  cout << ans << endl;
}