Go: package main

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

const (
	MAX   = 100
	INFTY = 1 << 21
	NIL   = -1
)

const (
	WHITE = iota
	GRAY
	BLACK
)

type Graph [MAX][MAX]int

func (g *Graph) prim(n int) int {
	var (
		u, minv     = 0, 0
		d, p, color [MAX]int
	)

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

	for {
		minv = INFTY
		u = -1
		for 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 v := 0; v < n; v++ {
			if color[v] != BLACK && g[u][v] != INFTY {
				if d[v] > g[u][v] {
					d[v] = g[u][v]
					p[v] = u
					color[v] = GRAY
				}
			}
		}
	}

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

	return sum
}

func answer(reader io.Reader) string {
	sc := bufio.NewScanner(reader)
	sc.Split(bufio.ScanWords)

	sc.Scan()
	n, _ := strconv.Atoi(sc.Text())

	var g Graph
	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			sc.Scan()
			e, _ := strconv.Atoi(sc.Text())
			if e == -1 {
				g[i][j] = INFTY
				continue
			}
			g[i][j] = e
		}
	}

	return fmt.Sprint(g.prim(n))
}

func main() {
	fmt.Println(answer(os.Stdin))
}

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

int x[10000][10000];
int MIN[10000];
int MINX = 0;
int MINZ = 10000000;

int main() {
	memset(MIN, 127, sizeof(MIN));
	int n, a;
	cin >> n;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			cin >> a;
			x[i][j] = a;
		}
	}
	for(int i=0;i<n;i++){
	    for(int j=0;j<n;j++){
	        if(x[i][j]!=-1){
	            MIN[i]=min(x[i][j],MIN[i]);
	        }
	    }
	}
	for(int i=0;i<n;i++){
	    MINX=0;
	    for(int j=0;j<n;j++){
	        MINX += MIN[j];
	    }
	    MINZ=min(MINZ,MINX);
	}
	cout<<MINZ<<endl;
	return 0;
}

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

// 総数を1000000007で割った余り
const long long mod = 1e9 + 7;

using ll  = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define ull unsigned long long
#define ld long double
#define vi vector<int>
#define vll vector<ll>
#define vc vector<char>
#define vs vector<string>
#define vpii vector<pii>
#define vpll vector<pll>

#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; i++)
#define rep1(i, n) for (int i = 1, i##_len = (n); i <= i##_len; i++)
#define repr(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define rep1r(i, n) for (int i = ((int)(n)); i >= 1; i--)

#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

#define SORT(v, n) sort(v, v + n);
// #define SORT(v, n) stable_sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define RSORT(x) sort(rall(x));
#define pb push_back
#define mp make_pair

#define PI (acos(-1))
#define EPS (1e-7)

ull gcd(ull a, ull b) { return b ? gcd(b, a % b) : a; }
ull lcm(ull a, ull b) { return a / gcd(a, b) * b; }

const int MAX   = 100;
const int INF   = (1 << 21);
const int white = 0;
const int gray  = 1;
const int black = 2;

int n, m[MAX][MAX];

int prim() {
    int u, minV;
    int d[MAX], p[MAX], color[MAX];
    rep(i, n) {
        d[i]     = INF;
        p[i]     = -1;
        color[i] = white;
    }

    d[0] = 0;
    while (1) {
        minV = INF;
        u    = -1;
        rep(i, n) {
            if (minV > d[i] && color[i] != black) {
                u    = i;
                minV = d[i];
            }
        }
        if (u == -1) break;
        color[u] = black;
        rep(v, n) {
            if (color[v] != black && m[u][v] != INF) {
                if (d[v] > m[u][v]) {
                    d[v]     = m[u][v];
                    p[v]     = u;
                    color[v] = gray;
                }
            }
        }
    }
    int sum = 0;
    rep(i, n) {
        if (p[i] != -1) sum += m[i][p[i]];
    }
    return sum;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    // cout << fixed << setprecision(5);

    cin >> n;
    rep(i, n) {
        rep(j, n) {
            int e;
            cin >> e;
            m[i][j] = (e == -1) ? INF : e;
        }
    }
    cout << prim() << endl;

    return 0;
}

C++: // Graph II - Minimum Spanning Tree
// 問題URL: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_12_A&lang=jp
//
// 使うライブラリも多く、難しく感じた...
#include <iostream>
#include <algorithm>

using namespace std;

int n, buff, cost[101][101]={}, P[101], M = 0, ans = 0;
pair<int, int> edges[10010], vertices[10010];

void init(int n){
  for(int i = 0; i < n; ++i){
    P[i] = i;
  }
}

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

bool is_same_set(int x, int y){
  return root(x) == root(y);
}

void unite(int x, int y){
  P[root(x)] = root(y);
}

int main(){
  cin >> n;

  for(int row = 0; row < n; ++row){
    for(int col = 0; col < n; ++col){
      cin >> buff;
      cost[row][col] = buff; //隣接行列の読込
    }
  }

  for(int row = 0; row < n; ++row){
    for(int col = 0; col < row; ++col){
      if(cost[row][col]>0){
        edges[M].first = cost[row][col];
        edges[M].second = M;
        // cout << M << ": (" << row << ", " << col << ") = " << edges[M].first << endl;
        vertices[M].first = row, vertices[M].second = col;
        M++; //辺の数を管理、idとして利用
      }
    }
  }

  init(n);

  sort(edges, edges+M); //重みの小さい順に整列

  for(int i = 0; i <= M; ++i){
    int index = edges[i].second; //辺のidを取り出す
    if(!is_same_set(vertices[index].first, vertices[index].second)){ //辺の両端が繋がっていなければ
      unite(vertices[index].first, vertices[index].second); //両端を繋げる
      // cout << "unite " << vertices[index].first << " and " << vertices[index].second << endl;
      ans += edges[i].first; //コストを加える
    }
  }
  cout << ans << endl;
}

C++: #include<bits/stdc++.h>

using namespace std;

int mat[110][110];

typedef pair<int,int> P;

P edge[10010];

bool cmp(P a,P b)
{
    return mat[a.first][a.second]>mat[b.first][b.second];
}

int p[110];

void init(int n)
{
    for(int i=0;i<n;i++)
    {
        p[i]=i;
    }
}

int fnd(int x)
{
    if(p[x]!=x)
    {
        p[x]=fnd(p[x]);
    }
    return p[x];
}

bool unit(int a,int b)
{
    int fa=fnd(a),fb=fnd(b);

    if(fa!=fb)
    {
        p[fa]=fb;
    }
}

bool same(int a,int b)
{
    return fnd(a)==fnd(b);
}

int main()
{

    int n;
    cin>>n;

    int cnt=0;

    init(n);
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            cin>>mat[i][j];

            if(mat[i][j]!=-1)
            {
                edge[cnt++]=make_pair(i,j);
            }
        }
    }

    sort(edge,edge+cnt,cmp);

    int ans=0;
    while(cnt--)
    {
        P tmp=edge[cnt];

        if(!same(tmp.first,tmp.second))
        {
            ans+=mat[tmp.first][tmp.second];
            unit(tmp.first,tmp.second);
        }
    }

    cout<<ans<<endl;

    return 0;
}