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++: //
// Created by tattsun on 2017/02/16.
//

#include <iostream>

#define MAX_N 101
#define INFTY 20000
#define NIL -1

enum Color {
    White,
    Glay,
    Black,
};

using namespace std;

int n;
int w[MAX_N][MAX_N];
int d[MAX_N], p[MAX_N];
Color color[MAX_N];

int min(int x, int y) {
    if(x < y) return x;
    return y;
}

int solve() {
    d[0] = 0;

    while(true) {
        int u = NIL, c = INFTY;

        for(int i=0; i<n; i++) {
            if(d[i] < c && color[i] != Black) {
                u = i;
                c = d[i];
            }
        }

        if(u == NIL)
            break;

        color[u] = Black;

        for(int i=0; i<n; i++) {
            if(color[i] != Black && w[u][i] != INFTY) {
                if(d[i] > w[u][i]) {
                    d[i] = w[u][i];
                    p[i] = u;
                    color[i] = Glay;
                }
            }
        }
    }


    int tc = 0;
    for(int i=0; i<n; i++) {
        if(color[i] == Black) {
            tc += w[i][p[i]];
        }
    }
    return tc;
}

int main() {
    for (int i = 0; i < MAX_N; i++) {
        d[i] = INFTY;
        p[i] = INFTY;
        color[i] = White;
        for (int j = 0; j < MAX_N; j++)
            w[i][j] = INFTY;
    }

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

    cout << solve() << endl;
}

C++: #include <stdio.h>
#define rep(i, n) for(int i = 0; i < n; ++i)

int main(void) {
  int n, i, j, ans = 0;
  scanf("%d", &n);
  int a[n][n];
  char f[n];
  rep(i, n) rep(j, n) scanf("%d", &a[i][j]);
  rep(i, n) f[i] = 0;
  f[0] = 1;
  rep(i, n - 1) {
    int min = 10000, minx, miny;
    rep(j, n) {
      rep(k, n) {
        if(f[j] && !f[k] && a[j][k] >= 0 && a[j][k] < min) min = a[j][k], minx = j, miny = k;
      }
    }
    ans += min;
    f[miny] = 1;
  }
  printf("%d\n", ans);
  return 0;
}

C++: #include<iostream>
using namespace std;
static const int INF = 0x7fffffff;
static const int MAXN = 101;
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;
int n, M[MAXN][MAXN];

int prim()
{
	int node, minn;  //记录当前状态权值最小的边和对应的节点
	int d[MAXN], p[MAXN], color[MAXN]; //最小边权，父节点，访问状态
	/*初始化*/
	for (int i = 0; i < n; i++)
	{
		d[i] = INF;
		p[i] = -1;
		color[i] = WHITE; 
	}
	d[0] = 0;  //初始节点加入MST
	while (true)
	{
		node = -1;
		minn = INF;
		for (int i = 0; i < n; i++)
		{
			if (d[i] < minn && color[i] != BLACK)
			{
				minn = d[i];
				node = i;
			}
		}
		if (node == -1)	break; //图遍历完毕，没有可以新加入MST的节点
		color[node] = BLACK;   //把新节点加入MST

		/*更新状态*/
		for (int i = 0; i < n; i++) 
		{
			if (color[i] != BLACK && M[node][i] < INF)
			{
				if (d[i] > M[node][i])
				{
					d[i] = M[node][i];
					p[i] = node;
				}
			}
		}
	}
	int sum = 0;
	for (int i = 0; i < n; i++)
	{
		if (p[i] != -1)	sum += M[i][p[i]];  //求MST每条边的权和
	}
	return sum;
}

int main()
{
	int tmp;
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < n; j++)
		{
			cin >> tmp;
			M[i][j] = (tmp == -1 ) ? INF : tmp;
		}
	}
	cout << prim() <<endl;
    return 0;
}

C++: #include<stdio.h>
using namespace std;
const int sss=1<<21;
int n;
int a[1000][1000];
int d[1000],p[10000],color[1000];
int prim(){
	int u,minv;
	for(int i=0;i<n;i++){
		d[i]=sss;
		p[i]=-1;
	}
	d[0]=0;
	while(1){
		minv=sss;
		u=-1;
		for(int i=0;i<n;i++){
			if(minv>d[i]&&color[i]!=1){
				u=i;
				minv=d[i];
			}
		}
		if(u==-1) break;
		color[u]=1;
		for(int v=0;v<n;v++){
			if(color[v]!=1&&a[u][v]!=sss){
				if(d[v]>a[u][v]){
					d[v]=a[u][v];
					p[v]=u;
				}
			}
		}
	}
	int sum=0;
	for(int i=0;i<n;i++){
		if(p[i]!=-1) {
			sum+=a[p[i]][i];
		}
	}
	return sum;
}
int main(){
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			int e;scanf("%d",&e);
			a[i][j]=e==-1?sss:e;
		}
	}
	printf("%d\n",prim());
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool f[100];
int n;
int tr[100];

bool ch(){
  for(int i=0;i<n;i++)
    if(f[i]==0) return true;
  return false;
}

int trmin(){
  int res=10000,resi;;
  for(int i=0;i<n;i++)
    if(f[i]==0 && tr[i]<res) res = tr[i],resi=i;
  return resi;
}


int main (){

  int mp[100][100];

  cin >> n;
  for(int i=0;i<n;i++)
    for(int j=0;j<n;j++)
      cin >> mp[i][j];

  for(int i=0;i<n;i++) f[i]=0;
  for(int i=0;i<n;i++) tr[i]=10000;
  tr[0] = 0;
  while(ch()){
    int r=trmin();
    f[r]=1;
    for(int i=0;i<n;i++) 
      if(mp[r][i]!=-1)
	tr[i] = min(tr[i],mp[r][i]);
  }
  int trsum=0;
  for(int i=0;i<n;i++) trsum+=tr[i];
  cout<<trsum<<endl;

  return 0;
}

C++: #include<bits/stdc++.h>

using namespace std;
#define fordebug int hoge;cin>>hoge;
#define DEKAI 1000000007
#define INF (1<<28)
#define lp(i,n) for(int i=0;i<n;i++)
#define lps(i,j,n) for(int i=j;i<n;i++)
#define floot10 cout<<fixed<<setprecision(10);
#define int long long
int n,m[1000][1000],col[1000],d[1000],cent[1000];

void prim(){
	lp(i,n){
		d[i]=INF;
	}
	d[0]=0;
	cent[0]=-1;
	while(1){
		int mini=INF,hoge;
		lp(i,n){
			if(col[i]!=2&&d[i]<mini){
				mini=d[i];
				hoge=i;
			}
		}
		if(mini==INF) return;
		col[hoge]=2;
		lp(i,n){
			if(col[i]!=2){
				if(m[hoge][i]==INF) continue;
				if(m[hoge][i]<d[i]){
					d[i]=m[hoge][i];
					cent[i]=hoge;
					col[i]=1;
				}
			}
		}
	}
}

signed main(){
	cin>>n;
	lp(i,n){
		lp(j,n){
			cin>>m[i][j];
			if(m[i][j]==-1) m[i][j]=INF;
		}
	}
	prim();
	int ans=0;
	lp(i,n){
		if(cent[i]==-1) continue;
		ans+=m[i][cent[i]];
	}
	cout<<ans<<endl;
    return 0;
}