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++: #include<bits/stdc++.h>
#define wgt(x) (x.first)
#define s(x) (x.second.first)
#define t(x) (x.second.second)
#define mwe(w,s,t) make_pair(w,make_pair(s,t))

using namespace std;

typedef pair<int,int> edge;
typedef pair<int,edge> we;

int main()
{
	int n,G[100][100];
	bool used[100];
	scanf("%d",&n);
	for(int i=0;i<n;i++)
		for(int j=0;j<n;j++)
			scanf("%d",&G[i][j]);
	memset(used,false,sizeof(used));
	priority_queue<we,vector<we>,greater<we> > pq;
	int tot=0;
	used[0]=true;
	for(int i=0;i<n;i++)
		if(G[0][i]>=0)
			pq.push(mwe(G[0][i],0,i));
	while(!pq.empty())
	{
		we t=pq.top();pq.pop();
		if(used[t(t)]) continue;
		tot+=wgt(t);
		used[t(t)]=true;
		for(int i=0;i<n;i++)
			if(!used[i]&&G[t(t)][i]>=0)
				pq.push(mwe(G[t(t)][i],t(t),i));
	}
	printf("%d\n",tot);
	return 0;
}

C++: #include <cstdio>
#include <limits>

static const int MAX_N   = 100;
static const int MAX_INT = std::numeric_limits<int>::max();
static const int UNKNOWN = 0;
static const int IN_MST  = 1;

int M[MAX_N][MAX_N];

// 任意の頂点uを起点としてMSTを構成する頂点集合Tを定義し、
// MSTに全ての頂点が含まれるまで、Tの周囲の辺のうち重みが
// 最小の辺を都度選択してTに加えていく。
int prim(int n) {
    int *costs   = new int[n];
    int *parents = new int[n];
    int *states  = new int[n];

    // 各変数を初期化する。
    for (int i = 0; i < n; i++) {
        costs[i]   = MAX_INT;
        parents[i] = -1;
        states[i]  = UNKNOWN;
    }

    costs[0] = 0;

    while (true) {
        int mincost = MAX_INT;
        int u = -1;

        // Tの周囲の辺のうち重みが最小の辺およびそれにつながる頂点を探す。
        for (int i = 0; i < n; i++) {
            if ( states[i] != IN_MST && costs[i] < mincost ) {
                u = i;
                mincost = costs[i];
            }
        }
        if ( u == -1 ) break;

        states[u] = IN_MST;
        for (int v = 0; v < n; v++) {
            if ( states[v] != IN_MST && M[u][v] < costs[v] ) {
                costs[v]   = M[u][v];
                parents[v] = u;
            }
        }
    }

    int total_cost = 0;
    for (int i = 0; i < n; i++) {
        if (parents[i] != -1)
            total_cost += M[i][ parents[i] ];
    }
    return total_cost;
}

int main() {
    int n;
    scanf("%d", &n);

    int cost;
    for (int u = 0; u < n; u++) {
        for (int v = 0; v < n; v++) {
            scanf("%d", &cost);
            M[u][v] = 0 <= cost ? cost : MAX_INT;
        }
    }

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

    return 0;
}

C++: #include <iostream>
using namespace std;

const long MAX = 100;
const long INFTY = (1<<21);
const long WHITE = 0;
const long GRAY = 1;
const long BLACK = 2;

long n, M[MAX][MAX];

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

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

	d[0] = 0;

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

	return sum;
}

int main(){
	cin >> n;

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

	cout << prim() << endl;
}

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

int n;
int MAXIMUM=100;
int M[100][100];
int INF = 123456789;
// W:未訪問
// G:訪問する候補
// B:訪問済み
int W=0,G=1,B=2;
int prim(){
  int u,minv;
  int d[MAXIMUM],p[MAXIMUM],color[MAXIMUM];
  // d[n]:Tに属する頂点と V - T に属する頂点をつなぐ辺の中で重みが最小の辺の重みを記録

  //初期化
  for(int i=0;i<n;i++){
    d[i]=INF;
    p[i]=-1;
    color[i]=W;
  }

  d[0]=0;

  while (1) {
    minv = INF;
    u = -1;
    //訪問済みでない頂点の中で最もコストが低い辺を探す
    for(int i=0;i<n;i++){
      if(minv>d[i] && color[i]!=B){
        u=i;
        minv=d[i];
      }
    }
    // 頂点が見つからなかったら終了
    if(u==-1) break;
    // 頂点を訪問済に
    color[u] = B;
    //訪問済みでないかつ辺がつながっている頂点の中でコストが最小の辺を選択
    for(int v=0;v<n;v++){
      if(color[v]!=B && M[u][v]!=INF){
        if(d[v]>M[u][v]){
          d[v] =M[u][v];
          p[v] =u;
          color[v]=G;
        }
      }
    }
  }

  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) ? INF :e;
      if(e==-1) M[i][j]=INF;
      else M[i][j]=e;
    }
  }

  int ans =prim();
  cout<<ans<<endl;
}

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