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>

using namespace std;

#define FOR(i,s,e) for((i)=(s);(i)<(int)(e);(i)++)
#define REP(i,e) FOR(i,0,e)

#define all(o) (o).begin(), (o).end()
#define psb(x) push_back(x)
#define mp(x,y) make_pair((x),(y))

typedef long long ll;
typedef pair<int, int> PII;

const double EPS = 1e-10;

typedef struct edge_def {
  int from;
  int to;
  int cost;

  edge_def() {;}
  edge_def(int _from, int _to, int _cost) {
    from = _from;
    to   = _to;
    cost = _cost;
  }
} edge;

const int INF = 1000 * 1000 * 1000;
const int N = 100;
int n;
int ed[N];  //ed[i]:??????i???????°???¨?????¨?????¨?????????????????????????????????i??¨??¨?????¨??????????????????????????????
            //????°???¨?????¨????§?????????????????????????????????£????????????
int u[N];  //??¢???????°???¨?????¨???????????????????????????
vector<edge> es[N];

int prim(int s) {
  int i, j, res = 0;
  priority_queue<PII, vector<PII>, greater<PII > > q;
  memset(u, 0, sizeof(u));
  for (i=0; i<n; i++) ed[i] = INF;
  ed[s] = 0;
  q.push(PII(0, s));

  while (q.size()) {
    PII p = q.top(); q.pop();

    u[p.second] = 1;
    res += p.first;

    int m = INF;
    edge me;
    for (i=0; i<n; i++)
      for (j=0; j<(int)es[i].size(); j++) {
        edge e = es[i][j];
        if (u[e.from] && !u[e.to] && e.cost < m) {
          me = e;
          m = me.cost;
        }
      }
    if (m != INF) {
      q.push(PII(me.cost, me.to)); 
      ed[me.to] = me.cost;
    }
  }

  return res;
}


int main() {
  int i, j;
  scanf("%d ", &n);
  int c;
  for (i=0; i<n; i++)
    for (j=0; j<n; j++) {
      scanf("%d ", &c);
      if (c!=-1) es[i].psb(edge(i,j,c));
    }
  printf("%d\n", prim(0));
  return 0;
}

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

const int MAX=100;
int INFTY=1<<30;
int WHITE=0;
int GRAY=1;
int BLACK=2;

int n,m[MAX][MAX];

void 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]){
				p[v]=u;
				d[v]=m[u][v];
				color[v]=GRAY;
			}
	}
	int sum=0;
	for(int i=0;i<n;i++)
	if(p[i]!=-i) sum+=m[i][p[i]];
	cout<<sum<<endl;
}

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;
		}
	}
	prim();
	return 0;
}