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<iostream>
using namespace std;

static const int MAX = 100;
static const int INF = (1<<30);
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,ii,vv;
  int d[MAX],p[MAX],color[MAX];

  //cout << "prim" << endl;
  //条件初期化
  for(ii=0;ii<n;ii++){
    d[ii] = INF;
    p[ii] = -1;
    color[ii] = WHITE;
  }

  d[0] = 0;

  while(1){

    minv = INF;
    u = -1;

    //起点の決定
    for(ii=0;ii<n;ii++){
      if(minv > d[ii] && color[ii] != BLACK){
        //cout << "ii:" << ii << endl;
        u = ii;
        minv = d[ii];
      }
    }

    if(u == -1){
      break;
    }

    //起点を探索済みに
    color[u] = BLACK;

    for(vv=0;vv<n;vv++){
      //未探索かつパスの繋っている位置
      if(color[vv] != BLACK && M[u][vv] != INF){
        if( d[vv] > M[u][vv]){
          d[vv] = M[u][vv];
          p[vv] = u;
          color[vv] = GRAY;
        }
      }
    }
  }

  int sum = 0;
  for(ii=0;ii<n;ii++){
    if(p[ii] != -1){
      sum += M[ii][p[ii]];
    }
  }
  
  return sum;
  
}

int main(){
  cin >> n;
  int ii,jj;

  for(ii=0;ii<n;ii++){
    for(jj=0;jj<n;jj++){
      int cost;
      cin >> cost;

      if(cost == -1){
        M[ii][jj] = INF;
      }
      else{
        M[ii][jj] = cost;
      }
    }
  }

  cout << prim() << endl;
  
}

C++: #include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <string>
#include <map>
#include <bitset>
#include <vector>
#include <queue>

using namespace std;

typedef long long ll;
#define FOR(i,a,b)   for(ll i = (a); i < (b); i++ )
#define REP(i, n)    FOR(i,0,n)
typedef pair< int, int >   cp2;
typedef pair< int, cp2 >   cp3;
#define fi first
#define se second
#define sec  se.fi
#define thr  se.se
const ll mod = 1000000007;
//              123456789


 

///////////////////////////////////////////////
//
//
///////////////////////////////////////////////

////////////////////////////////////////////////
////////////////////////////////////////////////



priority_queue< cp3, vector<cp3>, greater<cp3> >p;
cp3 now;

int g[112];

int find( int key ){
	//cout<<key<<" "<<g[key]<<endl;
	if( g[key] < 0 ) return key;
	return g[key] = find( g[key] );
}

void unite( int x, int y ){
	x = find( x );
	y = find( y );
	if( x == y ) return;
	
	if( g[x] < g[y] ) swap( x, y );
	if( g[x] == g[y] ) g[y]--;//??±??????x???????????????
	//g[y] += g[x];		 //????????°???x??????????°???????
	g[x] = y;
}

int a[112][112];
int N;
int ans = 0;

int main(){
	
	cin>>N;
	
	fill( g, g+112, -1 );
	
	
	REP( i, N ){
		REP( j, N ){
			scanf("%d",&a[i][j]);
			if( a[i][j] != -1 ){
				p.push( cp3( a[i][j], cp2(i, j) ) );
				//printf("%d %d %d\n",p[idx].fi, p[idx].sec, p[idx].thr);
				
			}
		}
	}
	
	while( !p.empty() ){
		now = p.top();
		p.pop();
		
		if( find(now.sec) != find(now.thr) ){
			unite( now.sec, now.thr );
			ans += now.fi;
		}
	}
	
	cout<<ans<<endl;
	
	return 0;

}

C++: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MAX 100
#define WHITE 0
#define GRAY 1
#define BLACK 2

int G[MAX][MAX];
int color[MAX], d[MAX], p[MAX];
int n;

int prim();

int main(){
    cin >> n;
    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            cin >> G[i][j];
            if(G[i][j] == -1) G[i][j] = INT_MAX;
        }
    }

    cout << prim() << endl;

    // for(int i = 0; i < n; i++){
    //     cout << i << " " << p[i] << endl;
    // }

    return 0;
}

int prim(){
    // プリムのアルゴリズム
    // 重み付きグラフから最小全域木を求め、その辺の重みの合計を求める
    // colorは訪問状態を表す　
    // d[i]は頂点iから繋がる辺の内最小のもの　
    // p[i]は頂点iの親（木なので）
    for(int i = 0; i < n; i++){
        color[i] = WHITE;
        d[i] = INT_MAX;
        p[i] = -1;
    }
    
    d[0] = 0;

    while(1){
        int min = INT_MAX, u = -1;
        
        // 隣接する最小の辺を持つところに行く 頂点番号u 辺のおもみmin
        for(int i = 0; i < n; i++){
            if(d[i] < min && color[i] != BLACK){
                min = d[i];
                u = i;
            }
        }

        // 隣接するところが全てblack、または最小のものが存在しない時は探索終了
        if(u == -1) break;

        // 頂点番号uのところに訪問
        color[u] = BLACK;

        // uから繋がるところでまだ訪問してないところの内、最小のものを見つける
        for(int i = 0 ; i < n; i++){
            if(color[i] != BLACK && G[u][i] != INT_MAX){
                if(d[i] > G[u][i]){
                    d[i] = G[u][i];
                    p[i] = u;
                    color[i] = GRAY;
                }
            }
        }
    }

    int sum = 0;
    for(int i = 0; i < n; i++){
        if(p[i] != -1) sum += G[i][p[i]];
    }

    return sum;
}

C++: #include<cstdio>
#include<iostream>
#include<algorithm>
#define Inf 1<<21
#define Lens 105

using namespace std;
int N;
int w[Lens][Lens], p[Lens], d[Lens];
char c[Lens];

int prime()
{
	d[0] = 0;
	int num = 0;
	while (true)
	{
		int minv = Inf;
		int u = -1;
		for (int i = 0; i < N; i++)
		{
			if (minv > d[i] && c[i] == 'w')
			{
				minv = d[i];
				u = i;
			}
		}
		if (u == -1) break;
		c[u] = 'b';
		for (int i = 0; i < N; i++)
		{
			if (c[i] == 'w'&&w[u][i] != -1)
				if (d[i] > w[u][i])
				{
					d[i] = w[u][i];
					p[i] = u;
				}
		}
	}
	int sum = 0;
	for (int i = 1; i < N; i++)
	{
		sum = sum + w[i][p[i]];
	}
	return sum;
}

int main()
{
	cin >> N;
	for (int i = 0; i < N; i++)
	{
		c[i] = 'w';
		d[i] = Inf;
		p[i] = -1;
		for (int j = 0; j < N; j++)
		{
			int k;
			cin >> k;
			w[i][j] = (k == -1) ? Inf : k;
		}
	}
	cout << prime() << endl;
	return 0;
}