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<iostream>
using namespace std;

const int MAX = 100;


int main() {
	int n;
	int graph[MAX][MAX];
	bool table[MAX] = { false };
	int sum = 0;

	cin >> n;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			int temp;
			cin >> temp;
			if (temp == -1)temp = INT_MAX;
			graph[i][j] = temp;
		}
	}

	table[0] = true;

	for (int i = 0; i < n - 1; i++) {
		int cost = INT_MAX;
		int m;

		for (int j = 0; j < n; j++) {
			if (table[j] == false)continue;

			for (int k = 0; k < n; k++) {
				if (graph[j][k] < cost && table[k] == false) {
					cost = graph[j][k];
					m = k;
				}
			}
		}

		sum += cost;
		table[m] = true;
	}

	cout << sum << endl;

	return 0;
}

C++: #include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <ctime>
#include <cstring>
#include <functional>
#include <iostream>
#include <iomanip>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <regex>
#include <vector>

#define fix(n)          cout<<fixed<<setprecision(n)
#define rep(i,n)        for (int i = 0; i < (n); ++i)
#define all(a)          (a).begin(), (a).end()
#define sort(a)         sort(all(a))
#define uniq(a)         sort(a);(a).erase(unique(all(a)), (a).end())
#define reverse(a)      reverse(all(a))
#define ctos(c)         string(1, (c))
#define out(d)          cout << (d)
#define outl(d)         std::cout<<(d)<<"\n"
#define YES()           cout << "YES" << endl
#define NO()            cout << "NO" << endl
#define Yes()           cout << "Yes" << endl
#define No()            cout << "No" << endl
#define ceil(x, y)      ((x + y - 1) / (y))
#define debug(x)        cerr << #x << ": " << (x) << '\n'
#define debug2(x, y)    cerr << #x << ": " << (x) << ", " << #y << ": " << (y) << '\n'
#define debug3(x, y, z) cerr << #x << ": " << (x) << ", " << #y << ": " << (y) << ", " << #z << ": " << (z) << '\n'
#define dbg(v)          for (size_t _ = 0; _ < v.size(); ++_){ cerr << #v << "[" << _ << "] : " << v[_] << '\n'; }
#define pb              push_back
#define fst             first
#define int             long long
#define INF             __LONG_LONG_MAX__

using namespace std;
using ll = long long;
using ld = long double;
using P = pair<ll,ll>;

const ll MOD = 1000000007; // 10^9 + 7
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};

void solve() {
  const ll MAX = 100;

  ll n; cin >> n;
  ll mat[MAX][MAX];
  rep(i, n)rep(j, n) cin >> mat[i][j];

  vector<bool> mst(n, false);
  ll ans = 0;
  mst[0] = true;
  priority_queue<tuple<ll, ll, ll>> pq;
  for (int i = 1; i < n; i++) {
    if (mat[0][i] != -1) pq.emplace(-mat[0][i], 0, i);
  }

  ll w, f, t;
  rep(i, n-1) {
    while(1) {
      tie(w, f, t) = pq.top(); pq.pop();
      if (mst[f] && !mst[t]) break;
    }
    // debug3(-w, f, t);
    ans += mat[f][t];
    mst[t] = true;

    for (int j = 0; j < n; j++) {
      if (t != j && mat[t][j] != -1) pq.emplace(-mat[t][j], t, j);
    }
  }
  outl(ans);
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  srand((unsigned)time(NULL));
  fix(12);

  solve();
}

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 M = 0;
		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