Go: package main

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

var (
	n int
	M [MAX][MAX]int
)

const MAX = 100
const WHITE = 0
const GRAY = 1
const BLACK = 2

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Scan()
	n, _ = strconv.Atoi(sc.Text())

	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			sc.Scan()
			c, _ := strconv.Atoi(sc.Text())
			if c != -1 {
				M[i][j] = c
			} else {
				M[i][j] = math.MaxInt64
			}

		}
	}
	prim()
}

func prim() {
	var u int
	var minv int
	var d [MAX]int
	var p [MAX]int
	var color [MAX]int

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

	d[0] = 0

	for {
		minv = math.MaxInt64
		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 && M[u][v] != math.MaxInt64 {
				if d[v] > M[u][v] {
					d[v] = M[u][v]
					p[v] = u
					color[v] = GRAY
				}
			}
		}
	}
	sum := 0
	for i := 0; i < n; i++ {
		if p[i] != -1 {
			sum += M[i][p[i]]
		}
	}
	fmt.Println(sum)

}

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

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 (true) {
    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>
  
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 a, min;
   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 ) {
      min = INFTY;
      a = -1;
  
      for ( int i = 0; i < n; i++ ) {
         if ( min > d[i] && color[i] != BLACK ) {
            a = i;
            min = d[i];
         }
      }
  
      if ( a == -1 ) break;
      color[a] = BLACK;
      for ( int v = 0; v < n; v++ ) {
         if ( color[v] != BLACK && M[a][v] != INFTY ) {
            if ( d[v] > M[a][v] ) {
               d[v] = M[a][v];
               p[v] = u;
               color[v] = GRAY;
            }
         }
      }
   }
  
   int sum = 0;
   for ( int i = 0; i < n; i++ ) {
      if ( p[i] != -1 ) sum = 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;
}