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++: #include <iostream>
#include <vector>
#include <queue>
#include <functional>
#include <string.h>

using namespace std;
typedef pair<int, int> P;
struct edge
{
	int to;
	int cost;
};

vector<edge> G[101];
bool isUsed[101];

void solve()
{
	int n;
	cin >> n;
	vector< vector<int> > data((n), vector<int>(n));
	for (int i = 0; i < n; ++i)
	{
		for (int j = 0; j < n; ++j)
		{
			int c;
			cin >> data[i][j];
		}
	}
	for (int i = 0; i < n; ++i)
	{
		for (int j = i; j < n; ++j)
		{
			if (data[i][j] != -1)
			{
				edge e1, e2;
				e1.to = j;
				e1.cost = data[i][j];
				e2.to = i;
				e2.cost = data[j][i];
				G[i].push_back(e1);
				G[j].push_back(e2);
			}
		}
	}
	memset(isUsed, 0, sizeof(isUsed));
	int ans = 0;
	priority_queue < P, vector<P>, greater<P>> pque;
	pque.push(P(0, 0));
	while (!pque.empty())
	{
		P p = pque.top();
		pque.pop();
		int v = p.second;
		if (isUsed[v])
		{
			continue;
		}
		isUsed[v] = true;
		ans += p.first;
		for (int i = 0; i < G[v].size(); ++i)
		{
			edge e = G[v][i];
			pque.push(P(e.cost, e.to));
		}
	}
	cout << ans << endl;
}

int main()
{
	solve();
	return(0);
}

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

const int white = 0;
const int gray = 1;
const int black = 2;
const int Max_n = 100;
const int Infty = (1 << 21);

int M[Max_n][Max_n];
int n;

int prim(){
    int u, minv;
    int color[Max_n], d[Max_n], p[Max_n];
    for(int i=0; i<n; i++){
        color[i] = white;
        d[i] = Infty;
        p[i] = -1;
    }

    d[0] = 0;

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

        color[u] = black;

        for(int v=0; v<n; v++){
            if(color[v] != black && M[u][v] != Infty){
                if(M[u][v] < d[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;


    return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#define maxn 101
#define Max 10000 
using namespace std;
int n,ans=0;
int map[maxn][maxn];

//struct edge
//{
//	int u;
//	int w;
//	edge(int u=0,int w=Max):u(u),w(w){}
//	bool operator <???(const node& rhs) const
//	{
//		return w>rhs.w;
//	} 
//};

void prim()
{
	int d[maxn];
	bool vis[maxn];
	memset(vis,0,sizeof(vis));
	for(int i=1;i<=n;++i)
	{
		d[i]=Max;
		vis[i]=0;
	}
	d[1]=0;
	while(1)
	{
		int MIN=Max;
		int u=-1;
		for(int i=1;i<=n;++i)
			if(MIN>d[i]&&!vis[i])
			{
				MIN=d[i];
				u=i;
			}
		if(u==-1) break;
		vis[u]=1;
		ans+=d[u];
		for(int i=1;i<=n;++i)
		{
			if(!vis[i] && map[u][i]!=Max)
			  if(d[i]>map[u][i])
			  	d[i]=map[u][i];
		}
	}
	return;
}

int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;++i)
	  for(int j=1;j<=n;++j)
	  {
	  	int x;
	  	scanf("%d",&x);
	  	if(x!=-1)
	  	  map[i][j]=x;
	  	else
	  	  map[i][j]=Max;
	  }
	prim();
	printf("%d\n",ans);
	return 0;
}

C++: // AOJ ALDS 1_12_A
#include<cstdio>
#include<algorithm>
#include<vector>
#define rep(i,a) for( int i = 0; i != (a); ++i )

struct edge
{
	int from, to, cost;

	edge( int from, int to, int cost )
	:	from( from ), to( to ), cost( cost )
	{}
};

int n;
std::vector<edge> es;

int par[100], rank[100];

void init()
{
	rep( i, n )
		par[i] = i, rank[i] = 0;

	return;
}

int find( int x )
{
	if( x == par[x] )
		return x;
	else
		return par[x] = find( par[x] );
}

void unite( int x, int y )
{
	x = find( x );
	y = find( y );
	if( x == y )
		return;

	if( rank[x] < rank[y] )
		par[x] = y;
	else
	{
		par[y] = x;
		if( rank[x] == rank[y] )
			++rank[x];
	}

	return;
}

bool same( int x, int y )
{
	return find( x ) == find( y );
}

int main()
{
	scanf( "%d", &n );
	rep( i, n )
	{
		rep( j, n )
		{
			int a;
			scanf( "%d", &a );
			if( ~a )
				es.push_back( edge( i, j, a ) );
		}
	}

	std::sort( es.begin(), es.end(), []( const edge& e1, const edge& e2 ){ return e1.cost < e2.cost; } );

	init();

	int ans = 0;
	rep( i, es.size() )
	{
		if( !same( es[i].from, es[i].to ) )
		{
			ans += es[i].cost;
			unite( es[i].from, es[i].to );
		}
	}

	printf( "%d\n", ans );

	return 0;
}

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

void print(const vector<int> &v) {
    cout << v[0];
    for (int i = 1, size = v.size(); i < size; i++) {
        cout << " " << v[i];
    }
    cout << endl;
}

int main() {
    int n;
    while (cin >> n) {
        vector<int> a(n);
        for (int i = 0; i < n; i++) {
            cin >> a[i];
        }
        print(a);
        for (int j = 1; j < n; j++) {
            int key = a[j];
            int i = j - 1;
            while (0 <= i && key < a[i]) {
                a[i + 1] = a[i];
                i--;
            }
            a[i + 1] = key;
            print(a);
        }
    }
}