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++: // 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<cstdio>
#include<functional>
#include<algorithm>
#define F first
#define SF second.first
#define SS second.second
using namespace std;
typedef pair<int,int> P;
typedef pair<int,P> P2;
int main(void)
{
	int i,j,n,a,par[100],leng[100],sum,p1,cnt,s,t;
	P2 pp[100*99/2];
	scanf("%d",&n);
	p1=0;
	for(i=0;i<n;i++)	{
		for(j=0;j<n;j++)	{
			scanf("%d",&a);
			if(i<j && a!=-1)	{
				pp[p1].F=a;
				pp[p1].SF=i;
				pp[p1].SS=j;
				p1++;
			}
		}
	}
	sort(pp,pp+p1);
	for(i=0;i<n;i++)	{
		leng[i]=1;	par[i]=i;
	}
	cnt=1;	sum=0;
	for(i=0;i<p1;i++)	{
		s=pp[i].SF;	t=pp[i].SS;
		while(par[s]!=s)	s=par[s];
		while(par[t]!=t)	t=par[t];
		if(s!=t)	{
			if(leng[s]>leng[t])	par[t]=s;
			else if(leng[s]<leng[t])	par[s]=t;
			else {
				par[s]=t;	leng[t]++;

			}
			sum+=pp[i].F;
			cnt++;
			if(cnt==n)	break;
		}
	}
	printf("%d\n",sum);
	return 0;
}

C++: #include<iostream>
using namespace std;
static const int whi=0;
static const int gra=1;
static const int bla=2;
static const int MAXX=(1<<21);
int color[110];
int a[110][110];
int minni[110];
int parent[110];
int n;

int prim(){
  int b=0;
  int minmin=0;
  int result=0;
  for(int i=0;i<n;i++){
    color[i]=whi;
    minni[i]=MAXX;
    parent[i]=-1;
  }
  minni[0]=0;
  while(1){
    minmin=MAXX;
    b=-1;
    for(int i=0;i<n;i++){
      if(color[i]!=bla&&minni[i]<minmin){
	b=i;
	minmin=minni[i];
	
      }
    }
    if(b==-1){
      break;
    }
    color[b]=bla;
    for(int j=0;j<n;j++){
      if(color[j]!=bla&&a[b][j]!=MAXX){
	if(a[b][j]<minni[j]){
	  minni[j]=a[b][j];
	  parent[j]=b;
	  color[j]=gra;
	}
      }
    }
  }
  for(int i=0;i<n;i++){
    if(parent[i]!=-1) result+=a[i][parent[i]];
  }
  return result;
}

int main(){
  cin>>n;

  int s;
  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
      cin>>s;
      a[i][j]=(s==-1) ? MAXX : s;
    }
  }
  cout<<prim()<<endl;
}

C++: #include <bits/stdc++.h>
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,min,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){
    min=INFTY;
    u=-1;
    for(int i=0;i<n;i++){
      if(min>d[i] && color[i]!=BLACK){
        u=i;
        min=d[i];
      }
    }
    if(u==-1) break;
    color[u]=BLACK;
    for(int j=0;j<n;j++){
      if(color[j]!=BLACK && m[u][j]!=INFTY){
        if(d[j]>m[u][j]){
          d[j]=m[u][j];
          p[j]=u;
          color[j]=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 <climits>
using namespace std;

int n;
int g[101][101];

int d[101];
char c[101];
int p[101];

void prim() {
    for (int i = 0; i < n; ++i) {
        d[i] = INT_MAX;
        c[i] = 'w';
    }
    c[0] = 'b';
    d[0] = 0;
    while (true) {
        int u, v;
        int mine = INT_MAX;
        for (int j = 0; j < n; ++j) {
            if (c[j] == 'b') {
                u = j;
                for (int k = 0; k < n; ++k) {
                    if(g[u][k] > -1 && g[u][k] <= d[k] && c[k] != 'b' && g[u][k] <= mine) {
                        d[k] = g[u][k];
                        mine = d[k];
                        v = k;
                    }
                }
            }
        }
        p[v] = u;
        c[v] = 'b';
        if(mine == INT_MAX) break;
    }
}

int main() {
    cin >> n;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            int w;
            cin >> w;
            g[i][j] = w;
        }
    }
    prim();
    int sum;
    for (int k = 0; k < n; ++k) {
        sum += d[k];
    }
    cout << sum << endl;


    return 0;
}