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<utility>
#include<algorithm>
using namespace std;
int p[101];
void makeset(int N){
    for(int i=0;i<N;++i){
        p[i]=i;
    }
}
int root(int x){
    if(p[x]==x) return x;
    return (p[x]==root(p[x]));
}
void unite(int x,int y){
    p[root(x)]=root(y);
}
bool same(int x,int y){
    return root(x)==root(y);
}
int main(){
    int N,M=0;
    int A[101][101],cost[101],src[101],dst[101];
    cin>>N;
    makeset(N);
    for(int r=0;r<N;++r){
        for(int c=0;c<N;++c){
            cin>>A[r][c];
        }
        for(int c=r+1;c<N;++c){
            if(A[r][c]>-1){
                cost[M]=A[r][c];
                src[M]=r;
                dst[M]=c;
                M+=1;
            }
        }
    }
    pair<int,pair<int,int>>edges[101];
    for(int i=0;i<M;++i){
       edges[i]=make_pair(cost[i],make_pair(src[i],dst[i]));
    }
    sort(edges,edges+M);
    //for(int i=0;i<M;++i){
        //cout<<edges[i].first<<edges[i].second.first<<edges[i].second.second<<endl;
    //}
    int total=0,num=0,flag=0;
    for(int i=0;i<M;++i){
        //if(same(edges[i].second.first,edges[i].second.second)){continue;}
        //else{
            unite(edges[i].second.first,edges[i].second.second);
            total+=edges[i].first;
            num++;
            cout<<edges[i].first<<edges[i].second.first<<edges[i].second.second<<endl;
        //}
        if(num==N-1){
            flag=1;
            break;
        }
    }
    if(flag){
        cout<<total<<endl;
    }
}

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 GREY = 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 <utility>
#include <vector>
#include <algorithm>


using namespace std;

int P[110];

void init(int N){
    for(int i = 0; i < N; i++) P[i] = i;
}

int root(int a){
    if (P[a] == a) return a;
    return (P[a] = root(P[a]));
}

bool is_same_set(int a, int b){
    return root(a) == root(b);
}

void unite(int a, int b){
    P[root(a)] = root(b);
}

int main(){
    int n; 
    cin >> n; 
    int w;
    init(n);
    vector< pair<int, pair<int, int> > > edges;
    for (int i = 0; i < n; i++){
        for (int j = 0; j < n; j++){
            cin >> w;
            if (i > j && w != -1) edges.push_back(make_pair(w, make_pair(i, j)));
        }
    }
    sort(edges.begin(),edges.end());
    int sum = 0;
    while(!edges.empty()){
        pair<int, pair<int, int> > edge = edges[0];
        edges.erase(edges.begin());
        int cost = edge.first;
        int src = edge.second.first;
        int dst = edge.second.second;
        cout << cost << " " << src << " " << dst << endl;
        if (!is_same_set(src, dst)){
            cout << "unite " << src << " " <<  dst << " cost = " << cost << endl;
            unite(src, dst);
            sum += cost;
        }
    }
    cout << sum << endl;
}

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 (true) {
		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<bits/stdc++.h>
using namespace std;

int n,m[100][100];

int prim();

int main(){
  cin>>n;
  
  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
      cin>>m[i][j];
      if(m[i][j]==-1)m[i][j]=200002;
    }
  }

  cout<<prim()<<endl;

}

int prim(){
  int u,mn;
  int d[100],p[100],ck[100];
  
  for(int i=0;i<n;i++){
    d[i]=200002;
    p[i]=-1;
    ck[i]=0;
  }

  d[0]=0;

  while(1){
    mn=200002;
    u=-1;
    for(int i=0;i<n;i++){
      if(mn>d[i] && ck[i]!=2){
	u=i;
	mn=d[i];
      }
    }
    if(u==-1)break;
    ck[u]=2;
    for(int i=0;i<n;i++){
      if(ck[i]!=2 && m[u][i]!=200002){
	if(d[i]>m[u][i]){
	  d[i]=m[u][i];
	  p[i]=u;
	  ck[i]=1;
	}
      }
    }
  }
  int sum=0;
  for(int i=0;i<n;i++){
    if(p[i]!=-1)sum+=m[i][p[i]];
  }
  return(sum);
}