Go: package main

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

const (
	MAX = 100
	INF = 1 << 21
	WHITE = 0
	GRAY = 1
	BLACK = 2
)

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

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

	for i := 0; i < n; i++ {
		d[i] = INF
		p[i] = -1
		color[i] = WHITE
	}
	d[0] = 0
	for {
		minv = INF
		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] != INF {
				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]]
		}
	}
	return sum
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Scan()
	n, _ = strconv.Atoi(scanner.Text())
	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			scanner.Scan()
			e, _ := strconv.Atoi(scanner.Text())
			if e == -1 {
				M[i][j] = INF
			} else {
				M[i][j] = e
			}
		}
	}
	fmt.Println(prim())
}

C++: // ????°???¨?????¨

#include <cstdio>
using namespace std;

static const int MAX = 100;
static const int INFTY = ( 1 << 21 );
static const int WHITE = 1;
static const int GLAY  = 2;
static const int BLACK = 3;

int M[MAX][MAX];
int d[MAX], p[MAX], color[MAX];

// ??????????????¢?????´?????????
int prim( int n )
{

	int mincost, u, msum = 0;

	// color?????????WHITE(????¨????)???d??????(????°??????????????¨????????????????)???INFTY???p??????(????°???¨?????¨?§????????¬????????£???¨?????????????????????????¨???¶????????????)???-1??§?????????????????????
	for( int i = 0; i < n; i++ ) { color[i] = WHITE; d[i] = INFTY; p[i] = -1; }

	// ????????????????¨?????????????????????????????????????????´????0(????????????)
	d[0] = 0;

	while( 1 ) {

		mincost = INFTY;	// ????°?????????????INFTY??§?????????
		u		= -1;		// ??????????£????????????????-1??§?????????(????¨??????¨????????????)

		// d???????????°??????????¨??????????????°????????????????????°?????????£?????´??????????°?????????????d[i]?????????????£????????????????i(?????¶???????°??????????????????????)??¨??????
		for( int i = 0; i < n; i++ ) {
			if( color[i] != BLACK && d[i] < mincost ) { mincost = d[i]; u = i; }
		}

		// ??????????£????????????????-1(????????\????????¢?´¢????????????)?????¨???while??????break
		if( u == -1 ) break;

		// ??????????£???°????¨????????????¨??????
		color[u] = BLACK;

		// ????????°?????°???
		for( int v = 0; v < n; v++ ) {

			// ?????¨??°?????????????¨????????????§????????????????????°????????????????????¨??????(INFTY??§?????????)??¨???
			if( color[v] != BLACK && M[u][v] != INFTY ) {
				// ???????????????????°????????????????????°???????????????§?????£?????¨???
				if( M[u][v] < d[v] ) {

					d[v]	 = M[u][v];		// ????°????????????????????????????
					p[v]	 = u;			// ????°?????£???°?????????????°???°(????????????)?????????
					color[v] = GLAY;		// ?¨??????¶??????MST????±???????????????¨??????

				}
			}

		}

	}

	for( int i = 0; i < n; i++ ) {
		if( p[i] != -1 ) msum += M[i][p[i]];	// ???????????????-1(?????¨??????????°???¨?????¨????????????)??§?????£?????´????????°????????????????????????????¶?????????????
	}

	return msum;	// ????°???¨?????¨?????¨??????????????????????????¨??????

}

int main()
{

	int n, a;

	// ????????°
	scanf( "%d", &n );

	for( int i = 0; i < n; i++ ) {
		for( int j = 0; j < n; j++ ) {

			// ??£??\?????????????´??????\???
			// -1?????\??????????????´?????????????????????(INFTY??£??\)
			scanf( "%d", &a );
			M[i][j] = ( a == -1 ) ? INFTY : a;
		
		}
	}

	// prim??¢??°???????????????printf
	printf( "%d\n", prim( n ) );

	return 0;
}

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

void init(int n, vector<int>& v){
  v.resize(n);
  for(int i = 0; i < n; i++){
    v[i] = i;
  }
}

