Go: package main

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

// Define disjoint-set forests

type vertexNo int

type vertex struct {
	number vertexNo
	rank int
	parent *vertex
}

func makeSet(number vertexNo) vertex {
	return vertex {
		number: number,
		rank: 0,
		parent: nil,
	}
}

func union(x, y *vertex) *vertex {
	return link(findSet(x), findSet(y))
}

func link(x, y *vertex) *vertex {
	if x.rank > y.rank {
		y.parent = x
		return x
	} else {
		x.parent = y
		if x.rank == y.rank {
			y.rank += 1
		}
		return y
	}
}

func findSet(v *vertex) *vertex {
	if v.parent == nil {
		return v
	}
	v.parent = findSet(v.parent)
	return v.parent
}

// Function to calculate minimum spanning tree

type matrix [][]int

type edge struct {
	one vertexNo
	two vertexNo
	weight int
}

const(
	MAX_VERTICES = 100
)

func newEdgesFromMatrix(matrix matrix) []edge {
	edges := make([]edge, 0, MAX_VERTICES * MAX_VERTICES)

	for i := 0; i < len(matrix); i++ {
		row := matrix[i]
		for j := i; j < len(row); j++ {
			if row[j] >= 0 {
				edge := edge{one: vertexNo(i+1), two: vertexNo(j+1), weight: row[j]}
				edges = append(edges, edge)
			}
		}
	}

	return edges
}

func solve(matrix matrix) int {
	nVertices := len(matrix)

	// initialize vertices
	vertices := make([]vertex, nVertices)
	for i := 0; i < nVertices; i++ {
		vertices[i] = makeSet(vertexNo(i+1))
	}

	// prepare edges sorted in the ascending order of weight
	edges := newEdgesFromMatrix(matrix)
	sort.Slice(edges, func(i, j int) bool {
		return edges[i].weight < edges[j].weight
	})

	unionCount := 0
	weightSum := 0
	for unionCount < nVertices-1 {
		for i := 0; i < len(edges); i++ {
			edge := edges[i]
			v1 := &vertices[edge.one-1]
			v2 := &vertices[edge.two-1]
			rep1 := findSet(v1)
			rep2 := findSet(v2)
			if rep1.number == rep2.number {
				continue
			}
			union(rep1, rep2)
			edges = append(edges[0:i], edges[i+1:]...)
			weightSum += edge.weight
			unionCount++
			break
		}
	}

	return weightSum
}

// main

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	n, err := nextInt(sc)
	if err != nil {
		panic(err)
	}

	matrix := parseMatrix(n, sc)

	edges := newEdgesFromMatrix(matrix)
	sort.Slice(edges, func(i, j int) bool {
		return edges[i].weight < edges[j].weight
	})

	fmt.Println(solve(matrix))
}

func parseMatrix(n int, sc *bufio.Scanner) [][]int {
	matrix := make([][]int, n)
	for i := 0; i < n; i++ {
		matrix[i] = make([]int, n)
		for j := 0; j < n; j++ {
			x, err := nextInt(sc)
			if err != nil {
				panic(err)
			}
			matrix[i][j] = x
		}
	}
	return matrix
}

func nextText(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

func nextInt(sc *bufio.Scanner) (int, error) {
	return strconv.Atoi(nextText(sc))
}

C++: //minimum spanning tree
//http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_ 12_A&lang=jp
//右　奥

#include <iostream>
#include <algorithm>
#include <tuple>
using namespace std;


int P[10010]; // 0 から 10000 までの頂点を取り扱い可能
void init(int N) { // 初期化 はじめは全ての頂点はバラバラ
  for (int i=0; i<N; i++) P[i] = i;
}
int root (int a) { //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;
  tuple<int,int,int> cost[10010]; //Array for cost
  int count = 0;
  for (int i = 0; i < n; i ++){
    for (int j = 0; j < n; j++){
      int t;
      cin >> t;
      if (t >= 0){
      get<1>(cost[count]) = i;
      get<2>(cost[count]) = j;
      get<0>(cost[count]) = t;
      count++;
    }
    }
  } // tupleに行列Aの情報を入れる
  sort(cost,cost + n*n);//sortする //n*n個ソートするときの書き方

  init(n + 10); // 初期化 はじめは全ての頂点はバラバラ

  int ans = 0;
  int anscount = 0; //このcountがnになった時点で終了する
  int k = 0;
  while (anscount != n-1){
    if (is_same_set(get<1>(cost[k]), get<2>(cost[k]))) {
      k ++; //get<1>(cost[k]), get<2>(cost[k])の親が同じときkを１増やす
    }
    else{
      unite(get<1>(cost[k]), get<2>(cost[k])); //合同じゃなければ合同にする（なぜならsortによって最小値から返されるから）
      ans += get<0>(cost[k]);
      k++;
      anscount++;
    }
  }


  cout << ans << endl;
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define FOR(i,k,n) for(int i = (k); i < (n); i++)
#define REP(i,n) FOR(i,0,n)
#define INF 114514810
#define MOD 1000000007
#define ALL(a) begin(a),end(a)
#define MS(m,v) memset(m,v,sizeof(m))
#define D10  fixed<<setprecision(10)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> P;
typedef long long ll;
struct edge
{
	int from, to, cost;
	bool operator < (const edge& e) const { return cost < e.cost; }
	bool operator >(const edge& e) const { return cost > e.cost; }
};
///*************************************************************************************///
///*************************************************************************************///
///*************************************************************************************///

int V, E;
bool used[10000];
int d[105][105];

int prim()
{
	memset(used, false, sizeof(used));
	int sum = 0;

	priority_queue<edge, vector<edge>, greater<edge>> que;
	que.push(edge{ -1, 0, 0 });

	while (!que.empty())
	{
		edge e = que.top(); que.pop();
		if (used[e.to]) continue;
		sum += e.cost;
		used[e.to] = true;
		REP(i, V)
		{
			if (!used[i]) que.push(edge{ e.to, i, d[e.to][i] });
		}
	}
	return sum;
}

int main()
{
	cin >> V;
	REP(i, V)REP(j, V)
	{
		int t;
		cin >> t;
		d[i][j] = (t == -1 ? INF : t);
	}
	cout << prim() << endl;
}

C++: #include<iostream>
using namespace std;
static const int MAX = 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[MAX][MAX];

int prim() {
	int u, minv;
	int d[MAX], p[MAX], color[MAX];
	
	for(int i = 0; i < n; i++) {
		d[i] = INFTY;
		p[i] = -1;
		color[i] = WHITE;
	}
	
	d[0] = 0;
	
	while(1) {
		minv = INFTY;
		u = -1;
		for(int i = 0; i < n; i++) {
			if(minv > d[i] && color[i] != BLACK) {
				u = i;
				minv = d[i];
			}
		}
	   if(u == -1) break;
	   color[u] = BLACK;
	   for(int 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;
   	           }
           }
   	   }	
	}
	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;
	
}

C++: #include <iostream>
const int MAX_N = 100;
const int INF = 999999999;
const int WHITE = 0;
const int GRAY = 1;
const int BLACK = 2;
using namespace std;

int n;
int color[MAX_N];
int adj[MAX_N][MAX_N];
int d[MAX_N], p[MAX_N];

int prim(void) {
    int u, minv;

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

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

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

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

    return sum;
}

int main(void) {
    cin >> n;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            int v; cin >> v;
            if (v == -1) adj[i][j] = INF;
            else adj[i][j] = v;
        }
    }

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