Go: package main

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

const (
	MAX   = 100
	INFTY = 1 << 21
	NIL   = -1
)

const (
	WHITE = iota
	GRAY
	BLACK
)

type Graph [MAX][MAX]int

func (g *Graph) prim(n int) int {
	var (
		u, minv     = 0, 0
		d, p, color [MAX]int
	)

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

	for {
		minv = INFTY
		u = -1
		for 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 v := 0; v < n; v++ {
			if color[v] != BLACK && g[u][v] != INFTY {
				if d[v] > g[u][v] {
					d[v] = g[u][v]
					p[v] = u
					color[v] = GRAY
				}
			}
		}
	}

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

	return sum
}

func answer(reader io.Reader) string {
	sc := bufio.NewScanner(reader)
	sc.Split(bufio.ScanWords)

	sc.Scan()
	n, _ := strconv.Atoi(sc.Text())

	var g Graph
	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			sc.Scan()
			e, _ := strconv.Atoi(sc.Text())
			if e == -1 {
				g[i][j] = INFTY
				continue
			}
			g[i][j] = e
		}
	}

	return fmt.Sprint(g.prim(n))
}

func main() {
	fmt.Println(answer(os.Stdin))
}

C++: #include <functional>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
typedef vector <int> vi;
typedef vector <vi> vvi;
struct edge{
	int from;
	int to;
};
typedef multimap <int, edge, less<int> > mme;

int addedge(vvi &graph, int y, int x, int val=1){
	graph[y][x] = val;
	return val;
}

int make_graph(vvi &graph, vi &stamp){
	int n=graph.size(), val;

	for (int j=0; j<n; j++){
		for (int i=0; i<n; i++){
			cin>>val;
			addedge(graph, i, j, val);
		}
		stamp[j] = 0;
	}
	return 0;
}

int Minimum_Spanning_Tree(vvi &graph, vi &stamp){

	mme edges;
	int n = graph.size(), p1, p2, p3, total=0;
	p1=0;
	stamp[p1] = 1;

	while (p1!=-1){
		for (int p2=0; p2<n; p2++){
			int *tmp=&graph[p1][p2];
			if (*tmp!=-1 && stamp[p2]==0){
				edge e;
				e.from = p1;
				e.to = p2;
				edges.insert(pair <int, edge> (*tmp, e));
			}
		}
		p1=-1;
		while (! edges.empty()){
			auto it = edges.begin();
			if (stamp[it->second.from]==1 && stamp[it->second.to]==0){
				p1 = it->second.to;
				stamp[p1]=1;
				total+=it->first;
				edges.erase(it);
				break;
			}
			edges.erase(it);
		}
	}
	return total;
}

int resultout(vvi &graph){
	for (int i=0; i<int(graph.size()); i++){
		for (int j=0; j<int(graph.size()); j++){
		cout<<graph[i][j]<<" ";
		}
		cout<<endl;
	}
	return 0;
}

int main(void){

	int n, row;
	cin>>n;
	vvi G(n, vi(n));
	vi stamp(n);

	make_graph(G, stamp);

	cout<<Minimum_Spanning_Tree(G, stamp)<<endl;

	return 0;
}

C++: #include <iostream>
#include <stdlib.h>
#include <cstdio>
using namespace std;
static const int N = 100;
static const int WHITE = 0, GRAY = 1, BLACK = 2;
static const int INFI = (1<<21);  // data type?

int n, sum, M[N][N], d[N], p[N], color[N];

void prim()
{
	for(int u = 0; u < n; u++) d[u] = INFI; 
	int u = 0;
	d[0] = 0;
	
	while(true)
	{
		//update current MST.
		int mincost = INFI;
		int i;
		for(i = 0; i < n; i++)
		{
			if(color[i] != BLACK && d[i] < mincost)
			{
				mincost = d[i];
				u = i;
			}
		}
//		printf("yes1");
		if(mincost == INFI) break;
		color[u] =  BLACK; 
		
	
		//update weight of candidates.
		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];
					//printf("%d-%d: %d", u, v, d[v]);
					//printf("yes");
					p[v] = u;
					color[v] = GRAY;
				} 				
			}
		}	
	}
	
	for(int i = 0; i < n; i++)
	{
		sum += d[i];
//		printf("%d ", d[i]);
	}
	
	printf("%d\n", sum);
}


