Go: package main

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

const R = 100000000

var sc = bufio.NewScanner(os.Stdin)

var n int
var A [101][101]int

func nextInt() int {
	sc.Scan()
	i, _ := strconv.Atoi(sc.Text())
	return i
}
func prim() int {
	var u, Minimum, sum int
	var d, p, color [101]int

	for i := 0; i < n; i++ {
		d[i] = R
		p[i] = -1
		color[i] = 0
	}
	d[0] = 0

	for {
		Minimum = R
		u = -1
		for i := 0; i < n; i++ {
			if Minimum > d[i] && color[i] != 2 {
				u = i
				Minimum = d[i]
			}
		}
		if u == -1 {
			break
		}
		color[u] = 2
		for j := 0; j < n; j++ {
			if color[j] != 2 && A[u][j] != R {
				if d[j] > A[u][j] {
					d[j] = A[u][j]
					p[j] = u
					color[j] = 1
				}
			}
		}
	}

	for i := 0; i < n; i++ {
		if p[i] != -1 {
			sum += A[i][p[i]]
		}
	}
	return sum
}
func main() {
	sc.Split(bufio.ScanWords)
	n = nextInt()

	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			e := nextInt()
			if e == -1 {
				A[i][j] = R
			} else {
				A[i][j] = e
			}
		}
	}
	fmt.Println(prim())
}

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

using namespace std;

#define N 101

int main(){

  int n;

  int g[N][N];
  int min=3000;
  int weight[N];
  int sum=0;

  //load n
  cin >>n;

  //load data
  for(int i=1;i<=n;i++){
    weight[i]=0;
    for(int j=1;j<=n;j++){
      scanf("%d",&g[i][j]);
    }
  }

  for(int i=1;i<=n;i++){
    for(int j=1;j<=n;j++){
      if(g[i][j]!= -1 && g[i][j]<min) {
	min=g[i][j];
      }
	weight[i]=min;
    }
  }

  for(int i=1;i<=n;i++){
    if(weight[i]!=3000) sum+=weight[i];
  }
  //print
  cout <<sum<<endl;

  return 0;

}

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

#define N 120
int n;

int g[N][N];
int mini;
int weight[N];
bool us[N];

int prim(){
  int v;
  int ans=0;
  weight[1]=0;
  
  while(true){
    v=-1;
   
    for(int i=1;i<=n;i++){
      if(us[i]!=true && (v==-1 ||weight[i]<weight[v]))v=i;
    }

    if(v==-1)break;
    us[v]=true;
    ans+=weight[v];

    for(int i=1;i<=n;i++){
      if(weight[i]>g[v][i])weight[i]=g[v][i];
      else weight[i]=weight[i];
    }
  }
  return ans;
}


int main(){

  //load n
  cin >>n;

  //load data
  for(int i=1;i<=n;i++){
    weight[i]=INT_MAX;
    us[i]=false;
    for(int j=1;j<=n;j++){
      scanf("%d",&g[i][j]);
      if(g[i][j]== -1)g[i][j]=INT_MAX;
      //    cout <<g[i][j]<<" ";
    }
    //    cout <<endl;
  }




  //print
  cout <<prim()<<endl;

  return 0;

}

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

C++: #include <iostream>
#include <vector>
#include <queue>
#include <functional>
#include <string.h>

using namespace std;
typedef pair<int, int> P;
struct edge
{
	int to;
	int cost;
};

vector<edge> G[101];
bool isUsed[101];

void solve()
{
	int n;
	cin >> n;
	vector< vector<int> > data((n), vector<int>(n));
	for (int i = 0; i < n; ++i)
	{
		for (int j = 0; j < n; ++j)
		{
			int c;
			cin >> data[i][j];
		}
	}
	for (int i = 0; i < n; ++i)
	{
		for (int j = i; j < n; ++j)
		{
			if (data[i][j] != -1)
			{
				edge e1, e2;
				e1.to = j;
				e1.cost = data[i][j];
				e2.to = i;
				e2.cost = data[j][i];
				G[i].push_back(e1);
				G[j].push_back(e2);
			}
		}
	}
	memset(isUsed, 0, sizeof(isUsed));
	int ans = 0;
	priority_queue < P, vector<P>, greater<P> > pque;
	pque.push(P(0, 0));
	while (!pque.empty())
	{
		P p = pque.top();
		pque.pop();
		int v = p.second;
		if (isUsed[v])
		{
			continue;
		}
		isUsed[v] = true;
		ans += p.first;
		for (int i = 0; i < G[v].size(); ++i)
		{
			edge e = G[v][i];
			pque.push(P(e.cost, e.to));
		}
	}
	cout << ans << endl;
}

int main()
{
	solve();
	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;

struct Node{
  int color, parent;
};

int n, M[MAX][MAX];
Node node[MAX];

void Printnodes(){
  string col[3] = {"WHITE", "GRAY ", "BLACK"};
  for(int i = 0; i < n; i++){
    cout << "nodeID : " << i ;
    cout << " color : " << col[node[i].color];
    cout << " parent : " << node[i].parent << endl;
  }
  cout << endl;
}

int main(){
  cin >> n;

  for(int i = 0; i < n; i++){
    for(int j = 0; j < n; j++){
      cin >> M[i][j];
    }
    node[i].color = WHITE;
    node[i].parent = -1;
  }

  node[0].color = BLACK;
  int totaldist = 0;

  while(true){
    Printnodes();

    int count = 0, min_d = INFTY, min_id, dist;
    for(int p = 0; p < n; p++){
      if(node[p].color == BLACK){
        for(int i = 0; i < n; i++){
          if(M[p][i] > 0 && node[i].color != BLACK){
            count++;
            node[i].color = GRAY;
            dist = M[p][i];
            if(dist < min_d){
              min_d = dist;
              min_id = i;
              node[i].parent = p;
            }
          }
        }
      }
    }

    if(count == 0)
      break;
    node[min_id].color = BLACK;
    totaldist += min_d;
  }

  cout << totaldist << endl;
  return 0;
}

C++: #include <stdio.h>
using namespace std;
static const int MAX = 100;
static const int INFTY = (1<<21);
static const int WHITE = 0;
static const int GRAY = 0;
static const int BLACK = 0;

int n, M[MAX][MAX];

int prim() {
    int d[n], p[n];
    int color[n];
    
    for(int i=0; i<n; i++) {
        color[i] = WHITE;
        d[i] = INFTY;
    }
    d[0] = 0;
    p[0] = -1;
    
    int sum = 0;
    
    while(1) {
        int u;
        int mincost = INFTY;
        for (int i=0; i<n; i++) {
            if (color[i] != BLACK && d[i] < mincost) {
                mincost = d[i];
                u = i;
            }
        }
        
        if (mincost == INFTY) break;
        
        color[u] = BLACK;
        
        int tmp=0;
        for (int v=0; v<=n; v++) {
            if (color[v] != BLACK && M[u][v] != -1) {
                if(M[u][v] < d[v]) {
                    d[v] = M[u][v];
                    p[v] = u;
                    color[v] = GRAY;
                    tmp = d[v];
                }
            }
        }
        sum += tmp;
    }

    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]);
        }
    }
    
    int sum = prim();
    printf("%d\n", sum);
    
    return 0;
}