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++: //
// Created by 李青坪 on 2018/11/6.
//

#include <cstdio>
#include <algorithm>
#include <set>
#define MAXN 100
#define INFINITY 1<<20
#define NIL -1
using namespace std;

int n;
int sum;
int graph[MAXN][MAXN];
int d[MAXN];
int parent[MAXN];
set<int> T;
set<int> V_T;

void init(){
    fill(d, d+n, INFINITY);
    fill(parent, parent+n, NIL);
    T.insert(0);
    for (int i = 1; i < n; ++i) {
        V_T.insert(i);
    }
    for (int j = 0; j < n; ++j) {
        d[j] = graph[0][j];
    }
}

void updateMinWeight(){

}

pair<int, pair<int, int> > findMinEdge(){
    int min = INFINITY;
    pair<int, int> r;
    set<int >::iterator itr;
    for (itr = T.begin(); itr != T.end(); itr++){
        for (set<int>::iterator itr1 = V_T.begin(); itr1 != V_T.end(); itr1++){
            if (graph[*itr][*itr1] < min){
                min = graph[*itr][*itr1];
                r = make_pair(*itr, *itr1);
            }
        }
    }
    return make_pair(min, r);
}

void findMinSpanTree(){
    while (!V_T.empty()){
        pair<int, pair<int, int> > v = findMinEdge();
        sum += v.first;
        parent[v.second.second] = v.second.first;
        T.insert(v.second.second);
        V_T.erase(V_T.find(v.second.second));
    }
}

int main(){
    scanf("%d", &n);
    for (int i = 0; i < n; ++i) {
        int w;
        for (int j = 0; j < n; ++j) {
            scanf("%d", &w);
            if (w == -1) graph[i][j] = INFINITY;
            else graph[i][j] = w;
        }
    }
    init();
    findMinSpanTree();
    printf("%d\n", sum);
}
/*
5
 -1 2 3 1 -1
 2 -1 -1 4 -1
 3 -1 -1 1 1
 1 4 1 -1 3
 -1 -1 1 3 -1
 */

C++: #include<bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define INF (1<<21)
#define white 0
#define gray 1
#define black 2

int n;
int W[100][100];
int color[100],d[100],p[100];

int prim() {

  d[0] = 0;

  while(true) {
    int mini = INF,u;
    for(int i = 0;i < n;i++) {
      if(d[i] < mini && color[i] != black) {
        mini = d[i];
        u = i;
      }
    }

    

    if(mini == INF) break;
    color[u] = black;

    for(int i = 0;i < n;i++) {
      if(color[i] != black && W[u][i] != -1) {
        if(W[u][i] < d[i]) {
          d[i] = W[u][i];
          p[i] = u;
        }
      }
    }
  }

  int sum = 0;
  for(int i = 0;i < n;i++) {
    if(p[i] != -1) sum += W[i][p[i]];
  }
 
  return sum;

}


int main() {

  cin >> n;

  for(int i = 0;i < n;i++) {
    color[i] = white;
    d[i] = INF;
    p[i] = -1;
    for(int j = 0;j < n;j++) {
      int a;
      cin >> a;
      W[i][j] = a;
    }
  }

 
    cout << prim() << endl;

}

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

/* 大文字を小文字に変換 */
char tolower(char c) {return (c + 0x20);}
/* 小文字を大文字に変換 */
char toupr(char c) {return (c - 0x20);}

// if('A'<=s[i] && s[i]<='Z') s[i] += 'a'-'A';

/*
string s = "abcdefg"
s.substr(4) "efg"
s.substr(0,3) "abc"
s.substr(2,4) "cdef"

// イテレータ要素のインデックス
distance(A.begin(), itr);
*/

const int MAX_N = 105, INF = 10000;
const int WHITE = 0, GRAY = 1, BLACK = 2;

int n;
int M[MAX_N][MAX_N], color[MAX_N], d[MAX_N], p[MAX_N];

void init(){
    rep(i,MAX_N){
        rep(j,MAX_N) M[i][j] = -1;
        color[i] = WHITE;
        d[i] = INF;
    }
}

void prim(int u){
    d[u] = 0;
    p[u] = -1;

    while(true){
        int mincost = INF;
        rep(i,n){
            if(color[i] != BLACK && mincost > d[i]){
                mincost = d[i];
                u = i;
            }
        }
        if(mincost == INF) break;
        color[u] = BLACK;
        rep(v,n){
            if(color[v] != BLACK && M[u][v] != -1){
                if(M[u][v] < d[v]){
                    d[v] = M[u][v];
                    p[v] = u;
                    color[v] = GRAY;
                }
            }
        }
    }
}

int main()
{
    cin >> n;
    init();
    rep(i,n){
        rep(j,n){
            int aij; cin >> aij;
            if(aij != -1) M[i][j] = aij;
        }
    }
    prim(0);
    int sum = 0;
    rep(i,n) sum += d[i];
    cout << sum << endl;
}