int main() {
	sum = 0;
	scanf("%d", &n);
	for(int u = 0; u < n; u++)
	{
		for(int v = 0; v < n; v++)
		{
			int value;
			scanf("%d", &value);
			M[u][v] = value;
		}
	}
	
	prim();
	
//	system("pause");
	return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <stdio.h>
#include <math.h>
#include <map>
#include <algorithm>

using namespace std;

#define FAR (1<<22)
#define N 100

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


int n, color[N], d[N], p[N];
int m[N][N];

int prim(){
	for (int i = 0; i < n; ++i){
		color[i] = WHITE;
		d[i] = FAR;
		p[i] = -1;
	}

	d[0] = 0;
	int dist = FAR;

	while (1)
	{
		int u = -1;
		dist = FAR;
		for (int i = 0; i < n; ++i) {
			if (color[i] != BLACK && d[i] < dist){
				dist = d[i];
				u = i;
			}
		}

		if (-1 == u) break;
		color[u] = BLACK;

		for (int v = 0; v < n; ++v){
			if (color[v] != BLACK && (m[u][v] < d[v])){
				d[v] = m[u][v];
				p[v] = u;
				color[v] = GRAY;
			}
		}
	}
	dist = 0;
	for (int i = 0; i < n; ++i){
		if (p[i] != -1) dist += m[i][p[i]];
	}
	return dist;
}

int main(){
	cin >> n;
	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < n; ++j) {
			int val; cin >> val;
			m[i][j] = (val == -1) ? FAR : val;
		}
	}
 
	cout << prim() << endl;

	return 0;
}

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


struct edge
{
   int cost;
   int src;
   int dst;
};

edge e[5000];
int A[100][100];
int M=0;
int P[100];

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

void checkEdges(int n)
{
    for(int r=0;r<n;r++)
        for(int c=r+1;c<n;r++)
    {
        if(A[r][c]>-1)
        {
            e[M].cost=A[r][c];
            e[M].src=r;
            e[M].dst=c;
            M+=1;
        }
    }
}

bool cmp(edge e1,edge e2)
{
    return e1.cost<=e2.cost;
}
int main()
{
    int n,count=0;
    cin>>n;
    init(n);
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++){
                cin>>A[i][j];
        }
    }
    checkEdges(n);
    sort(e,e+M,cmp);
    for (int i=0; i<M; ++i)
    {
        if(is_same_set(e[i].src,e[i].dst))
        {
            continue;
        }
        unite(e[i].dst,e[i].src);
        count+=e[i].cost;
    }
    cout<<count<<endl;
    //system("pause");
    return 0;
}

C++: #include<stdio.h>
#include<stdlib.h>
#define N 100
#define INF 1000000
#define MIN(a, b) ((a) < (b) ? (a) : (b))

int a[N][N];

typedef struct node{
	int id;
	int cost;
}node;

void swap(node h[],int x,int y){
	node temp=h[x];
	h[x]=h[y];
	h[y]=temp;
	return;
}


void heapsort(node heap[],int num){

	for(int i=num-1;i>0;i--){
		if(i%2==0){
			if(heap[i].cost < heap[i/2-1].cost)swap(heap,i,i/2-1);
		}else {
			if(heap[i].cost < heap[i/2].cost)swap(heap,i,i/2);
		}
	}
	
	return;

}

int update_heap(node heap[],int num){
	int u=heap[0].id;
	swap(heap,0,num-1);
	num--;
	
	for(int i=0;i<num;i++){
		heap[i].cost=MIN(heap[i].cost,a[u][heap[i].id]);
	}
	
	return num;
}




int main(){
	int i,j,n;
	int num=0;/*heapの要素の個数*/
	int ans=0;
	node heap[N];
	
	
	scanf("%d",&n);
	
	
	/*隣接行列の読み込み*/
	for(i=0;i<n;i++){
		for(j=0;j<n;j++){
			scanf("%d",&a[i][j]);
			if(a[i][j]==-1)a[i][j]=INF;
		}
	}
	
	/*0以外の頂点をinsert*/
	for(i=1;i<n;i++){
		heap[num].cost=a[0][i];
		heap[num++].id=i;
	}
	
	/*heapを昇順にソート*/
	heapsort(heap,num);
	
	while(num!=0){
		ans+=heap[0].cost;
		num=update_heap(heap,num);
		heapsort(heap,num);
	}
	
	printf("%d\n",ans);
	
	return 0;
	
}