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 <cstdio>
#pragma warning(disable:4996)
#define MAX 101
#define INF 100000
#define FLAG 1
#define DEFLAG 0

int main() {
	int i, j, n, min, a[MAX][MAX], k, x[MAX], minNode, sum = 0, g[MAX];

	scanf("%d", &n);
	for (i = 0; i<n; i++) {
		for (j = 0; j<n; j++) {
			scanf("%d", &a[i][j]);
		}
	}
	k = 1;
	x[0] = 0;
	g[0] = FLAG;
	for (i = 1; i<n; i++) {
		g[i] = DEFLAG;
	}
	while (1) {
		min = INF;
		for (i = 0; i<k; i++) {
			for (j = 0; j<n; j++) {
				if (min > a[x[i]][j] && a[x[i]][j] != -1 && g[j] != FLAG) {
					min = a[x[i]][j];
					minNode = j;
				}
			}
		}
		if (k != n) {
			x[k] = minNode;
			g[minNode] = FLAG;
			k++;
			sum += min;
		}
		else { break; }

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

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

#define rep(i, n) for (int i=0; i<(n); ++i)
using ll = long long;
using P = pair<int, int>;

const int INF = 1 << 21;
const int MAX = 103;
const int WHITE = 0;
const int GRAY = 1;
const int BLACK = 2;
int n, G[MAX][MAX];

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

  fill((int*)d, (int*)(d+MAX), INF);
  memset(p, -1, sizeof(p));
  memset(color, WHITE, sizeof(color));

  d[0] = 0;
  while (true) {
    minv = INF;
    u = -1;
    rep(i,n) {
      if (minv > d[i] && color[i] != BLACK) {
	u = i;
	minv = d[i];
      }
    }
    if (u == -1) break;
    color[u] = BLACK;
    rep(v,n) {
      if (color[v] != BLACK && G[u][v] != INF) {
	if (d[v] > G[u][v]) {
	  d[v] = G[u][v];
	  p[v] = u;
	  color[v] = GRAY;
	}
      }
    }
  }
  
  int sum = 0;
  rep(i,n) {
    if (p[i] != -1) sum += G[i][p[i]];
  }
  return sum;
}

int main() {
  cin >> n;
  rep(i,n) rep(j,n) {
    int e;
    cin >> e;
    G[i][j] = (e == -1) ? INF : e;
  }

  cout << prim() << endl;
  return 0;
}

C++: #include <cstdio>
#define INF 1<<30

int n, M[100][100];

int prim() {
    int e, minv, minx, s;
    int p[n], d[n], v[n];

    for (e = 0; e < n; e++) {
        p[e] = -1;
        d[e] = INF;
        v[e] = 0;
    }

    d[0] = 0;

    while (1) {
        minv = INF;
        minx = -1;
        for (e = 0; e < n; e++) {
            if (minv > d[e] && v[e] != 1) {
                minx = e;
                minv = d[e];
            }
        }

        if (minx == -1) break;
        v[minx] = 1;
        
        for (e = 0; e < n; e++) {
            if (v[e] != 1 && M[minx][e] != INF) {
                if (d[e] > M[minx][e]) {
                    d[e] = M[minx][e];
                    p[e] = minx;
                }
            }
        }
    }

    s = 0;
    for (e = 0; e < n; e++) {
        if (p[e] != -1) s += M[e][p[e]];
    }

    return s;
}

int main(void) {
    int i, j, v;
    scanf("%d", &n);

    for (i = 0; i < n; i++) {
        for (j = 0; j < n; j++) {
            scanf("%d", &v);
            M[i][j] = (v == -1) ? INF : v;
        }
    }

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

    return 0;
}