Go: package main

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

//util
var sc = bufio.NewScanner(os.Stdin)

func nextStr() string {
	sc.Scan()
	return sc.Text()
}

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func printList(list []int) {
	n := len(list)
	buf := bufio.NewWriter(os.Stdout)
	for _, v := range list[0 : n-1] {
		buf.Write([]byte(fmt.Sprintf("%d ", v)))
	}
	buf.Write([]byte(fmt.Sprintf("%d\n", list[n-1])))
	buf.Flush()
}

func max(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

func min(a, b int) int {
	if a > b {
		return b
	} else {
		return a
	}
}

//solve
const (
	MAX   = 100
	INFTY = (1 << 21)
	WHITE = 0
	GRAY  = 1
	BLACK = 2
)

//V: グラフの頂点全体の集合
//T: MSTに属する頂点の集合
var (
	n     int
	M     [MAX][MAX]int //重みを記録する隣接行列
	color [MAX]int
	p     [MAX]int //MSTにおける頂点の親を記録
	d     [MAX]int //Tに属する頂点と、V−Tに属する頂点を繋ぐ辺の中で、重みが最小のものを記録
)

func prim() int {
	var u, minv int
	//初期化
	for i := 0; i < n; i++ {
		d[i] = INFTY
		p[i] = -1
		color[i] = WHITE
	}

	d[0] = 0

	for {
		//頂点uを決めていく
		//つまり、Tに属する頂点と、V−Tに属する頂点を繋ぐ辺の中で、重みが最小のものを決める
		minv = INFTY
		u = -1
		for i := 0; i < n; i++ {
			//未訪問かつ、辺の重みが最小となるuを選ぶ
			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] != INFTY {
				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() {
	sc.Split(bufio.ScanWords)
	n = nextInt()

	for i := 0; i < n; i++ {

		for j := 0; j < n; j++ {
			tmp := nextInt()
			if tmp == -1 {
				tmp = INFTY
			}
			M[i][j] = tmp
		}

	}

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