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++: #if 0
????????£????????????1.5h
??¢?????????????????§??????????????¢??°????????£????????????sum????¨??????¨????????????????????£????????§?????????
???????????¢??????????????§????????????????????????????????????"-1"???????????????????????????????????§???????????§????????¨???????°???????????????????
#endif

#include <iostream>
#include <algorithm>
#include <utility>
using namespace std;

int P[10000+10];

void init(int N){
	for(int i=0;i<=N;++i) P[i]=i;
}
int root(int a){
	if(P[a]==a)return a;
	else 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(){
	int N;
	cin>>N;
	int A[N][N];
	
	for(int i=0; i<N;i++){
		for(int j=0;j<N;j++){
			cin>>A[j][i]; //???????????\???
		}
	}

	int M = 0; 
	int cost[N*N],src[N*N],dst[N*N];
	for (int r=0; r<N;r++)
		for (int c=0; c<r;c++) //r<c???????§?????????????
			if (A[r][c] > -1) {
				cost[M] = A[r][c];
				src[M] = r;
				dst[M] = c;
				M += 1;
			}
	pair<int,int> edges[2000+10];
	for(int i=0;i<M;i++)
		edges[i]=make_pair(cost[i],i); //first???????????????second???ID
	sort(edges,edges+M);
	
	init(N);
	int sum=0;
	for(int i=0;i<M;i++){
		if(is_same_set(src[edges[i].second],dst[edges[i].second])){
			unite(src[edges[i].second],dst[edges[i].second]);
			sum+=edges[i].first;
		}
	}
		cout<<sum<<endl;
}

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 n, M[MAX][MAX];

int prim() {
   int u,minv;
   int 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 = i;
   minv = d[i];
   }
 }
 if( u == -1) break;
 color[u] = BLACK;
 for(int v = 0; v < n; v++){
  if(color[v] != BLACK && M[u][v] != INFTY){
    if(d[v] > M[u][v]){
       d[v] = M[u][v];
       p[v] = u;
    color[v] = GRAY;
   }
  }
 }
}
 int sum = 0;
 for(int i = 0; i < n; i++){
   if(p[i] != -1) sum += M[i][p[i]];
   }

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

C++: #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using pii = pair<int, int>;

const int INF = 1e9;

int main() {
	int n;
	cin >> n;
	vector<vector<int>> a(n, vector<int>(n));
	rep(i, n) rep(j, n) cin >> a[i][j];
	vector<int> mn(n, INF);
	mn[0] = 0;
	vector<bool> used(n, false);
	int ans = 0;
	while (1) {
		int v = -1;
		rep(u, n) if (!used[u] && (v == -1 || mn[u] < mn[v])) v = u;
		if (v == -1) break;
		used[v] = true;
		ans += mn[v];
		rep(u, n) if (a[v][u] != -1) mn[u] = min(mn[u], a[v][u]);
	}
	cout << ans << endl;
	return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <climits>
#include <string>
#include <vector>
#include <utility>
#include <functional>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <cassert>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

const ll MOD = ll(1e9) + 7;
const ll INF = LLONG_MAX;
const ll MAX_N = ll(1e5) + 5;
const double EPS = 10e-8;
const double PI = 3.14159265359;

#define REP(i, n) for (ll i = 0; i < n; i++)
#define REPR(i, n) for (ll i = n; i >= 0; i--)
#define FOR(i, m, n) for (ll i = m; i < n; i++)

int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

struct UF{
    static const int MAX_N = 100000;
    int p[MAX_N];
    int r[MAX_N];

    void init(int n){
        REP(i,n){
            p[i]=i;
            r[i]=0;
        }
    }
    int find(int x){
        if(p[x]==x)return x;
        return p[x] = find(p[x]);
    }
    void unite(int x, int y){
        x = find(x);
        y = find(y);
        if(x == y)return;
        if(r[x] < r[y])swap(x,y);
        if(r[x]==r[y])r[x]++;
        p[y]=x;
    }
    bool same(int x, int y){
        return find(x)==find(y);
    }
};

int n;
vector<pair<int,P>> es;

int MST(){
    int res = 0;
    sort(es.begin(), es.end());
    UF g;
    g.init(n);
    REP(i,es.size()){
        int cost = es[i].first, u = es[i].second.first, v = es[i].second.second;
        if(g.same(u,v))continue;
        g.unite(u,v);
        res += cost;
    }
    return res;
}

int main() {
    cin >> n;
    REP(i,n){
        REP(j,n){
            int a;
            cin >> a;
            if(a==-1)continue;
            es.push_back({a,{i,j}});
        }
    }
    cout << MST() << endl;
    return 0;
}