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;

static const int N = 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[N][N];

int prim()
{
	int d[N], p[N], color[N];

	for (int i = 0; i < n; ++i)
	{
		d[i] = INFTY;
		p[i] = -1;
		color[i] = WHITE;
	}

	d[0] = 0;

	while (1)
	{
		int u = -1;
		int minv = INFTY;
		for (int i = 0; i < n; ++i)
		{
			if (minv > d[i] && color[i] != BLACK)
			{
				minv = d[i];
				u = i;
			}
		}

		if (u == -1) break;
		color[u] = BLACK;

		for (int v = 0; v < n; ++v)
		{
			if (m[u][v] != INFTY && color[v] != BLACK && m[u][v] < d[v])
			{
				d[v] = m[u][v];
				p[v] = u;
				color[v] = GRAY;
			}
		}
	}

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

	return s;
}

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 <iostream>
#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <string>
#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <algorithm>

using namespace std;

// ---------------------
// repetition
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)

// debug
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

// ---------------------
#define INF 922337203685477580
typedef long long ll;

int main() {
  int n;
  cin >> n;

  int G[n][n];
  int visited[n];
  int d[n];
  int p[n];
  REP(y, n) {
    REP(x, n) {
      int cost;
      cin >> cost;

      if (cost == -1) {
        G[y][x] = 1 << 15;
      } else {
        G[y][x] = cost;
      }
    }
    visited[y] = 0;
    d[y] = 1 << 15;
    p[y] = -1;
  }

  d[0] = 0;

  REP(_, n) {
    int mincost = 1 << 15;
    int minv = -1;
    REP(v, n) {
      if (!visited[v] && d[v] < mincost) {
        minv = v;
        mincost = d[v];
      }
    }
    visited[minv] = 1;
    REP(to, n) {
      if (!visited[to] && G[minv][to] < (1 << 15) && G[minv][to] < d[to]) {
        d[to] = G[minv][to];
        p[to] = minv;
      }
    }
  }

  ll sum = 0;
  REP(i, n) {
    if (p[i] != -1) {
      sum += G[i][p[i]];
    }
  }

  cout << sum << endl;
  return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

struct Edge{
  int weight;
  int from;
  int to;
  Edge(int w,int f,int t):weight(w),from(f),to(t){}
};

const int inf = 1<<28;

bool operator<(Edge e1,Edge e2){
  return (e1.weight < e2.weight);
}

int nlist[110];

int parent(int i){
  if(nlist[i] == i) return i;
  else return (nlist[i] = parent(nlist[i]));
}

int main(){
  int n;
  cin>>n;
  vector<Edge> Elist;
  for(int i=0;i<n;i++) nlist[i]=i;
  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
      int temp;
      cin>>temp;
      if(temp != -1){
	Elist.push_back(Edge(temp,i,j));
      }
    }
  }
  sort(Elist.begin(),Elist.end());
  int ans = 0;
  for(int i=0;i<Elist.size();i++){
    Edge e = Elist[i];
    int fp = parent(e.from);
    int tp = parent(e.to);
    if(fp != tp){
      ans += e.weight;
      nlist[fp] = tp;
    }
  }
  cout<<ans<<endl;
  return 0;
}