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>
#include <limits>

constexpr int MAXN = 100;
int G[MAXN][MAXN];
int D[MAXN];
int P[MAXN];
int C[MAXN];
int n;
constexpr int MAXINT = 2001;

#define FOR(i,a,b) for(int i = (a); i<(b); ++i)

int main() {

    std::cin >> n;
    FOR(i, 0, n)
    FOR(j, 0, n)
    {
        int v;
        std::cin >> v;
        if(v < 0) G[i][j] = MAXINT;
        else G[i][j] = v;
    }

    FOR(i, 0, n) D[i] = MAXINT;

    int r = 0;
    D[r] = 0;
    P[r] = -1;
    while (true) {
        int mincost = MAXINT;
        int t;
        for (int i = 0; i < n; i++) {
            if ((C[i] == 0) && (D[i] < mincost)) {
                mincost = D[i];
                t = i;
            }
        }
        if (mincost == MAXINT) {
            break;
        }

        C[t] = 1;
        P[t] = r;

        for (int i = 0; i < n; i++) {
            if ((C[i] == 0) && (G[t][i] < D[i])) {
                D[i] = G[t][i];
            }
        }
        r = t;
    }

    int sum = 0;
    FOR(i, 0, n) {
        sum += D[i];
    }
    std::cout << sum << std::endl;

}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <queue>
#include <stack>
#include <map>
#define FOR(i,b,e) for(int i=b;i<e;i++)
#define REP(i,n) FOR(i,0,n)
#define INFTY 1000000
using namespace std;

const int N = 100;
int n;
int D[N], M[N][N];
bool V[N];

int prim() {
  int nn, minD, cost;

  REP(i, n) D[i] = INFTY;
  D[0] = 0;

  while (true) {
    minD = INFTY;
    REP(i, n) {
      if (!V[i] && D[i] < minD) {
        minD = D[i];
        nn = i;
      }
    }
    if (minD == INFTY) break;

    cost += D[nn];
    V[nn] = true;
    REP(i, n) {
      if (!V[i] && M[nn][i] != -1 && M[nn][i] < D[i]) {
        D[i] = M[nn][i];
      }
    }
  }

  return cost;
}

int main() {
	cin.tie(0);
 	ios::sync_with_stdio(false);

  cin >> n;
  REP(i, n) {
    REP(j, n) {
      cin >> M[i][j];
    }
  }

  cout << prim() << endl;
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <cmath>
#include <deque>
#include <iterator>
#include <map>
#include <queue>
#include <string>
#include <tuple>
#include <utility>
using namespace std;

using ll=long long;
template<class T> using V = vector<T>;
template<class T, class U> using P = pair<T, U>;
using vll = V<ll>;
using vvll = V<vll>;
#define rep(i, k, n) for (ll i=k; i<(ll)n; ++i)
#define REP(i, n) rep(i, 0, n)
template<class T> inline bool chmax(T& a, T b) {if (a<b) {a=b; return true;} return false;}
template<class T> inline bool chmin(T& a, T b) {if (a>b) {a=b; return true;} return false;}

vll parent(100);
vll size(100);

ll root(ll i) {
  if (parent.at(i) != i) parent.at(i) = root(parent.at(i));
  return parent.at(i);
}

bool same(ll i, ll j) {
  return root(i) == root(j);
}

void unite(ll i, ll j) {
  ll pi = root(i), pj = root(j);
  if (pi == pj) return;
  if (size.at(pi) < size.at(pj)) parent.at(pi) = pj;
  else if (size.at(pi) > size.at(pj)) parent.at(pj) = pi;
  else {
    if (pi < pj) {
      parent.at(pj) = pi; size.at(pi)++;
    } else {
      parent.at(pi) = pj; size.at(pj)++;
    }
  }
}

int main() {
  ll n; cin >> n;
  REP(i, n) parent.at(i) = i;
  priority_queue<vll, vvll, greater<vll> > pq;
  REP(i, n) REP(j, n) {
    ll c; cin >> c;
    if (i >= j) continue;
    if (c != -1) {
      vll p; p.push_back(c); p.push_back(i); p.push_back(j);
      pq.push(p);
    }
  }
  ll ans = 0;
  while (!pq.empty()) {
    vll q = pq.top(); pq.pop();
    if (root(q.at(1)) == root(q.at(2))) continue;
    else {
      ans += q.at(0);
      unite(q.at(1), q.at(2));
    }
  }
  cout << ans << endl;
  return 0;
}

C++: #include <algorithm>
#include <iostream>
#include <set>
#include <vector>
#define FOR(i,k,n) for (int (i)=(k); (i)<(n); ++(i))
#define REP(i,n) FOR(i,0,n)
#define pb push_back
#define mp make_pair
#define fst first
#define snd second
#define all(v) v.begin(), v.end()
using namespace std;
typedef pair<int,int> PII;

int P[100]; //union_find

int root(int i){
    if(P[i] == i) return i;
    else return P[i] = root(P[i]);
}

bool same(int x, int y){
    return root(P[x]) == root(P[y]);
}

void unite(int x, int y){
    P[root(x)] = root(y);
}

int main(){
    int n;
    cin >> n;
    int g[n][n];
    vector<pair<int,PII> > V;
    REP(i,n){
        REP(j,n){
            cin >> g[i][j];
            if(i<j && g[i][j] >= 0) V.pb(mp(g[i][j],mp(i,j)));
        }
    }
    sort(all(V));
    REP(i,n){
        P[i] = i;
    }
    int wsum = 0, count = 0;
    REP(i,n*n){
        int x = V[i].snd.fst;
        int y = V[i].snd.snd;
        if(same(x,y)) continue;
        unite(x,y);
        wsum += V[i].fst;
        count++;
        if(count == n-1) break;
    }

    cout <<wsum<<endl;

    return 0;
}