Go: package main

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

const (
	WHITE int = iota
	GRAY
	BLACK
)

type graph struct {
	G [][]int
	C []int
	D []int
	P []int
}

var INFTY = 1 << 32

func constructGraph() *graph {
	sc := bufio.NewScanner(os.Stdin)
	sc.Scan()
	n, _ := strconv.Atoi(sc.Text())

	g := make([][]int, n)
	c := make([]int, n)
	d := make([]int, n)
	p := make([]int, n)

	for i := 0; i < n; i++ {
		sc.Scan()
		fields := strings.Fields(sc.Text())
		g[i] = toIntSlice(fields)
	}

	return &graph{g, c, d, p}
}

func (g *graph) initMst() {
	for i := 0; i < len(g.G); i++ {
		g.C[i] = WHITE
		g.D[i] = INFTY
	}
	g.D[0] = 0
	g.P[0] = -1
}

func (g *graph) primMst() int {
	g.initMst()

	var u int
	for {
		mincost := INFTY
		for i := 0; i < len(g.G); i++ {
			if g.C[i] != BLACK && g.D[i] < mincost {
				mincost = g.D[i]
				u = i
			}
		}

		if mincost == INFTY {
			break
		}

		g.C[u] = BLACK

		for i := 0; i < len(g.G); i++ {
			if g.C[i] != BLACK && g.existEdge(u, i) {
				if g.G[u][i] < g.D[i] {
					g.D[i] = g.G[u][i]
					g.P[i] = u
				}
			}
		}
	}

	totalCost := 0
	for _, cost := range g.D {
		totalCost += cost
	}
	return totalCost
}

func (g *graph) existEdge(u, v int) bool {
	return g.G[u][v] != INFTY && g.G[v][u] != INFTY
}

func toIntSlice(s []string) []int {
	a := make([]int, len(s))
	for i, v := range s {
		num, _ := strconv.Atoi(v)
		if num == -1 {
			num = INFTY
		}
		a[i] = num
	}
	return a
}

func main() {
	g := constructGraph()
	fmt.Println(g.primMst())
}

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