Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func main() {
	fp := os.Stdin
	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(os.Stdout)

	n := getNextInt(scanner)

	g := make([][]int, n)
	ans := make([][]int, n)

	inf := 1 << 30
	for i := 0; i < n; i++ {
		g[i] = make([]int, n)
		ans[i] = make([]int, n)
		for ii := 0; ii < n; ii++ {
			g[i][ii] = getNextInt(scanner)
			if g[i][ii] == -1 {
				g[i][ii] = inf
			}
			ans[i][ii] = -1
		}
	}

	im := make(IntMap, 1)

	im.increment(1)

	for len(im) < n {
		min := inf
		u := -1
		v := -1
		for i := range im {
			for ii := 0; ii < n; ii++ {
				if im.get(ii) != 0 {
					continue
				}
				if min > g[i][ii] {
					min = g[i][ii]
					u = i
					v = ii
				}
			}
		}
		im.increment(v)
		ans[u][v] = min
		ans[v][u] = min
	}

	sum := 0
	for i := 0; i < n-1; i++ {
		for ii := i + 1; ii < n; ii++ {
			if ans[i][ii] != -1 {
				sum += ans[i][ii]
			}
		}
	}

	fmt.Fprintln(writer, sum)
	writer.Flush()
}

// IntMap ...
type IntMap map[int]int

func (m IntMap) get(key int) int {
	if _, ok := m[key]; ok {
		return m[key]
	}
	return 0
}
func (m IntMap) increment(key int) {
	if _, ok := m[key]; ok == false {
		m[key] = 0
	}
	m[key]++
}

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

C++: #include<bits/stdc++.h>
#define wgt(x) (x.first)
#define s(x) (x.second.first)
#define t(x) (x.second.second)
#define mwe(w,s,t) make_pair(w,make_pair(s,t))

using namespace std;

typedef pair<int,int> edge;
typedef pair<int,edge> we;

int main()
{
	int n,G[100][100];
	bool used[100];
	scanf("%d",&n);
	for(int i=0;i<n;i++)
		for(int j=0;j<n;j++)
			scanf("%d",&G[i][j]);
	memset(used,false,sizeof(used));
	priority_queue<we,vector<we>,greater<we> > pq;
	int tot=0;
	used[0]=true;
	for(int i=0;i<n;i++)
		if(G[0][i]>=0)
			pq.push(mwe(G[0][i],0,i));
	while(!pq.empty())
	{
		we t=pq.top();pq.pop();
		if(used[t(t)]) continue;
		tot+=wgt(t);
		used[t(t)]=true;
		for(int i=0;i<n;i++)
			if(!used[i]&&G[t(t)][i]>=0)
				pq.push(mwe(G[t(t)][i],t(t),i));
	}
	printf("%d\n",tot);
	return 0;
}

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

static const int MAX_N   = 100;
static const int MAX_INT = std::numeric_limits<int>::max();
static const int UNKNOWN = 0;
static const int IN_MST  = 1;

int M[MAX_N][MAX_N];

// 任意の頂点uを起点としてMSTを構成する頂点集合Tを定義し、
// MSTに全ての頂点が含まれるまで、Tの周囲の辺のうち重みが
// 最小の辺を都度選択してTに加えていく。
int prim(int n) {
    int *costs   = new int[n];
    int *parents = new int[n];
    int *states  = new int[n];

    // 各変数を初期化する。
    for (int i = 0; i < n; i++) {
        costs[i]   = MAX_INT;
        parents[i] = -1;
        states[i]  = UNKNOWN;
    }

    costs[0] = 0;

    while (true) {
        int mincost = MAX_INT;
        int u = -1;

        // Tの周囲の辺のうち重みが最小の辺およびそれにつながる頂点を探す。
        for (int i = 0; i < n; i++) {
            if ( states[i] != IN_MST && costs[i] < mincost ) {
                u = i;
                mincost = costs[i];
            }
        }
        if ( u == -1 ) break;

        states[u] = IN_MST;
        for (int v = 0; v < n; v++) {
            if ( states[v] != IN_MST && M[u][v] < costs[v] ) {
                costs[v]   = M[u][v];
                parents[v] = u;
            }
        }
    }

    int total_cost = 0;
    for (int i = 0; i < n; i++) {
        if (parents[i] != -1)
            total_cost += M[i][ parents[i] ];
    }
    return total_cost;
}

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

    int cost;
    for (int u = 0; u < n; u++) {
        for (int v = 0; v < n; v++) {
            scanf("%d", &cost);
            M[u][v] = 0 <= cost ? cost : MAX_INT;
        }
    }

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

    return 0;
}