Go: package main

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

const (
	WHITE int = iota
	GRAY
	BLACK
)

type graph struct {
	G [][]int
	C []int
	D []int
	P []int
}

var INFTY = 1 << 32

func constructGraph() *graph {
	sc := bufio.NewScanner(os.Stdin)
	sc.Scan()
	n, _ := strconv.Atoi(sc.Text())

	g := make([][]int, n)
	c := make([]int, n)
	d := make([]int, n)
	p := make([]int, n)

	for i := 0; i < n; i++ {
		sc.Scan()
		fields := strings.Fields(sc.Text())
		g[i] = toIntSlice(fields)
	}

	return &graph{g, c, d, p}
}

func (g *graph) initMst() {
	for i := 0; i < len(g.G); i++ {
		g.C[i] = WHITE
		g.D[i] = INFTY
	}
	g.D[0] = 0
	g.P[0] = -1
}

func (g *graph) primMst() int {
	g.initMst()

	var u int
	for {
		mincost := INFTY
		for i := 0; i < len(g.G); i++ {
			if g.C[i] != BLACK && g.D[i] < mincost {
				mincost = g.D[i]
				u = i
			}
		}

		if mincost == INFTY {
			break
		}

		g.C[u] = BLACK

		for i := 0; i < len(g.G); i++ {
			if g.C[i] != BLACK && g.existEdge(u, i) {
				if g.G[u][i] < g.D[i] {
					g.D[i] = g.G[u][i]
					g.P[i] = u
				}
			}
		}
	}

	totalCost := 0
	for _, cost := range g.D {
		totalCost += cost
	}
	return totalCost
}

func (g *graph) existEdge(u, v int) bool {
	return g.G[u][v] != INFTY && g.G[v][u] != INFTY
}

func toIntSlice(s []string) []int {
	a := make([]int, len(s))
	for i, v := range s {
		num, _ := strconv.Atoi(v)
		if num == -1 {
			num = INFTY
		}
		a[i] = num
	}
	return a
}

func main() {
	g := constructGraph()
	fmt.Println(g.primMst())
}

C++: #include <iostream>
#include <cstdio>
#include <cstring>
#include <stack>
#include <vector>
using namespace std;
const int maxx = 1010;
const int INF = 1 << 30;
int n, m[maxx][maxx];
int u, num, v;
int flag[maxx];

int prim() {
	// d??°????????????V-T??? p??°????????°???MST???
	int d[maxx], p[maxx];
	for (int i = 0; i < n; i++) {
		d[i] = INF;
		p[i] = -1;
		flag[i] = 0;
	}

	d[0] = 0;
	while (1) {
		int minv = INF;
		u = -1;
		// ???????????????????°???????
		for (int i = 0; i < n; i++) {
			if (minv > d[i] && flag[i] == 0) {
				u = i;
				minv = d[i];
			}
		}

		if (u == -1)
			break;

		// ?????°???????????\MST
		flag[u] = 1;

		// ??´??°?????????????????????????????????????????´???????°???????????????????????????????
		for (int v = 0; v < n; v++) {
			if (flag[v] == 0 && m[u][v] != INF && d[v] > m[u][v]) {
				d[v] = m[u][v];
				p[v] = u;
			}
		}
	}


	int ans = 0;
	for (int i = 0; i < n; i++)
		if (p[i] != -1)
			ans += m[i][p[i]];

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

C++: #include <iostream>
#include <array>
#include <vector>
#include <algorithm>

int n;
static const int nmax = 2005;
static const int INFTY = (1 << 21);
static const int WHITE = 0; // Not visited
static const int GRAY = 1;  // Visited
static const int BLACK = 2; // In MST
std::array<std::array<int, nmax>, nmax> adjmat;
std::array<int, nmax> d, p;
std::array<int, nmax> color;

void prim()
{
  color.fill(WHITE);
  d.fill(INFTY);
  p.fill(-1);

  int v = 0;
  d[v] = 0;
  color[v] = GRAY;

  while (true)
    {
      auto mincost = INFTY;

      // Find minimum cost vertex from current MST
      for (int i=0; i<n; ++i)
        {
          if (color[i] != BLACK && d[i] < mincost)
            {
              mincost = d[i];
              v = i;
            }
        }

      // Break if no update
      if (mincost == INFTY) break;

      // Include v in MST
      color[v] = BLACK;

      // Update minimum cost outside current MST
      for (int i=0; i<n; ++i)
        {
          if (color[i] != BLACK && adjmat[v][i] != -1 && adjmat[v][i] < d[i])
            {
              d[i] = adjmat[v][i];
              color[i] = GRAY;
              p[i] = v;
            }
        }
    }
}

int main ()
{
  std::cin >> n;
  for (int i=0; i<n; ++i)
    {
      for (int j=0; j<n; ++j)
        std::cin >> adjmat[i][j];
    }

  prim();

  int ans = 0;
  //std::for_each(d.begin(), d.begin()+n, [&](auto x){ans+=x;});
  for (int i=0; i<n; ++i)
    {
      if (p[i] != -1)
        ans += adjmat[i][p[i]];
    }
  std::cout << ans << std::endl;
}

C++: //
//  main.cpp
//  minnimum_spanning_tree
//
//  Created by ???????????? on 2017/10/23.
//  Copyright ?? 2017 ????????????. All rights reserved.
//

#include <iostream>
#include <stdio.h>

#define n_max 100
#define WHITE 0
#define GRAY 1
#define BLACK 2
#define INFTY 3000

using namespace std;

int n, M[n_max][n_max], color[n_max], d[n_max], p[n_max];

void init(){
    int i, j;
    scanf("%d", &n);
    for(i = 0; i < n; i++){
        for(j = 0; j < n; j++){
            scanf("%d", &M[i][j]);
        }
        color[i] = WHITE;
        d[i] = INFTY;
    }
}

void prim(){
    //initializing
    d[0] = 0;
    p[0] = -1;
    
    int u;
    while(true){
        int mincost = INFTY;
        for(int 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(int v = 0; v < n; v++){
            if((color[v] != BLACK) && (M[u][v] != -1)){
                if(M[u][v] < d[v]){
                    d[v] = M[u][v];
                    p[v] = u;
                    color[v] = GRAY;
                }
            }
        }
    }
    int sum = 0;
    for(int i = 0; i < n; i++){
        sum += d[i];
    }
    cout<<sum<<endl;
}

int main(int argc, const char * argv[]) {
    init();
    prim();
//    std::cout << "Hello, World!\n";
    return 0;
}

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