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++: // AOJ ALDS 1_12_A
#include<cstdio>
#include<algorithm>
#include<vector>
#define rep(i,a) for( int i = 0; i != (a); ++i )

struct edge
{
	int from, to, cost;

	edge( int from, int to, int cost )
	:	from( from ), to( to ), cost( cost )
	{}
};

int n;
std::vector<edge> es;

int par[100], rank[100];

void init()
{
	rep( i, n )
		par[i] = i, rank[i] = 0;

	return;
}

int find( int x )
{
	if( x == par[x] )
		return x;
	else
		return par[x] = find( par[x] );
}

void unite( int x, int y )
{
	x = find( x );
	y = find( y );
	if( x == y )
		return;

	if( rank[x] < rank[y] )
		par[x] = y;
	else
	{
		par[y] = x;
		if( rank[x] == rank[y] )
			++rank[x];
	}

	return;
}

bool same( int x, int y )
{
	return find( x ) == find( y );
}

int main()
{
	scanf( "%d", &n );
	rep( i, n )
	{
		rep( j, n )
		{
			int a;
			scanf( "%d", &a );
			if( ~a )
				es.push_back( edge( i, j, a ) );
		}
	}

	std::sort( es.begin(), es.end(), []( const edge& e1, const edge& e2 ){ return e1.cost < e2.cost; } );

	init();

	int ans = 0;
	rep( i, es.size() )
	{
		if( !same( es[i].from, es[i].to ) )
		{
			ans += es[i].cost;
			unite( es[i].from, es[i].to );
		}
	}

	printf( "%d\n", ans );

	return 0;
}

C++: #include<iostream>

using namespace std;

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


int n, M[MAX][MAX];;

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

  for (int i = 0; i < n; i++) {
    d[i] = INFTY;
    p[i] = -1;
    color[i] = WHITE;
  }

  d[0] = 0;

  while (1) {
    minv = INFTY;
    u = -1;
    for (int 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 (int v = 0; i < n; i++) {
      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;
        }
      }
    }
  }
  int sum = 0;
  for (int i = 0; i < n; i++) {
    if (p[i] != -1) {
      sum += M[i][p[i]]
    }
  }

  return sum;
}

int main() {
  cin >> n;

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      int e; cin >> e;
      M[i][j] = (e == -1) ? INFTY : e;
    }
  }

  cout << prim() << endl;

  return 0;
}

C++: #include <iostream>
#include <stack>
using namespace std;
static const int MAX_V = 100;
static const int INF = 10000000;
int cost[MAX_V][MAX_V];
bool wasUsed[MAX_V];
int mincost[MAX_V];
//n???????????°
int n;

int prim(){
    for(int i=0;i<n;i++){
        wasUsed[i] = false;
        mincost[i] = INF;
    }
    mincost[0] = 0;
    int res = 0;
    while(true){
        int v = -1;
        for(int u =0;u<n;u++){
            if(!wasUsed[u] && (v==-1||mincost[u] < mincost[v]))
               v = u;
        }
        if(v==-1) break;
        wasUsed[v] = true;
        res += mincost[v];
        
        for(int u=0;u<n;u++)
            mincost[u] = min(mincost[u],cost[v][u]);
    }
    return res;
}

int main(int argc, const char * argv[]) {
    cin >> n;
    
    for(int i=0;i<n;i++)
        for(int j=0;j<n;j++){
            int a;
            cin >> a;
            if(a == -1) a = INF;
            cost[i][j] = a;
        }
    
    cout << prim() << endl;
    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;

static const int MAX = 100;
static const int INF = (1<<21);
static const int WHI = 0;
static const int GRA = 1;
static const int BLA = 2;

int n, m[MAX][MAX];

int prim(){
  int u,minv;
  int d[MAX], p[MAX], color[MAX];
  
  for ( int i = 0; i < n ; ++i) {
    d[i] = INF;
    p[i] = -1;
    color[i] = WHI;
  }
  d[0] = 0;
  
  while (1) {
    minv = INF;
    u = -1;
    for ( int i = 0; i < n ; ++i) {
      if ( minv > d[i] && color[i] != BLA ) {
	u = i;
	minv = d[i];
      }
    }
    if ( u == -1 ) {
      break; 
    }
    color[u] = BLA;
    
    for ( int v = 0; v < n ; ++v) {
      if ( color[v] != BLA && m[u][v] != INF) {
	if ( m[u][v] < d[v] ) {
	  d[v] = m[u][v];
	  p[v] = u;
	  color[v] = GRA;
	}
      }
    }
  }
  int sum = 0;
  for ( int i = 0; i < n ; ++i) {
    if ( p[i] != -1 ) {
      sum += m[i][p[i]];
    }
  }
  return sum;
}

  
main()
{
  cin >> n;
  for ( int i = 0; i < n ; ++i) {
    for ( int j = 0; j < n ; ++j) {
      int e;
      cin >> e;
      if ( e == -1 ) {
	m[i][j] = INF;
      } else {
	m[i][j] = e;
      }
    }
  }

  cout << prim() << endl;

  return (0);
}

C++: #include<iostream>
using namespace std;
static const int MAX = 100;
static const int INFTY = (1<<21);
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;
    int d[MAX],p[MAX],color[MAX];

    for(int i=0;i<n;i++){
        d[i] = INFTY; 
        p[i] = -1;
        color[i] = WHITE;
    }
    d[0] = 0;

    while(1){
        minv = INFTY;
        u = -1;
       // 最適点(コスト最小点)を探す
        for(int i=0;i<n;i++){
            if(minv > d[i] && color[i] != BLACK){
                u = i;
                minv = d[i];
            }
        }
        if(u == -1) break; // uに対しての辺がなかった場合
        color[u] = BLACK;
        
        // 次点の捜索
        for(int 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;
                }
            }
        }
    }
    int sum =0;
    for(int i=0;i<n;i++){
        if(p[i] != -1) sum +=M[i][p[i]];
    }
    return sum;
} 

int main(){
    cin >> n;

    for(int i=0;i<n;i++){
        for(int j =0;j<n;j++){
            int e; cin >> e;
            M[i][j] = (e== -1) ? INFTY : e;
        }
    }
    cout << prim() << endl;
    return 0;
}