int root(int a, vector<int>& v){
  if(v[a] == a) return a;
  return (v[a] = root(v[a], v));
}

bool same(int a, int b, vector<int>& v){
  return root(a,v) == root(b,v);
}

void unite(int a, int b, vector<int>& v){
  v[root(a,v)] = root(b,v);
}


int main(){
  int n;
  cin >> n;
  vector<vector<int> > a(n, vector<int>(n, -1));
  for(int i = 0; i < n; i++)
    for(int j = 0; j < n; j++)
      cin >> a[i][j];

  vector<int> cost, src, dst;
  for(int i = 0; i < n; i++){
    for(int j = i+1; j < n; j++){
      if(a[i][j] > -1){
        cost.push_back(a[i][j]);
        src.push_back(i);
        dst.push_back(j);
      }
    }
  }
  vector<pair<int, int> > edges(cost.size());

  for(int i = 0; i < cost.size(); i++){
    // cout << "cost" << cost[i] << endl;
    // cout << "i" << i << endl;
    edges[i] = make_pair(cost[i], i);
    // cout << edges[i].first << " : " << edges[i].second << endl;
  }

  sort(edges.begin(),edges.end());

  for(int i = 0; i < cost.size(); i++)
    cout << edges[i].first << " : " << edges[i].second << endl;

  int sum = 0;
  vector<int> units;
  init(n, units);
  for(int i = 0; i < edges.size(); i++){
    int idx = edges[i].second;
    int s = src[idx], d = dst[idx];
    // cout << s << "," << d << endl;
    if(same(s, d, units))
      continue;
    unite(s, d, units);

    sum += cost[idx];
  }

  cout << sum << endl;
}

C++: #include<iostream>
#include<utility>
#include<algorithm>
using namespace std;
int matrix[100][100];
pair<int,int> edges[100];
int cost[100];
int src[100];
int dst[100];
int p[10010];
void init(int n)
{
    for(int i=0;i<=n;i++)
        p[i]=i;
};
int root(int a)
{
    if(p[a]==a)
        return a;
    return (p[a]=root(p[a]));
};
bool is_same_set(int a,int b)
{
    return root(a)==root(b);
};
void unite(int a,int b)
{
    p[root(a)]=root(b);
};

int main()
{
    int n,amount=0,sum=0;
    
    cin>>n;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            cin>>matrix[i][j];
        }
    }
    
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<=i;j++)
        {
            if(matrix[i][j]!=-1)
            {
                cost[amount]=matrix[i][j];
                src[amount]=i;
                dst[amount]=j;
                amount++;
            }
        }
    }
    for(int i=0;i<amount;i++)
    {
        edges[i]=make_pair(cost[i],i);
    }

	init(n);
    
    sort(edges,edges+amount);
    
    for(int i=0;i<amount;i++)
    {
        if(is_same_set(src[edges[i].second()],dst[edges[i].second()]))
			continue;
		unite(src[edges[i].second()],dst[edges[i].second()]);
		sum+=edges[i].first();
    }
	cout<<sum<<endl;
}

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

const int INF = 1 << 20;
const int MAX_N = 110;

int main() {
  int cost[MAX_N][MAX_N];
  int mincost[MAX_N];
  bool used[MAX_N];
  int n, a, res;

  cin >> n;

  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      cin >> a;
      cost[i][j] = a == -1 ? INF : a;
    }
  }

  for (int i = 0; i < n; ++i) {
    mincost[i] = INF;
    used[i] = false;
  }

  mincost[0] = 0;
  res = 0;

  while (true) {
    int v = -1;
    for (int u = 0; u < n; ++u) {
      if (!used[u] && (v == -1 || mincost[u] < mincost[v])) v = u;
    }

    if (v == -1) break;

    used[v] = true;
    res += mincost[v];

    for (int u = 0; u < n; ++u) {
      mincost[u] = min(mincost[u], cost[v][u]);
    }
  }

  cout << res << endl;
  
  return 0;
}