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 <stdio.h>
#define INF 100000000
#define BLACK 0
#define WHITE 1
#define N 101
 
int n,a[N][N];

int prim(void)
{
  int u,v,i,mincost,d[N],pi[N],total = 0,color[N];
 
  for(u = 1;u <= n;u++){
    d[u] = INF;
    pi[u] = -1;
    color[u] = WHITE;
  }
 
  d[1] = 0;
     
  while(1){
    mincost = INF;
    for(i = 1;i <= n;i++){
      if(color[i] != BLACK && d[i] < mincost){
	mincost = d[i];
	u = i;
      }
    }
     
    if(mincost == INF) break;
    
    color[u] = BLACK;
     
    for(v = 1;v <= n;v++){
      if(color[v] != BLACK && a[u][v] < d[v]){
	pi[v] = u;
	d[v] = a[u][v];
      }
    }
    color[v] = BLACK;
  }
 
  for(i = 1;i <= n; i++){
    if(pi[i] != -1) total += a[i][pi[i]];
  }
 
  return total;
}

 
int main()
{
  int i,j;
 
  scanf("%d",&n);
 
  for(i = 1;i <= n;i++){
    for(j = 1;j <= n;j++){
      scanf("%d",&a[i][j]);
      if(a[i][j] == -1) a[i][j] = INF;
    }
  }
 
   printf("%d\n",prim());
    
   return 0;
 
}

C++: #include <iostream>

const int MAX   = 100;
const int INFTY = (1<<21);
const int WHITE = 0;
const int GRAY  = 1;
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; 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() {
    std::cin >> n;

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

    std::cout << prim() << std::endl;

    return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include<string>
#include <set>
#include <map>
#include <queue>
#include <limits.h>
#include <cmath>

using namespace std;

#define rip(i,n,s) for (int i=(s);i<(int)(n);i++)
#define all(a) (a).begin(),(a).end()
typedef long long ll;

struct UnionFind{
    int size;
    vector<int> par;
    vector<int> rank;
    UnionFind(int x){
        size=x;
        par=vector<int> (x);
        rank=vector<int> (x,1);
        for (int i=0;i<x;i++){
            par[i]=i;
        }
    }
    int root(int x){
        if(par[x]==x){
            return x;
        }
        else{
            return par[x]=root(par[x]);
        }
    }
    void unite(int x,int y){
        x=root(x);y=root(y);
        if(x==y){
            return;
        }
        if(rank[x]<rank[y]){
            par[x]=y;
        }
        else{
            par[y]=x;
        }
        if(rank[x]==rank[y]){
            rank[x]++;
        }
    }
    bool check(int x,int y){
        return root(x)==root(y);
    }
    void initialize(){
        for (int i=0;i<size;i++){
            par[i]=i;
        }
    }
};

typedef pair<int,pair<int,int>> P;

int main(){
    int n;
    cin >> n;
    UnionFind uf(n);
    priority_queue<P,vector<P>,greater<P>> q;
    int v;
    P now;
    rip(i,n,0){
        rip(j,n,0){
            cin >> v;
            if(v>=0 && i>j){
                now.first=v;
                now.second.first=i;
                now.second.second=j;
                q.push(now);
            }
        }
    }
    int co=n-1;
    int ans=0;
    while (co){
        now=q.top();
        q.pop();
        if(uf.check(now.second.first,now.second.second)){
            continue;
        }
        ans+=now.first;
        uf.unite(now.second.first,now.second.second);
        co--;
    }
    printf("%d\n",ans);
}

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