Go: package main

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

const (
	white int = iota
	gray
	black
)
const INFTY int = 2001

var G [][]int
var color []int
var d []int
var p []int
var N int

func prim() int {
	u := 0
	d[u] = 0
	for {
		minCost := INFTY
		for i:=0; i<N; i++ {
			if color[i] != black && d[i] < minCost {
				minCost = d[i]
				u = i
			}
		}

		if minCost == INFTY {
			break
		}

		color[u] = black

		for v:=0; v<N; v++ {
			if color[v] != black && G[u][v] != -1 {
				if G[u][v] < d[v] {
					d[v] = G[u][v]
					p[v] = u
					color[v] = gray
				}
			}
		}
	}
	// sum
	sum := 0
	for i:=0; i<N; i++ {
		if p[i] != -1 {
			sum += G[i][p[i]]
		}
	}
	return sum
}




func initVariable(N int) {
	G = make([][]int, N)
	for i:=0; i<N; i++ {
		G[i] = make([]int, N)
	}
	color = make([]int, N)
	d = make([]int, N)
	p = make([]int, N)
	for i:=0; i<N; i++ {
		d[i] = INFTY
		p[i] = -1
	}
}

func nextInt(scanner *bufio.Scanner) int {
	scanner.Scan()
	num, _ := strconv.Atoi(scanner.Text())
	return num
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	N = nextInt(scanner)
	initVariable(N)
	for i:=0; i<N; i++ {
		for j:=0; j<N; j++ {
			G[i][j] = nextInt(scanner)
		}
	}
	fmt.Printf("%d\n", 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;
}