Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

//util
var sc = bufio.NewScanner(os.Stdin)

func nextStr() string {
	sc.Scan()
	return sc.Text()
}

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func printList(list []int) {
	n := len(list)
	buf := bufio.NewWriter(os.Stdout)
	for _, v := range list[0 : n-1] {
		buf.Write([]byte(fmt.Sprintf("%d ", v)))
	}
	buf.Write([]byte(fmt.Sprintf("%d\n", list[n-1])))
	buf.Flush()
}

func max(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

func min(a, b int) int {
	if a > b {
		return b
	} else {
		return a
	}
}

//solve
const (
	MAX   = 100
	INFTY = (1 << 21)
	WHITE = 0
	GRAY  = 1
	BLACK = 2
)

//V: グラフの頂点全体の集合
//T: MSTに属する頂点の集合
var (
	n     int
	M     [MAX][MAX]int //重みを記録する隣接行列
	color [MAX]int
	p     [MAX]int //MSTにおける頂点の親を記録
	d     [MAX]int //Tに属する頂点と、V−Tに属する頂点を繋ぐ辺の中で、重みが最小のものを記録
)

func prim() int {
	var u, minv int
	//初期化
	for i := 0; i < n; i++ {
		d[i] = INFTY
		p[i] = -1
		color[i] = WHITE
	}

	d[0] = 0

	for {
		//頂点uを決めていく
		//つまり、Tに属する頂点と、V−Tに属する頂点を繋ぐ辺の中で、重みが最小のものを決める
		minv = INFTY
		u = -1
		for i := 0; i < n; i++ {
			//未訪問かつ、辺の重みが最小となるuを選ぶ
			if minv > d[i] && color[i] != BLACK {
				u = i
				minv = d[i]
			}
		}

		if u == -1 {
			break
		}

		color[u] = BLACK
		for v := 0; v < n; v++ {
			//未訪問かつ、辺が存在するとき
			if color[v] != BLACK && M[u][v] != INFTY {
				if d[v] > M[u][v] {
					d[v] = M[u][v]
					p[v] = u
					color[v] = GRAY
				}
			}
		}
	}

	sum := 0
	for i := 0; i < n; i++ {
		if p[i] != -1 {
			sum += M[i][p[i]]
		}
	}
	return sum
}

func main() {
	sc.Split(bufio.ScanWords)
	n = nextInt()

	for i := 0; i < n; i++ {

		for j := 0; j < n; j++ {
			tmp := nextInt()
			if tmp == -1 {
				tmp = INFTY
			}
			M[i][j] = tmp
		}

	}

	fmt.Println(prim())

}

C++: // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_12_A
// Graph II - Minimum Spanning Tree
// TODO: REVIEW

#define SUBMIT

#include <utility>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <vector>
#include <limits>
#include <numeric>
#include <functional>
#include <set>
#include <cmath>
#include <cstring>
#include <climits>
#include <memory>

using namespace std;
using ui64 = unsigned long long;
using i64 = long long;

enum class color_type {
    white,
    gray,
    black
};

const int INF = INT_MAX;
const ui64 MAX_N = 100;
int adj[MAX_N][MAX_N];
int n;

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

    for (int i = 0; i < n; ++i) {
        d[i] = INF;
        p[i] = -1;
        color[i] = color_type::white;
    }

    d[0] = 0;

    while (true) {
        // 未訪問のノード
        int u = -1, min_v = INF;
        for (int i = 0; i < n; ++i) {
            if (d[i] < min_v && color[i] != color_type::black) {
                u = i;
                min_v = d[i];
            }
        }

        if (u == -1) break;

        color[u] = color_type::black;
         for (int v = 0; v < n; ++v) {
            if (color[v] != color_type::black && adj[u][v] != INF) {
                if (adj[u][v] < d[v]) {
                    d[v] = adj[u][v];
                    p[v] = u;
                    color[v] = color_type::gray;
                }
            }
        }
    }

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

    return sum;
}

int main() {
#ifdef SUBMIT
    auto& stream = cin;
#else
    string program_input_str = R"(20
 -1 100 2 -1 5 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
 100 -1 3 1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
 2 3 -1 1 10 12 -1 -1 -1 -1 -1 -1 -1 -1 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    memset(adj, 0, sizeof(adj));
    stream >> n;

    int w;
    for (int y = 0; y < n; ++y) {
        for (int x = 0; x < n; ++x) {
            stream >> w;
            if (w == -1) adj[y][x] = INF;
            else adj[y][x] = w;
        }
    }

    cout << prim() << endl;
    return 0;
}

C++: n = int(raw_input())
A = [0]
i = 0
long = 0
NY = range(1, n)
a = [[0 for i in xrange(n)] for j in xrange(n)]
while i <=n:
    a[i] = map(int,raw_input().split())
    i = i+1
while NY != []:
    amin = 2001
    for i in A:
        for j in NY:
            if 0 <= a[i][j] < amin:
                amin = a[i][j]
                addA = j
    A.append(addA)
    NY.remove(addA)
    long =long + amin
print long

C++: #include <bits/stdc++.h>
using namespace std;

int n;
vector< vector<int> > a;

int Prim() {
  vector<int> color(n);
  vector<int> d(n, 1000000000);
  vector<int> p(n);
  d[0] = 0, p[0] = -1;

  while (true) {
    int min_cost = 1000000000, u;
    for (int i = 0; i < n; i++) if (color[i] != 2 && d[i] < min_cost) min_cost = d[i], u = i;
    if (min_cost == 1000000000) break;
    color[u] = 2;
    for (int v = 0; v < n; v++)
      if (color[v] != 2 && a[u][v] != -1)
        if (a[u][v] < d[v]) d[v] = a[u][v], p[v] = u, color[v] = 1;
  }

  int ans = 0;
  for (int i = 0; i < n; i++) if(p[i]!=-1 )ans += a[i][p[i]];

  return ans;
}

int main() {
  cin >> n;
  a.resize(n, vector<int>(n));
  for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) cin >> a[i][j];
  cout << Prim() << endl;
}

C++: #include <iostream>
using namespace std;
const int maxx=110;
const int infinity=(1<<30);

int A[maxx][maxx];
bool flag[maxx];	//用flag来标记这个节点是不是在MST中，true是在T里，false是在V-T里；

int main (){
		int n;
		cin>>n;
		for(int i =1;i<=n;i++){
			for(int j=1;j<=n;j++){
				int e;cin>>e;
				A[i][j]=(e==-1)?infinity:e;
			}
		}

		for(int i=0;i<maxx;i++) flag[i]=false;
		int sum=0,minv,minj;
		flag[1]=true;
		while(1){
			minv=infinity;
			minj=-1;
			for(int i=1;i<=n;i++){
				if(flag[i]==true){
					for(int j=1;j<=n;j++){
						if(A[i][j]!=-1 && A[i][j]<minv && flag[j]==false) {
							minv=A[i][j];
							minj=j;
						} 
					}
				}
			}
//			cout<<minj<<endl;
			if(minv==infinity || minj==-1) break;
			flag[minj]=true;
			sum+=minv;
		}
		
		cout<<sum<<endl;
		return 0;
		
}