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;
#define MAX 100
#define INF 10000
int M[MAX][MAX];
int n;
int d[MAX];
int color[MAX];
int p[MAX];



int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cin >> M[i][j];
    }
  }

  for (int i = 0; i < n; i++) {
    d[i] = INF;
    color[i] = 0;
  }

  int w = 0;
  d[0] = 0;
  while(true) {

    int com = INF;
    int u;
    for (int i = 0; i < n; i++) {
      if (color[i] != 2 && com > d[i] && d[i] != -1){
        com = d[i];
        u = i;
      }
    }

    if (com == INF) {
      break;
    }

    w += com;
    color[u] = 2;

    for (int j = 0; j < n; j++) {
      if (M[u][j] != -1 && M[u][j] < d[j] && color[j] != 2) {
        d[j] = M[u][j];
        p[j] = u;
      }
    }
  }

  cout << w << endl;
  return 0;
}

C++: #include <cstdio>
#include <iostream>
#include <queue>
#include <utility>

using namespace std;

const int NIL = 2001;
const int MAX = 100;
int G[MAX][MAX];
int color[MAX];
int W[MAX];
int n;

const int WHITE = 0;
const int GRAY = 1;
const int BLACK = 2;

void MST() {
	priority_queue<pair<int, int>> PQ;
	PQ.push(make_pair(0, 0));
	W[0] = 0;
	color[0] = BLACK;
	while (!PQ.empty()) {
		int u = PQ.top().second;
		PQ.pop();
		color[u] = BLACK;
		for (int i = 0; i < n; ++i) {
			if (G[u][i] != -1 && G[u][i] < W[i] && color[i] != BLACK) {
				PQ.push(make_pair(G[u][i] * -1, i));
				W[i] = G[u][i];
//				cout << u << ":" << i << ":" << G[u][i] << endl;
			}
		}
	}
}

int main() {

	scanf("%d", &n);
	for (int i = 0; i < n; ++i) {
		W[i] = NIL;
		color[i] = WHITE;
	}

	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < n; ++j) {
			int a;
			scanf("%d", &a);
			G[i][j] = a;
		}
	}

	MST();

	cout << endl;

	int sum = 0;
	for (int i = 0; i < n; ++i) {
//		cout << i << ":" << W[i] << endl;
		sum += W[i];
	}
	cout << sum << endl;

	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define WHITE 0
#define GRAY 1
#define BLACK 2

int M[101][101];
int n;
vector<int> q;

int check(int point){
    if(find(q.begin(), q.end(), point) != q.end()){
        //printf("%d in q\n",point);
        return 1;
    }
    else{
        //printf("%d not in q\n",point);
        return 0;
    }
}

int prim(int point){
    q.push_back(point);
    int dis = INT_MAX;
    int next_point;
    int sum = 0;
    while(q.size()!=n){
        dis = INT_MAX;
        for(int i=0;i<q.size();++i){
            for(int j=0;j<n;++j){
                if(M[q[i]][j] != INT_MAX && M[q[i]][j] < dis && check(j)==0){
                    dis = M[q[i]][j];
                    next_point = j;
                }
            }
        }
        q.push_back(next_point);
        //printf("push %d\n",next_point);
        sum += dis;
    }
    return sum;
}

int main(){
    int u, z, y;
    int value;
    scanf("%d",&n);
    for(int i=0;i<n;++i){
        for(int j=0;j<n;++j){
            cin>>value;
            M[i][j] = (value==-1)?INT_MAX:value;
            M[j][i] = (value==-1)?INT_MAX:value;
        }
    }
    int sum = prim(0);

    printf("%d\n",sum);
    return 0;
}

C++: #include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
using namespace std;

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef stack<int> SI;
typedef queue<int> QI;
typedef pair<int,int> PII;
typedef long long LL;

#define d(x)  cout<<#x<<" = "<<(x)<<endl;
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define REACH(i,c) for(typeof((c).rbegin()) i=(c).rbegin(); i!=(c).rend(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c,n) sort(c,c+n)
#define VSORT(c) sort((c).begin(),(c).end())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define DFOR(i,b,a) for(int i=(b)-1;i>=(a);--i)
#define DREP(i,n) DFOR(i,n,0)
#define INF 2000000000

const double EPS = 1e-10;
const double PI  = acos(-1.0);
////////////////////////////////////////////////
int a[100][100],n,s,judge,cost;
int t[100];
int main()
{
	cin>>n;
	REP(i,n)
	{
		REP(j,n)
		{
			cin>>a[i][j];
			if(a[i][j]==-1)a[i][j]=INF;
		}
	}
	t[0]=1;
	for(;;)
	{
		int mincost=INF;
		REP(u,n)
		{
			REP(v,n)
			{
				if(t[u]==1&&t[v]==0)
				{
					if(mincost>a[u][v])
					{
						mincost=a[u][v];
						s=v;
					}
				}
			}
		}
		cost+=mincost;
		t[s]=1;
		judge=1;
		REP(i,n)judge&=t[i];
		if(judge==1)break;
	}
	cout<<cost<<endl;
	return 0;
}