Go: package main

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

const (
	white int = iota
	gray
	black
)
const INFTY int = 2001

var G [][]int
var color []int
var d []int
var p []int
var N int

func prim() int {
	u := 0
	d[u] = 0
	for {
		minCost := INFTY
		for 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 v:=0; v<N; v++ {
			if color[v] != black && G[u][v] != -1 {
				if G[u][v] < d[v] {
					d[v] = G[u][v]
					p[v] = u
					color[v] = gray
				}
			}
		}
	}
	// sum
	sum := 0
	for i:=0; i<N; i++ {
		if p[i] != -1 {
			sum += G[i][p[i]]
		}
	}
	return sum
}




func initVariable(N int) {
	G = make([][]int, N)
	for i:=0; i<N; i++ {
		G[i] = make([]int, N)
	}
	color = make([]int, N)
	d = make([]int, N)
	p = make([]int, N)
	for i:=0; i<N; i++ {
		d[i] = INFTY
		p[i] = -1
	}
}

func nextInt(scanner *bufio.Scanner) int {
	scanner.Scan()
	num, _ := strconv.Atoi(scanner.Text())
	return num
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	N = nextInt(scanner)
	initVariable(N)
	for i:=0; i<N; i++ {
		for j:=0; j<N; j++ {
			G[i][j] = nextInt(scanner)
		}
	}
	fmt.Printf("%d\n", prim())
}

C++: #include <stdio.h>

#define MAX_N (105)
#define INF (10000)
int G[MAX_N][MAX_N];
int N;

int dist[MAX_N];
bool arrived[MAX_N];

int prim()
{
    int ret = 0;
    for ( int i = 0; i < N; i++ ) {
	arrived[i] = false;
	dist[i] = INF;
    }

    dist[0] = 0;

    while ( true ) {
	int min_dist = INF;
	int u = 0;
	for ( int i = 0; i < N; i++ ) {
	    if ( !arrived[i] && dist[i] < min_dist ) {
		min_dist = dist[i];
		u = i;
	    }
	}
	
	if ( min_dist == INF ) break;

	ret += dist[u];
	arrived[u] = true;

	for ( int i = 0; i < N; i++ ) {
	    if ( !arrived[i] && (G[u][i] != -1) && (dist[i] > G[u][i]) ) {
		dist[i] = G[u][i];
	    }
	}
    }

    return ret;
}

int main()
{
    int ans = 0;
    scanf("%d", &N);

    for ( int i = 0; i < N; i++ ) {
	for ( int j = 0; j < N; j++ ) {
	    scanf("%d", &G[i][j]);
	}
    }

    ans = prim();

    printf("%d\n", ans);

    return 0;
}

C++: #include<iostream>
#include<cstdio>
#define N 101
using namespace std;

int A[N][N];
int n;
int father[N];

int getparent(int i){
    while(father[i]!=i){
        i=father[i];
    }
    return i;
}
void mergeGroup(int a,int b){
    int fa=getparent(a);
    int fb=getparent(b);
    father[fb]=fa;
}

int prim(){
    int ans=0;
    int c=0;
    int minval=4000;
    int ab,ae;
    while(c<n-1){
        int b,e;
        minval=4000;
        for(b=0;b<n;b++){
            for(e=b+1;e<n;e++){
                if(A[b][e]!=-1 && A[b][e]<minval &&getparent(b)!=getparent(e)){
                    minval=A[b][e];
                    ab=b;
                    ae=e;
                    //printf("b= %d  e= %d \n",b,e);
                }
            }
        }
        ans+=A[ab][ae];
        //printf("ab= %d  ae= %d  mincost=%d \n ",ab,ae,minval);
        c++;
        mergeGroup(ab,ae);
    }
    return ans;
}

int main(){

    int k,i,j;
    cin>>n;
    for(int i=0;i<n;i++){
        father[i]=i;
        for(int j=0;j<n;j++){
            cin>>A[i][j];
        }
    }
    cout<<prim()<<endl;
    return 0;
}

C++: #include<iostream>

using namespace std;
#define MAX 100
#define INF 10000
int M[MAX][MAX];
int n;
int d[MAX];
int color[MAX];
int p[MAX];



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

  for (int i = 0; i < n; i++) {
    d[i] = INF;
    color[i] = 0;
  }

  int w = 0;
  d[0] = 0;
  while(true) {

    int com = INF;
    int u;
    for (int i = 0; i < n; i++) {
      if (color[i] != 2 && com > d[i] && d[i] != -1){
        com = d[i];
        u = i;
      }
    }

    if (com == INF) {
      break;
    }

    w += com;
    color[u] = 2;

    for (int j = 0; j < n; j++) {
      if (M[u][j] != -1 && M[u][j] < d[j] && color[j] != 2) {
        d[j] = M[u][j];
        p[j] = u;
      }
    }
  }

  cout << w << endl;
  return 0;
}

