Go: package main

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

// Define disjoint-set forests

type vertexNo int

type vertex struct {
	number vertexNo
	rank int
	parent *vertex
}

func makeSet(number vertexNo) vertex {
	return vertex {
		number: number,
		rank: 0,
		parent: nil,
	}
}

func union(x, y *vertex) *vertex {
	return link(findSet(x), findSet(y))
}

func link(x, y *vertex) *vertex {
	if x.rank > y.rank {
		y.parent = x
		return x
	} else {
		x.parent = y
		if x.rank == y.rank {
			y.rank += 1
		}
		return y
	}
}

func findSet(v *vertex) *vertex {
	if v.parent == nil {
		return v
	}
	v.parent = findSet(v.parent)
	return v.parent
}

// Function to calculate minimum spanning tree

type matrix [][]int

type edge struct {
	one vertexNo
	two vertexNo
	weight int
}

const(
	MAX_VERTICES = 100
)

func newEdgesFromMatrix(matrix matrix) []edge {
	edges := make([]edge, 0, MAX_VERTICES * MAX_VERTICES)

	for i := 0; i < len(matrix); i++ {
		row := matrix[i]
		for j := i; j < len(row); j++ {
			if row[j] >= 0 {
				edge := edge{one: vertexNo(i+1), two: vertexNo(j+1), weight: row[j]}
				edges = append(edges, edge)
			}
		}
	}

	return edges
}

func solve(matrix matrix) int {
	nVertices := len(matrix)

	// initialize vertices
	vertices := make([]vertex, nVertices)
	for i := 0; i < nVertices; i++ {
		vertices[i] = makeSet(vertexNo(i+1))
	}

	// prepare edges sorted in the ascending order of weight
	edges := newEdgesFromMatrix(matrix)
	sort.Slice(edges, func(i, j int) bool {
		return edges[i].weight < edges[j].weight
	})

	unionCount := 0
	weightSum := 0
	for unionCount < nVertices-1 {
		for i := 0; i < len(edges); i++ {
			edge := edges[i]
			v1 := &vertices[edge.one-1]
			v2 := &vertices[edge.two-1]
			rep1 := findSet(v1)
			rep2 := findSet(v2)
			if rep1.number == rep2.number {
				continue
			}
			union(rep1, rep2)
			edges = append(edges[0:i], edges[i+1:]...)
			weightSum += edge.weight
			unionCount++
			break
		}
	}

	return weightSum
}

// main

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	n, err := nextInt(sc)
	if err != nil {
		panic(err)
	}

	matrix := parseMatrix(n, sc)

	edges := newEdgesFromMatrix(matrix)
	sort.Slice(edges, func(i, j int) bool {
		return edges[i].weight < edges[j].weight
	})

	fmt.Println(solve(matrix))
}

func parseMatrix(n int, sc *bufio.Scanner) [][]int {
	matrix := make([][]int, n)
	for i := 0; i < n; i++ {
		matrix[i] = make([]int, n)
		for j := 0; j < n; j++ {
			x, err := nextInt(sc)
			if err != nil {
				panic(err)
			}
			matrix[i][j] = x
		}
	}
	return matrix
}

func nextText(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

func nextInt(sc *bufio.Scanner) (int, error) {
	return strconv.Atoi(nextText(sc))
}

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

C++: #include<iostream>
#include<cstdio>
#define INF 1000000
#define NIL -2
#define GRAY -3
#define WHITE -4
#define BRACK -5

using namespace std;

int n;
int M[101][101];
int d[101];//重み
int p[101];//親
int color[101];
int sum = 0;

void Prim(int u) {
	color[u] = BRACK;

	for (int i = 1; i <= n; i++) {
		if (color[i] != BRACK && M[u][i] != -1) {
			color[i] = GRAY;
			if (d[i] > M[u][i]) {
				d[i] = M[u][i];
				p[i] = u;
			}
		}
	}

	sum += d[u];

	//全体のうち重みが一番小さいものについて再帰
	//color[v] = WHITE かつ　d[v]が一番小さい点v について再帰
	//かつGRAYの点とつながっている
	int min = 0;
	d[min] = 100000;


	for (int v = 1; v <= n; v++) {
		if (color[v] == GRAY) {
			if (d[v] < d[min]) {
				min = v;
			}
		}
	}

	if (min != 0) {
		Prim(min);
	}
}

int main() {
	cin >> n;

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

	for (int i = 1; i <= n; i++) {
		d[i] = INF;
		p[i] = NIL;
	}
	d[1] = 0;

	//関数
	Prim(1);

	cout << sum << endl;


	return 0;
}

C++: #include <stdio.h>

#define NMAX 100
#define WMAX 2000
#define INF WMAX + 1
#define NIL -1

int n;
int M[NMAX][NMAX];
int d[NMAX]; // distance to mst
int p[NMAX]; // parent
int c[NMAX]; // c[i]=1 if i in mst

int solve() { // find total cost of mst
    for (int i = 0; i < n; i++) {
        c[i] = 0;
        p[i] = NIL;
        d[i] = INF;
    }
    d[0] = 0; // root
    p[0] = NIL;
    int total = 0;
    while (1) {
        // find i s.t d[i] is min
        int mincost = INF;
        int u;
        for (int i = 0; i < n; i++) {
            // if i not in mst and d[i] < mincost
            if (!c[i] && d[i] < mincost) {
                mincost = d[i];
                u = i;
            }
        }

        if (mincost == INF)
            break;

        // printf("include %d to mst!\n", u);
        c[u] = 1; // include u to mst
        total += mincost;

        // update d[i] for M[u][i]!=-1
        for (int i = 0; i < n; i++) {
            if (!c[i] && M[u][i] < d[i]) {
                d[i] = M[u][i];
                p[i] = u;
            }
        }
    }
    return total;
}

int main() {
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            int w;
            scanf("%d", &w);
            if (w != -1) {
                M[i][j] = w;
            } else {
                M[i][j] = INF;
            }
        }
    }
    printf("%d\n", 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;

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