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 <list>
#include <utility>
using namespace std;
#define MAXN 100
#define MAXW 2001

int main(){
    int weight[MAXN][MAXN];
    int n; cin >> n;
    for(int i=0; i<n; i++){
        for(int j=0; j<n; j++){
            cin >> weight[i][j];
        }
    }
    
    list<int> MST;
    MST.push_back(0);
    list<int> nonMST;
    for(int i=1; i<n; i++) nonMST.push_back(i);
    
    int weightSum = 0;
    while(!nonMST.empty()){
        int tmpv;
        int tmpWeight = MAXW;
        for(list<int>::iterator it=MST.begin(); it!=MST.end(); it++){
            for(list<int>::iterator it2=nonMST.begin(); it2!=nonMST.end(); it2++){
                if (weight[*it][*it2] < tmpWeight && weight[*it][*it2] != -1){
                    //cout << "tmpWeight update with " << *it2 << endl;
                    tmpWeight = weight[*it][*it2];
                    tmpv = *it2;
                }
            }
        }
        //cout << "MST pushback :" << tmpv << endl; 
        MST.push_back(tmpv);
        nonMST.remove(tmpv);
        weightSum += tmpWeight;
        /*for(list<int>::iterator it=MST.begin(); it!=MST.end(); it++){
            cout << *it << " ";
        }
        cout << endl;
        for(list<int>::iterator it=nonMST.begin(); it!=nonMST.end(); it++){
            cout << *it << " ";
        }
        cout << endl;*/
    }
    
    cout << weightSum << endl;
}

C++: #include<iostream>
using namespace std;
static const int MAX = 100;
static const int INF = (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] = INF;
		p[i] = -1;
		color[i] = WHITE;
	}
	
	d[0] = 0;
	
	while(1){
		minv = INF;
		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] != INF){
				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)?INF:e;
		}
	}
	
	cout << prim() <<endl;
	
	
	return 0;
}

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

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

int n;
int g[101][101];

int d[101];
char c[101];
int p[101];

void prim() {
    for (int i = 0; i < n; ++i) {
        d[i] = INT_MAX;
        c[i] = 'w';
    }
    c[0] = 'b';
    d[0] = 0;
    while (true) {
        int u, v;
        int mine = INT_MAX;
        for (int j = 0; j < n; ++j) {
            if (c[j] == 'b') {
                u = j;
                for (int k = 0; k < n; ++k) {
                    if(g[u][k] > -1 && g[u][k] <= d[k] && c[k] != 'b' && g[u][k] <= mine) {
                        d[k] = g[u][k];
                        mine = d[k];
                        v = k;
                    }
                }
            }
        }
        p[v] = u;
        c[v] = 'b';
        if(mine == INT_MAX) break;
    }
}

int main() {
    cin >> n;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            int w;
            cin >> w;
            g[i][j] = w;
        }
    }
    prim();
    int sum;
    for (int k = 0; k < n; ++k) {
        sum += d[k];
    }
    cout << sum << endl;


    return 0;
}

C++: #include <stdio.h>
#include <vector>
#include <queue>
using namespace std;
struct side{
    int n;
    int m;
    int l;
};
side sides[100000];
void mergesort(int l,int r){
    if(l==r)return;
    side temp;
    if(l==r-1){
        if(sides[l].l>sides[r].l){
            temp=sides[l];
            sides[l]=sides[r];
            sides[r]=temp;
        }
        return;
    }
    int mid=(l+r)/2,now=0;
    side b[r-l+1];
    mergesort(l,mid-1);
    mergesort(mid,r);
    for(int i=0;i<=r-l;i++){
        if(sides[i+l-now].l<sides[mid+now].l){
            b[i]=sides[i+l-now];
            if(i+l-now==mid-1){
                for(i++;i<=r-l;i++){
                    b[i]=sides[mid+now];
                    now++;
                }
            }
        }
        else{
            b[i]=sides[mid+now];
            now++;
            if(mid+now>r){
                for(i++;i<=r-l;i++)
                    b[i]=sides[i+l-now];
            }
        }
    }
    for(int i=0;i<=r-l;i++)
        sides[i+l]=b[i];
}
int main(){
    int n,a,ans=0,c=1,now=0;
    bool tree[10000];
    scanf("%d",&n);
    tree[0]=true;
    for(int i=1;i<n;i++){
        tree[i]=false;
    }
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            scanf("%d",&sides[now].l);
            sides[now].n=i;
            sides[now].m=j;
            if(sides[now].l==-1)now--;
            now++;
        }
    }
    mergesort(0,now-1);
    while(c<n){
        a=0;
        while(tree[sides[a].n]==tree[sides[a].m])a++;
        ans+=sides[a].l;
        tree[sides[a].n]=tree[sides[a].m]=true;
        c++;
    }
    printf("%d\n",ans);
}