Go: package main

import (
	"bufio"
	"container/heap"
	"fmt"
	"os"
	"strconv"
)

type Node struct {
	id     int
	weight int
}

func init() {
	sc.Split(bufio.ScanWords)
}

type NodeHeap []Node

func (h NodeHeap) Len() int           { return len(h) }
func (h NodeHeap) Less(i, j int) bool { return h[i].weight < h[j].weight }
func (h NodeHeap) Swap(i, j int)      { h[i], h[j] = h[j], h[i] }

func (h *NodeHeap) Push(x interface{}) {
	*h = append(*h, x.(Node))
}

func (h *NodeHeap) Pop() interface{} {
	old := *h
	n := len(old)
	x := old[n-1]
	*h = old[0 : n-1]
	return x
}

var sc = bufio.NewScanner(os.Stdin)

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

func scanString() string {
	sc.Scan()
	return sc.Text()
}

func init() {
	sc.Split(bufio.ScanWords)
}

func main() {
	n := scanInt()

	list := make([][]int, n)
	seen := make([]bool, n)
	for i := range list {
		list[i] = make([]int, n)
		for j := range list[i] {
			list[i][j] = scanInt()
		}
	}

	start := Node{id: 0, weight: 0}
	h := &NodeHeap{start}
	heap.Init(h)

	sum := 0
	for h.Len() > 0 {
		node := heap.Pop(h).(Node)
		if seen[node.id] {
			continue
		}
		seen[node.id] = true
		sum += node.weight

		for i, w := range list[node.id] {
			if w < 0 {
				continue
			}
			heap.Push(h, Node{id: i, weight: w})
		}
	}

	fmt.Println(sum)
}

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, i;
    int d[MAX], q[MAX], color[MAX];
    
    for ( i = 0; i < n; i++ ) {
        d[i] = INFTY;
        q[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];
                    q[v] = u;
                    color[v] = GRAY;
                }
            }
        }
    }
    int sum = 0;
    for ( int i = 0; i < n; i++ ){
        if( q[i] != -1 ) sum += M[i][q[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>
using namespace std;
#define white 0
#define gray 1
#define black 2
#define NIL -1
#define MAX 2000
#define N 100
#define INFTY (1<<21)

int n,a[N][N];

int prim(){
    int u,minv;
    int d[MAX],p[MAX],color[MAX];
    
    for(int i = 0;i < n;i++){
        d[i] = INFTY;
        p[i] = NIL;
        color[i] = white;
    }
    
    d[0] = 0;
    
    while(1){
        minv = INFTY;
        u = NIL;
        for(int i = 0;i < n;i++){
            if(minv > d[i]&&color[i] != black){
                u = i;
                minv = d[i];
            }
        }
        if(u == NIL)break;
        color[u] = black;
        for(int v = 0;v < n;v++){
            if(color[v] != black&&a[u][v] != INFTY){
                if(d[v] > a[u][v]){
                    d[v] = a[u][v];
                    p[v] = u;
                    color[v] = gray;
                }
            }
        }
    }
    int sum = 0;
    for(int i = 0;i < n;i++){
        if(p[i] != NIL)sum += a[i][p[i]];
    }
    return sum;
}

int main(){
    cin >> n;
    
    for(int i = 0;i < n;i++){
        for(int j = 0;j < n;j++){
            cin >> a[i][j];
            if(a[i][j] == NIL)a[i][j] = INFTY;
        }
    }
    
    cout << prim() << endl;
    
    return 0;
}

C++: #include<bits/stdc++.h>
#define INF 10000000
#define NIL -1
#define BLACK 2
#define WHITE 0
#define GRAY 1
#define MAX 100
using namespace std;

int color[MAX]={},d[MAX]={},pi[MAX]={},n;

int prim(int w[MAX][MAX]){
    int u,i,v,mincost;
    for(u=0;u<n;u++){
        d[u]=INF;
        pi[u]=NIL;
        color[u]=WHITE;
    }
    d[0]=0;
    while(1){
        mincost = INF;
        u=-1;
        for(i=0;i<n;i++){
            if(color[i]!=BLACK&&d[i]<mincost){
                mincost=d[i];
                u=i;
            }
        }
        if(u==-1)break;
        color[u]=BLACK;
        for(v=0;v<n;v++){
            if(color[v]!=BLACK&&w[u][v]!=INF){
                if(d[v]>w[u][v]){
                    pi[v]=u;
                    d[v]=w[u][v];
                    color[v]=GRAY;
                }
            }
        }
    }
    int sum=0;
    for(i=0;i<n;i++){
        if(pi[i]!=NIL)sum+=w[i][pi[i]];
    }
    return sum;
}

int main(){
    int w[MAX][MAX],i,j;
    cin>>n;
    for(i=0;i<n;i++){
        for(j=0;j<n;j++){
            cin>>w[i][j];
            if(w[i][j]==-1)w[i][j]=INF;
        }
    }
    cout<<prim(w)<<endl;
}

C++: #include<iostream>
#include<vector>
#include<queue>
using namespace std;
template< typename FirstType, typename SecondType >
struct PairComparator {
  bool operator()( const pair<FirstType, SecondType>& p1, const pair<FirstType, SecondType>& p2 ) const 
    {  
		return p1.first > p2.first;
    }
};
bool inMST(vector< std::pair<int,int> > & MST,int j) {
	for (int k=0;k<(int)MST.size();++k){
		if(j == MST[k].first)
			return true;
	}
	return false;
}
int main(){
	int n;
	cin >> n;
	vector<int> E(n*n);
	vector< std::pair<int,int> > MST;

	for(int k=0;k<n*n;++k) {
		cin >> E[k];
	}
	priority_queue<pair<int,pair<int,int> >,vector< pair<int,pair<int,int> > >,
		PairComparator<int,pair<int,int> > > r;

	//put Node 0
	int cost = 0;
	int i=0;
	MST.push_back(pair<int,int>(i,i));
	while((int)MST.size() < n) {
		int ii=i*n;
		for (int j=0;j<n;++j) {
			if (E[ii+j] > 0) {
				pair<int,int> add = std::make_pair(i,j);
				if (!inMST(MST,add.second)){
					r.push(std::pair<int,std::pair<int,int> >(E[ii+j],add));
				}
			}
		}
		pair<int,pair<int,int> > top;
		while (!r.empty()) {
			top = r.top();
			r.pop();
			if (!inMST(MST,top.second.second)){
				break;
			}
		}
		cost += top.first;
		MST.push_back(top.second);
		i = top.second.second;
	}
	cout << cost << endl;
}

C++: #include<bits/stdc++.h>
#define REP(i,n) for(int i=0;i<(int)(n);i++)
using namespace std;

int N;
const int MAX_E=100;
int label[MAX_E];
priority_queue<int,vector<pair<int,pair<int,int> > >,greater<pair<int,pair<int,int> > > > pq;

int main(){
  cin>>N;
  REP(i,N)
    label[i]=i;
  REP(i,N){
    REP(j,N){
      int a;cin>>a;
      if(a>=0&&i>j){
	pq.push(make_pair(a,make_pair(i,j)));
      }
    }
  }
  int size=0;
  while(!pq.empty()){
    int cost=pq.top().first;
    pair<int,int> e=pq.top().second;
    //printf("%d,%d,%d\n",cost,e.first,e.second);
    pq.pop();
    if(label[e.first]!=label[e.second]){
      int tmp=label[e.second];
      size+=cost;
      REP(i,N){
	if(label[i]==tmp)
	  label[i]=label[e.first];
	//printf("%d ",label[i]);
      }
      //printf("\n");
    }
  }
  printf("%d\n",size);
}