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 <vector>
#include <queue>
#include <functional>
#include <string.h>

using namespace std;
typedef pair<int, int> P;
struct edge
{
	int to;
	int cost;
};

vector<edge> G[101];
bool isUsed[101];

void solve()
{
	int n;
	cin >> n;
	vector< vector<int> > data((n), vector<int>(n));
	for (int i = 0; i < n; ++i)
	{
		for (int j = 0; j < n; ++j)
		{
			int c;
			cin >> data[i][j];
		}
	}
	for (int i = 0; i < n; ++i)
	{
		for (int j = i; j < n; ++j)
		{
			if (data[i][j] != -1)
			{
				edge e1, e2;
				e1.to = j;
				e1.cost = data[i][j];
				e2.to = i;
				e2.cost = data[j][i];
				G[i].push_back(e1);
				G[j].push_back(e2);
			}
		}
	}
	memset(isUsed, 0, sizeof(isUsed));
	int ans = 0;
	priority_queue < P, vector<P>, greater<P>> pque;
	pque.push(P(0, 0));
	while (!pque.empty())
	{
		P p = pque.top();
		pque.pop();
		int v = p.second;
		if (isUsed[v])
		{
			continue;
		}
		isUsed[v] = true;
		ans += p.first;
		for (int i = 0; i < G[v].size(); ++i)
		{
			edge e = G[v][i];
			pque.push(P(e.cost, e.to));
		}
	}
	cout << ans << endl;
}

int main()
{
	solve();
	return(0);
}

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

const int white = 0;
const int gray = 1;
const int black = 2;
const int Max_n = 100;
const int Infty = (1 << 21);

int M[Max_n][Max_n];
int n;

int prim(){
    int u, minv;
    int color[Max_n], d[Max_n], p[Max_n];
    for(int i=0; i<n; i++){
        color[i] = white;
        d[i] = Infty;
        p[i] = -1;
    }

    d[0] = 0;

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

        color[u] = black;

        for(int v=0; v<n; v++){
            if(color[v] != black && M[u][v] != Infty){
                if(M[u][v] < d[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<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#define maxn 101
#define Max 10000 
using namespace std;
int n,ans=0;
int map[maxn][maxn];

//struct edge
//{
//	int u;
//	int w;
//	edge(int u=0,int w=Max):u(u),w(w){}
//	bool operator <???(const node& rhs) const
//	{
//		return w>rhs.w;
//	} 
//};

void prim()
{
	int d[maxn];
	bool vis[maxn];
	memset(vis,0,sizeof(vis));
	for(int i=1;i<=n;++i)
	{
		d[i]=Max;
		vis[i]=0;
	}
	d[1]=0;
	while(1)
	{
		int MIN=Max;
		int u=-1;
		for(int i=1;i<=n;++i)
			if(MIN>d[i]&&!vis[i])
			{
				MIN=d[i];
				u=i;
			}
		if(u==-1) break;
		vis[u]=1;
		ans+=d[u];
		for(int i=1;i<=n;++i)
		{
			if(!vis[i] && map[u][i]!=Max)
			  if(d[i]>map[u][i])
			  	d[i]=map[u][i];
		}
	}
	return;
}

int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;++i)
	  for(int j=1;j<=n;++j)
	  {
	  	int x;
	  	scanf("%d",&x);
	  	if(x!=-1)
	  	  map[i][j]=x;
	  	else
	  	  map[i][j]=Max;
	  }
	prim();
	printf("%d\n",ans);
	return 0;
}

C++: // AOJ ALDS 1_12_A
#include<cstdio>
#include<algorithm>
#include<vector>
#define rep(i,a) for( int i = 0; i != (a); ++i )

struct edge
{
	int from, to, cost;

	edge( int from, int to, int cost )
	:	from( from ), to( to ), cost( cost )
	{}
};

int n;
std::vector<edge> es;

int par[100], rank[100];

void init()
{
	rep( i, n )
		par[i] = i, rank[i] = 0;

	return;
}

int find( int x )
{
	if( x == par[x] )
		return x;
	else
		return par[x] = find( par[x] );
}

void unite( int x, int y )
{
	x = find( x );
	y = find( y );
	if( x == y )
		return;

	if( rank[x] < rank[y] )
		par[x] = y;
	else
	{
		par[y] = x;
		if( rank[x] == rank[y] )
			++rank[x];
	}

	return;
}

bool same( int x, int y )
{
	return find( x ) == find( y );
}

int main()
{
	scanf( "%d", &n );
	rep( i, n )
	{
		rep( j, n )
		{
			int a;
			scanf( "%d", &a );
			if( ~a )
				es.push_back( edge( i, j, a ) );
		}
	}

	std::sort( es.begin(), es.end(), []( const edge& e1, const edge& e2 ){ return e1.cost < e2.cost; } );

	init();

	int ans = 0;
	rep( i, es.size() )
	{
		if( !same( es[i].from, es[i].to ) )
		{
			ans += es[i].cost;
			unite( es[i].from, es[i].to );
		}
	}

	printf( "%d\n", ans );

	return 0;
}