Go: package main

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

const (
	MAX   = 100
	INFTY = 1 << 21
	NIL   = -1
)

const (
	WHITE = iota
	GRAY
	BLACK
)

type Graph [MAX][MAX]int

func (g *Graph) prim(n int) int {
	var (
		u, minv     = 0, 0
		d, p, color [MAX]int
	)

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

	for {
		minv = INFTY
		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 && g[u][v] != INFTY {
				if d[v] > g[u][v] {
					d[v] = g[u][v]
					p[v] = u
					color[v] = GRAY
				}
			}
		}
	}

	sum := 0
	for i := 0; i < n; i++ {
		if p[i] != -1 {
			sum += g[i][p[i]]
		}
	}

	return sum
}

func answer(reader io.Reader) string {
	sc := bufio.NewScanner(reader)
	sc.Split(bufio.ScanWords)

	sc.Scan()
	n, _ := strconv.Atoi(sc.Text())

	var g Graph
	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			sc.Scan()
			e, _ := strconv.Atoi(sc.Text())
			if e == -1 {
				g[i][j] = INFTY
				continue
			}
			g[i][j] = e
		}
	}

	return fmt.Sprint(g.prim(n))
}

func main() {
	fmt.Println(answer(os.Stdin))
}

C++: #include<iostream>
using namespace std;
static const int MAX=100;
static const int INF=(1<<21);
static const int WH=0;
static const int GRY=1;
static const int BL=2;

int n, MA[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]=WH;
  }

  d[0]=0;

  while(1)
  {
    minV=INF;
    u = -1;
    for(int i=0; i<n; i++)
    {
      if(minV > d[i] && color[i] != BL)
      {
        u=i;
        minV=d[i];
      }
    }
    if(u == -1) break;
    color[u]=BL;
    for(int v=0; v<n; v++)
    {
      if(color[v] != BL && MA[u][v] != INF)
      {
        if(d[v] > MA[u][v])
        {
          d[v]=MA[u][v];
          p[v]=u;
          color[v]=GRY;
        }
      }
    }
  }
  int sum=0;
  for(int i=0; i<n; i++)
  {
    if(p[i] != -1) sum += MA[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;
      MA[i][j] = (e == -1) ? INF : e;
    }
  }

  cout << prim() << endl;

  return 0;
}

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

#define WHITE 0
#define GRAY 1
#define BLACK 2
#define INF 114514

int x[10000][10000];
int color[10000];
int MIN[10000];
int sum;
int sum_min = INF;

int main() {
	int n, a, b, c;
	cin >> n;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			cin >> x[i][j];
		}
	}
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			MIN[j] = INF;
			color[j] = WHITE;
		}
		b = INF;
		a = n;
		color[i] = GRAY;
		MIN[i] = 0;
		while (a != 0) {
			b = INF;
			for (int j = 0; j < n; j++) {
				if (color[j] == GRAY) {
					if (b > MIN[j]) {
						b = MIN[j];
						c = j;
					}
				}
			}
			color[c] = BLACK;
			a -= 1;
			for (int k = 0; k < n; k++) {
				if (color[k] != BLACK && x[c][k] != -1) {
					color[k] = GRAY;
					MIN[k] = min(x[c][k], MIN[k]);
				}
			}
		}
		sum = 0;
		for (int j = 0; j < n; j++) {
			sum += MIN[j];
		}
		sum_min = min(sum_min, sum);
	}
	cout << sum_min << endl;
	return 0;
}

C++: #include <stdio.h>
#define INFTY 1000000
#define M -1
#define WHITE 0
#define GRAY  1
#define BLACK 2
#define N 100
int prim(void);
int n,A[N][N];

int main()
{
  int i,j,k;

  scanf("%d",&n);
  for(i=0;i<n;i++)
    {
      for(j=0;j<n;j++)
        {
          scanf("%d",&k);
          if(k==M) A[i][j]=INFTY;
        }
    }

  printf("%d\n",prim());
  return 0;
}

int prim()
{
  int d[N],p[N],color[N];
  int i,v,u,mincost,sum=0;

  for(i=0;i<n;i++)
    {
      d[i]=INFTY;
      p[i]=M;
      color[i]=WHITE;
    }
  d[0]=0;
while(1)
    {
      mincost=INFTY;
      u=M;
      for(i=0;i<n;i++)
        {
          if(color[i]!=BLACK&&d[i]<mincost)
            {
              u=i;
              mincost=d[i];
            }
        }
      if(u==M) break;
      color[u]=BLACK;
      for(v=0;v<n;v++)
        {
          if(color[v]!=BLACK&&A[u][v]!=INFTY)
            {
              if(d[v]>A[u][v])
                {
                  d[v]=A[u][v];
                  p[v]=u;
                  color[v]=GRAY;
                }
            }
        }
    }
  for(i=0;i<n;i++)
    {
      if(p[i]!=M) sum+=A[i][p[i]];
    }
  return sum;

C++: #include <iostream>
#define N 100
#define INFINITY 200000000
using namespace std;
void prim();
int M[N][N],d[N],p[N],u,n,mincost,i,j;
string color[N];
int main(){
  cin >> n;
  for(i=0;i<n;i++){
    for(j=0;j<n;j++){
      cin >> M[i][j];
    }
  }
  prim();
  return 0;
}
void prim(){
  for(i=0;i<n;i++) color[i]="WHITE",d[i]=INFINITY;
  d[0]=0;
  p[0]=-1;
  while(1){
    mincost=INFINITY;
    for(i=0;i<n;i++){
      if(color[i]!="BLACK"&&d[i]<mincost){
	mincost=d[i];
	u=i;
      }
    }
    if(mincost==INFINITY) break;
    color[u]="BLACK";
    for(i=0;i<n;i++){
      if(color[i]!="BLACK"&&M[u][i]!=-1){
	if(M[u][i]<d[i]){
	  d[i]=M[u][i];
	  p[i]=u;
	  color[i]="GLAY";
	}
      }
    }
  }
  int sum=0;
  for(i=0;i<n;i++) sum+=d[i];
  cout << sum << 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;
}