Go: package main

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

var scanner = bufio.NewScanner(os.Stdin)

func nextString() string {
	scanner.Scan()
	return scanner.Text()
}

func nextInt() int {
	n, err := strconv.Atoi(nextString())
	if err != nil {
		fmt.Printf("strconv.Atoi failed: %v\n", err)
	}
	return n
}

type ElementKey int
type Node struct {
	key    ElementKey
	parent *Node
	rank   int
}

func newNode(key ElementKey) *Node {
	node := Node{}
	node.key = key
	node.parent = &node
	node.rank = 0

	return &node
}

func union(x, y *Node) {
	xSet := findSet(x)
	ySet := findSet(y)
	link(xSet, ySet)
}

func link(x, y *Node) {
	if x.rank > y.rank {
		y.parent = x
	} else {
		x.parent = y

		if x.rank == y.rank {
			y.rank++
		}
	}
}

func findSet(x *Node) *Node {
	if x != x.parent {
		x.parent = findSet(x.parent)
	}
	return x.parent
}

func newDisjointSets(nSets int) *DisjointSets {
	sets := DisjointSets{}
	sets.keyToNode = map[ElementKey]*Node{}
	for i := 0; i < nSets; i++ {
		key := ElementKey(i)
		sets.keyToNode[key] = newNode(key)
	}
	return &sets
}

type DisjointSets struct {
	keyToNode map[ElementKey]*Node
}

func (sets *DisjointSets) union(x, y ElementKey) {
	xNode := sets.keyToNode[x]
	yNode := sets.keyToNode[y]
	union(xNode, yNode)
}

func (sets DisjointSets) findSet(x ElementKey) *Node {
	xNode := sets.keyToNode[x]
	return findSet(xNode)
}

type Edge struct {
	i, j   int
	weight int
}

func main() {
	scanner.Split(bufio.ScanWords)

	n := nextInt()
	sets := newDisjointSets(n)
	edges := []Edge{}
	a := make([][]int, n)
	for i := 0; i < n; i++ {
		a[i] = make([]int, n)
		for j := 0; j < n; j++ {
			a[i][j] = nextInt()
			if a[i][j] >= 0 {
				e := Edge{
					i:      i,
					j:      j,
					weight: a[i][j],
				}
				edges = append(edges, e)
			}
		}
	}
	sort.Slice(edges, func(i, j int) bool { return edges[i].weight < edges[j].weight })
	totalWeight := 0
	for _, edge := range edges {
		i := ElementKey(edge.i)
		j := ElementKey(edge.j)
		if sets.findSet(i) != sets.findSet(j) {
			totalWeight += edge.weight
			sets.union(i, j)
		}
	}
	fmt.Println(totalWeight)
}

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

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, min_value;
	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) {
		min_value = INFTY;
		u = -1;
		for (int i = 0; i < n; i++) {
			if (min_value > d[i] && color[i] != BLACK) {
				u = i;
				min_value = 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;
}