Go: package main

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

// I/O
type Scanner struct {
	sc *bufio.Scanner
}

func NewScanner() *Scanner {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 1024), int(1e+9))
	return &Scanner{sc}
}

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

func (s *Scanner) nextInt() int {
	i, e := strconv.Atoi(s.nextStr())
	if e != nil {
		panic(e)
	}
	return i
}

func (s *Scanner) nextFloat() float64 {
	f, e := strconv.ParseFloat(s.nextStr(), 64)
	if e != nil {
		panic(e)
	}
	return f
}

func (s *Scanner) nextRuneSlice() []rune {
	return []rune(s.nextStr())
}

func (s *Scanner) nextIntSlice(n int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextInt()
	}
	return res
}

func (s *Scanner) nextFloatSlice(n int) []float64 {
	res := make([]float64, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextFloat()
	}
	return res
}

// Arithmetic
func max(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m < i {
			m = i
		}
	}
	return m
}

func min(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m > i {
			m = i
		}
	}
	return m
}

func abs(x int) int {
	if x > 0 {
		return x
	}
	return -x
}

func pow(x, y int) int {
	res := 1
	for i := 0; i < y; i++ {
		res *= x
	}
	return res
}

func ceil(a, b int) int {
	if a%b == 0 {
		return a / b
	} else {
		return a/b + 1
	}
}

// Sort
type RuneSlice []rune

func (a RuneSlice) Len() int           { return len(a) }
func (a RuneSlice) Less(i, j int) bool { return a[i] < a[j] }
func (a RuneSlice) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

const INF = 1 << 60

// Main
func main() {
	sc := NewScanner()
	wtr := bufio.NewWriter(os.Stdout)
	N := sc.nextInt()
	G := make([][]int, N)
	for i := 0; i < N; i++ {
		G[i] = make([]int, N)
		for j := 0; j < N; j++ {
			G[i][j] = sc.nextInt()
		}
	}
	d := make([]int, N)
	for i := 0; i < N; i++ {
		d[i] = INF
	}
	seen := make([]bool, N)
	d[0] = 0
	ans := 0
	for {
		cur := 0
		minCost := INF
		for i := 0; i < N; i++ {
			if !seen[i] && d[i] < minCost {
				minCost = d[i]
				cur = i
			}
		}
		if minCost == INF {
			break
		}
		seen[cur] = true
		ans += d[cur]
		for i := 0; i < N; i++ {
			cost := G[cur][i]
			if cost != -1 && cost < d[i] {
				d[i] = cost
			}
		}
	}

	fmt.Fprintln(wtr, ans)
	wtr.Flush()
}

C++: #include <iostream>
#include <algorithm>
using namespace std;
int P[10000];
void init(int N) {
  for (int i = 0; i < N; i++) {
    P[i] = i;
  }
}

int root(int a) {
  if(P[a] == a)
    return a;
  return (P[a] = root(P[a]));
}

bool is_same_set(int a, int b) {
  return root(a) == root(b);
}

void unite(int a, int b) {
  P[root(a)] = root(b);
}

int main()
{
  int N;
  cin >> N;
  int A[N][N];
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      int x;
      cin >> x;
      A[j][i] = x;
      // cout << "cost: " << x << " = " <<  j << "->"  << i << endl;
    }
  }
  // cout << "-----エッジ-------" << endl;
  int cost[10000], src[10000], dst[10000];
  int M = 0;
  for (int r = 0; r < N; r++)
  {
    for (int c = r + 1; c < N; c++)
    {
      if (A[r][c] > -1)
      {
        cost[M] = A[r][c];
        src[M] = r;
        dst[M] = c;
        // cout << "cost: " << cost[M] << " = " <<  src[M] << "->"  << dst[M] << endl;
        M += 1;
      }
    }
  }
  // cout << "M = " << M << " N = " << N << endl;
  init(N);
  pair<int, pair<int, int> > edges[M + 1];
  for (int m = 0; m < M; m++)
  {
    edges[m] = make_pair(cost[m], make_pair(src[m], dst[m]));
  }
  // cout << "-----sort前-------" << endl;
  // for (int i = 0; i < M; i++)
  //     cout << "cost: " << edges[i].first << " = " << edges[i].second.first << "->"  << edges[i].second.second << endl;
  sort(edges, edges+M);
  // cout << "-----sort後-------" << endl;
  // for (int i = 0; i < M; i++)
  //     cout << "cost: " << edges[i].first << " = " << edges[i].second.first << "->"  << edges[i].second.second << endl;
  int sum = 0;
  for (int i=0; i<M; i++) {
    if (is_same_set(edges[i].second.first, edges[i].second.second))
      continue;
    unite(edges[i].second.first, edges[i].second.second);
    sum += edges[i].first;
  }
  cout << sum << endl;
}

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 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 -1 1 3 -1
)";
    stringstream stream(program_input_str);
#endif
    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