C++: #include <cstdio>
#include <iostream>
#include <queue>
#include <utility>

using namespace std;

const int NIL = 2001;
const int MAX = 100;
int G[MAX][MAX];
int color[MAX];
int W[MAX];
int n;

const int WHITE = 0;
const int GRAY = 1;
const int BLACK = 2;

void MST() {
	priority_queue<pair<int, int>> PQ;
	PQ.push(make_pair(0, 0));
	W[0] = 0;
	color[0] = BLACK;
	while (!PQ.empty()) {
		int u = PQ.top().second;
		PQ.pop();
		color[u] = BLACK;
		for (int i = 0; i < n; ++i) {
			if (G[u][i] != -1 && G[u][i] < W[i] && color[i] != BLACK) {
				PQ.push(make_pair(G[u][i] * -1, i));
				W[i] = G[u][i];
//				cout << u << ":" << i << ":" << G[u][i] << endl;
			}
		}
	}
}

int main() {

	scanf("%d", &n);
	for (int i = 0; i < n; ++i) {
		W[i] = NIL;
		color[i] = WHITE;
	}

	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < n; ++j) {
			int a;
			scanf("%d", &a);
			G[i][j] = a;
		}
	}

	MST();

	cout << endl;

	int sum = 0;
	for (int i = 0; i < n; ++i) {
//		cout << i << ":" << W[i] << endl;
		sum += W[i];
	}
	cout << sum << endl;

	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define WHITE 0
#define GRAY 1
#define BLACK 2

int M[101][101];
int n;
vector<int> q;

int check(int point){
    if(find(q.begin(), q.end(), point) != q.end()){
        //printf("%d in q\n",point);
        return 1;
    }
    else{
        //printf("%d not in q\n",point);
        return 0;
    }
}

int prim(int point){
    q.push_back(point);
    int dis = INT_MAX;
    int next_point;
    int sum = 0;
    while(q.size()!=n){
        dis = INT_MAX;
        for(int i=0;i<q.size();++i){
            for(int j=0;j<n;++j){
                if(M[q[i]][j] != INT_MAX && M[q[i]][j] < dis && check(j)==0){
                    dis = M[q[i]][j];
                    next_point = j;
                }
            }
        }
        q.push_back(next_point);
        //printf("push %d\n",next_point);
        sum += dis;
    }
    return sum;
}

int main(){
    int u, z, y;
    int value;
    scanf("%d",&n);
    for(int i=0;i<n;++i){
        for(int j=0;j<n;++j){
            cin>>value;
            M[i][j] = (value==-1)?INT_MAX:value;
            M[j][i] = (value==-1)?INT_MAX:value;
        }
    }
    int sum = prim(0);

    printf("%d\n",sum);
    return 0;
}

C++: #include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
using namespace std;

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef stack<int> SI;
typedef queue<int> QI;
typedef pair<int,int> PII;
typedef long long LL;

#define d(x)  cout<<#x<<" = "<<(x)<<endl;
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define REACH(i,c) for(typeof((c).rbegin()) i=(c).rbegin(); i!=(c).rend(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c,n) sort(c,c+n)
#define VSORT(c) sort((c).begin(),(c).end())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define DFOR(i,b,a) for(int i=(b)-1;i>=(a);--i)
#define DREP(i,n) DFOR(i,n,0)
#define INF 2000000000

const double EPS = 1e-10;
const double PI  = acos(-1.0);
////////////////////////////////////////////////
int a[100][100],n,s,judge,cost;
int t[100];
int main()
{
	cin>>n;
	REP(i,n)
	{
		REP(j,n)
		{
			cin>>a[i][j];
			if(a[i][j]==-1)a[i][j]=INF;
		}
	}
	t[0]=1;
	for(;;)
	{
		int mincost=INF;
		REP(u,n)
		{
			REP(v,n)
			{
				if(t[u]==1&&t[v]==0)
				{
					if(mincost>a[u][v])
					{
						mincost=a[u][v];
						s=v;
					}
				}
			}
		}
		cost+=mincost;
		t[s]=1;
		judge=1;
		REP(i,n)judge&=t[i];
		if(judge==1)break;
	}
	cout<<cost<<endl;
	return 0;
}