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<stdio.h>

#define MAX 1000
#define INFINITY 10000

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

enum {
    WHITE,
    GRAY,
    BLACK
};

int prim() {
    for (int i = 0; i < n; ++i) {
        color[i] = WHITE;
        d[i] = INFINITY;
        p[i] = -1;
    }
    d[0] = 0;
    p[0] = -1;

    while (true) {
        int min = INFINITY;
        int target = -1;
        for (int i = 0; i < n; ++i) {
            if (d[i] < min && color[i] != BLACK) {
                min = d[i];
                target = i;
            }
        }
        if (target == -1) break;

        color[target] = BLACK;

        for (int v = 0; v < n; ++v) {
            if (color[v] != BLACK && M[target][v] > 0) {
                if(M[target][v] < d[v]) {
                    d[v] = M[target][v];
                    p[v] = target;
                    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() {
    scanf("%d", &n);
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            scanf("%d", &M[i][j]);
        }
    }
    printf("%d\n", prim());
    return 0;
}

C++: #include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define N 110
#define INF 0x3f3f3f3f
int m[N][N];
int used[N];
int d[N];
int n;
int sum = 0;
void prim(){
    used[1] = 1;
    d[1] = 0;
    for(int k = 1; k < n; k++){
        int min = INF, u;
        for(int i = 1; i <= n; i++){
            if(!used[i] && d[i] < min){
                min = d[i];
                u = i;
            }
        }

        used[u] = 1;
        sum += d[u];
        for(int v = 1; v <= n; v++){
            if(!used[v] && m[u][v] < d[v]){
                d[v] = m[u][v];
            }
        }
    }
    printf("%d\n", sum);
}
int main(){

    scanf("%d", &n);
    for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) m[i][j] = INF;
    for(int i = 1; i <= n; i++){
        d[i] = INF;
        used[i] = 0;
    }

    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= n; j++){
            int p;
            scanf("%d", &p);
            if(p != -1) m[i][j] = p;
        }
    }
    for(int i = 1; i <= n; i++) d[i] = m[1][i];
    prim();
    return 0;
}

C++: #include <iostream>
#include <string>
#include <stack>
#include <queue>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <algorithm>

#include <cstdio>

using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repc(i, s, n) for (int i = (s); i <= (n); i++)
#define rrep(i, n) for (int i = (n) - 1; i >= 0; i--)
#define rrepc(i, s, n) for (int i = (s); i >= (n); i--)
#define print_array(ary, size) cout << ary[0]; rep1(i, 1, size - 1) cout << ' ' << ary[i]; cout << endl;
typedef long long ll;
typedef unsigned long long ull;

int M[100][100];
int done[100] = { 0 };

int main()
{
	int n;
	int ans = 0;
	int mincost;
	int index = 0;

	cin >> n;
	rep(i, n) {
		rep(j, n) {
			cin >> M[i][j];
		}
	}

	done[0] = 1;

	while (true) {
		mincost = -1;

		rep(i, n) {
			rep(j, n) {
				if ((done[i] == 1 && done[j] == 0)
					&& (M[i][j] != -1)
					&& (mincost == -1 || mincost > M[i][j])) {
					mincost = M[i][j];
					index = j;
				}
			}
		}

		if (mincost == -1) break;
		else {
			done[index] = 1;
			ans += mincost;
		}
	}

	cout << ans << endl;

	return 0;
}

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

class union_find  // union find.
{
    int* U;
public:
    union_find(int n){ U = new int [n]; for(int i = 0; i < n; i++) U[i] = i; }
    int root(int a){ if(U[a] == a){ return a; } U[a] = root(U[a]); return U[a]; }
    bool same(int a, int b){ return root(a) == root(b); }
    void join(int a, int b){ int x = root(a), y = root(b); U[x] = y; }
    ~union_find(){ delete [] U; }
};

struct edge{
	int v1, v2, w;
	bool operator < (const edge &e) const{ return w > e.w; }  // const?????????????????????
};

edge init(int ver_1, int ver_2, int weight)
{
	edge e;
	e.v1 = ver_1, e.v2 = ver_2, e.w = weight;
	return e;
}

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

	priority_queue<edge> ES;
	queue<edge> ST;
	union_find VS(n);

	for(i = 0; i < n; i++){
		for(j = 0; j < n; j++){
			scanf("%d", &w);
			if(i >= j || w < 0) continue;
			ES.push(init(i, j, w));
		}
	}
	int size = 0;
	edge e;
	while(!ES.empty() && size < n - 1){
		e = ES.top();
		ES.pop();
		if(!VS.same(e.v1, e.v2)){
			VS.join(e.v1, e.v2);
			ST.push(e);
			size++;
		}
	};
	int sum = 0;
	while(!ST.empty()){ e = ST.front(); sum += e.w; ST.pop(); };
	printf("%d\n", sum);

	return 0;
}

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

int main(){
  int n;
  int a[100][100],b[100]={},c[100]={};
  cin>>n;
  for(int i=0;i<n;i++)
   for(int j=0;j<n;j++)cin>>a[i][j];
  c[0]=1;

  while(1){
   int k=0.min=10000,min1=0;
   if(k==n)break;
   for(int i=0;i<n;i++)
    for(int j=0;j<n;j++)
     if(a[b[i]][j]<min && a[b[i]][j]!=-1 &&c[j]!=1){
      min=a[b[i]][j];
      b[k]=j;
    }
   c[b[k]]=1
   ans+=min;
   k++;
  }
 cout<<ans<<endl;
 return 0;
}