Go: package main

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

var scanner = bufio.NewScanner(os.Stdin)

func nextString() string {
	scanner.Scan()
	return scanner.Text()
}

func nextInt() int {
	n, err := strconv.Atoi(nextString())
	if err != nil {
		fmt.Printf("strconv.Atoi failed: %v\n", err)
	}
	return n
}

type ElementKey int
type Node struct {
	key    ElementKey
	parent *Node
	rank   int
}

func newNode(key ElementKey) *Node {
	node := Node{}
	node.key = key
	node.parent = &node
	node.rank = 0

	return &node
}

func union(x, y *Node) {
	xSet := findSet(x)
	ySet := findSet(y)
	link(xSet, ySet)
}

func link(x, y *Node) {
	if x.rank > y.rank {
		y.parent = x
	} else {
		x.parent = y

		if x.rank == y.rank {
			y.rank++
		}
	}
}

func findSet(x *Node) *Node {
	if x != x.parent {
		x.parent = findSet(x.parent)
	}
	return x.parent
}

func newDisjointSets(nSets int) *DisjointSets {
	sets := DisjointSets{}
	sets.keyToNode = map[ElementKey]*Node{}
	for i := 0; i < nSets; i++ {
		key := ElementKey(i)
		sets.keyToNode[key] = newNode(key)
	}
	return &sets
}

type DisjointSets struct {
	keyToNode map[ElementKey]*Node
}

func (sets *DisjointSets) union(x, y ElementKey) {
	xNode := sets.keyToNode[x]
	yNode := sets.keyToNode[y]
	union(xNode, yNode)
}

func (sets DisjointSets) findSet(x ElementKey) *Node {
	xNode := sets.keyToNode[x]
	return findSet(xNode)
}

type Edge struct {
	i, j   int
	weight int
}

func main() {
	scanner.Split(bufio.ScanWords)

	n := nextInt()
	sets := newDisjointSets(n)
	edges := []Edge{}
	a := make([][]int, n)
	for i := 0; i < n; i++ {
		a[i] = make([]int, n)
		for j := 0; j < n; j++ {
			a[i][j] = nextInt()
			if a[i][j] >= 0 {
				e := Edge{
					i:      i,
					j:      j,
					weight: a[i][j],
				}
				edges = append(edges, e)
			}
		}
	}
	sort.Slice(edges, func(i, j int) bool { return edges[i].weight < edges[j].weight })
	totalWeight := 0
	for _, edge := range edges {
		i := ElementKey(edge.i)
		j := ElementKey(edge.j)
		if sets.findSet(i) != sets.findSet(j) {
			totalWeight += edge.weight
			sets.union(i, j)
		}
	}
	fmt.Println(totalWeight)
}

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;
}

C++: #include<cstdio>
#include<iostream>
#include<stack>
#include<vector>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#define _USE_MATH_DEFINES // M_PI????????????????????????
#include<math.h>
#include<queue>
#pragma warning(disable:4996)
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;
		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() << "\n";
}

C++: #include<iostream>
#include<stack>
#include<queue>
using namespace std;
#define max 100
#define infty 1<<21

int n;
int aa[max][max];
int color[max];
int d[max];
int p[max];

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

	while (true) {
		int mincost = infty;
		int u = -1;
		for (int i = 0; i < n; i++) {
			if (color[i] != 1 && d[i] < mincost) {
				u = i;
				mincost = d[i];
			}
		}
		if (u == -1) break;
		color[u] = 1;
		for (int i = 0; i < n; i++) {
			if (color[i] != 1 && aa[u][i] != -1) {
				if (aa[u][i] < d[i]) {
					d[i] = aa[u][i];
					p[i] = u;
				}
			}
		}
	}
	for (int i = 0; i < n; i++) {
		if (p[i] != -1) ans += aa[i][p[i]];
	}

	return ans;
}

int main() {
	cin >> n;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			int num1;
			cin >> num1;
			aa[i][j] = num1;
		}
	}

	int kotae = prim();

	cout << kotae << endl;

	return 0;
}