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 <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <string>
#include <map>
#include <bitset>
#include <vector>
#include <queue>

using namespace std;

typedef long long ll;
#define FOR(i,a,b)   for(ll i = (a); i < (b); i++ )
#define REP(i, n)    FOR(i,0,n)
typedef pair< int, int >   cp2;
typedef pair< int, cp2 >   cp3;
#define fi first
#define se second
#define sec  se.fi
#define thr  se.se
const ll mod = 1000000007;
//              123456789


 

///////////////////////////////////////////////
//
//
///////////////////////////////////////////////

////////////////////////////////////////////////
////////////////////////////////////////////////



priority_queue< cp3, vector<cp3>, greater<cp3> >p;
cp3 now;

int g[112];

int find( int key ){
	//cout<<key<<" "<<g[key]<<endl;
	if( g[key] < 0 ) return key;
	return g[key] = find( g[key] );
}

void unite( int x, int y ){
	x = find( x );
	y = find( y );
	if( x == y ) return;
	
	if( g[x] < g[y] ) swap( x, y );
	if( g[x] == g[y] ) g[y]--;//??±??????x???????????????
	//g[y] += g[x];		 //????????°???x??????????°???????
	g[x] = y;
}

int a[112][112];
int N;
int ans = 0;

int main(){
	
	cin>>N;
	
	fill( g, g+112, -1 );
	
	
	REP( i, N ){
		REP( j, N ){
			scanf("%d",&a[i][j]);
			if( a[i][j] != -1 ){
				p.push( cp3( a[i][j], cp2(i, j) ) );
				//printf("%d %d %d\n",p[idx].fi, p[idx].sec, p[idx].thr);
				
			}
		}
	}
	
	while( !p.empty() ){
		now = p.top();
		p.pop();
		
		if( find(now.sec) != find(now.thr) ){
			unite( now.sec, now.thr );
			ans += now.fi;
		}
	}
	
	cout<<ans<<endl;
	
	return 0;

}

C++: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MAX 100
#define WHITE 0
#define GRAY 1
#define BLACK 2

int G[MAX][MAX];
int color[MAX], d[MAX], p[MAX];
int n;

int prim();

int main(){
    cin >> n;
    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            cin >> G[i][j];
            if(G[i][j] == -1) G[i][j] = INT_MAX;
        }
    }

    cout << prim() << endl;

    // for(int i = 0; i < n; i++){
    //     cout << i << " " << p[i] << endl;
    // }

    return 0;
}

int prim(){
    // プリムのアルゴリズム
    // 重み付きグラフから最小全域木を求め、その辺の重みの合計を求める
    // colorは訪問状態を表す　
    // d[i]は頂点iから繋がる辺の内最小のもの　
    // p[i]は頂点iの親（木なので）
    for(int i = 0; i < n; i++){
        color[i] = WHITE;
        d[i] = INT_MAX;
        p[i] = -1;
    }
    
    d[0] = 0;

    while(1){
        int min = INT_MAX, u = -1;
        
        // 隣接する最小の辺を持つところに行く 頂点番号u 辺のおもみmin
        for(int i = 0; i < n; i++){
            if(d[i] < min && color[i] != BLACK){
                min = d[i];
                u = i;
            }
        }

        // 隣接するところが全てblack、または最小のものが存在しない時は探索終了
        if(u == -1) break;

        // 頂点番号uのところに訪問
        color[u] = BLACK;

        // uから繋がるところでまだ訪問してないところの内、最小のものを見つける
        for(int i = 0 ; i < n; i++){
            if(color[i] != BLACK && G[u][i] != INT_MAX){
                if(d[i] > G[u][i]){
                    d[i] = G[u][i];
                    p[i] = u;
                    color[i] = GRAY;
                }
            }
        }
    }

    int sum = 0;
    for(int i = 0; i < n; i++){
        if(p[i] != -1) sum += G[i][p[i]];
    }

    return sum;
}

C++: #include<cstdio>
#include<iostream>
#include<algorithm>
#define Inf 1<<21
#define Lens 105

using namespace std;
int N;
int w[Lens][Lens], p[Lens], d[Lens];
char c[Lens];

int prime()
{
	d[0] = 0;
	int num = 0;
	while (true)
	{
		int minv = Inf;
		int u = -1;
		for (int i = 0; i < N; i++)
		{
			if (minv > d[i] && c[i] == 'w')
			{
				minv = d[i];
				u = i;
			}
		}
		if (u == -1) break;
		c[u] = 'b';
		for (int i = 0; i < N; i++)
		{
			if (c[i] == 'w'&&w[u][i] != -1)
				if (d[i] > w[u][i])
				{
					d[i] = w[u][i];
					p[i] = u;
				}
		}
	}
	int sum = 0;
	for (int i = 1; i < N; i++)
	{
		sum = sum + w[i][p[i]];
	}
	return sum;
}

int main()
{
	cin >> N;
	for (int i = 0; i < N; i++)
	{
		c[i] = 'w';
		d[i] = Inf;
		p[i] = -1;
		for (int j = 0; j < N; j++)
		{
			int k;
			cin >> k;
			w[i][j] = (k == -1) ? Inf : k;
		}
	}
	cout << prime() << endl;
	return 0;
}

C++: #include <bits/stdc++.h>
#define nMAX 100
#define MAX 2000
using namespace std;
int n, res=0;
vector<vector<int>> M;
vector<int> now;

int buildT()
{
    int tryn, min, d[nMAX], p[nMAX], pass[nMAX];
    for(int i=0; i<n; i++)
    {
        if(i!=0) d[i] = MAX+1;
        p[i] = -1;
        pass[i] = 0;
    }
    while(1)
    {
        min = MAX + 1;
        tryn = -1;
        for(int i=0; i<n; i++)
        {
            if(min > d[i] && pass[i] !=now[2])
            {
                min = d[i];
                tryn = i;
            }
        }
        if(min == MAX + 1) break;
        pass[tryn] = now[2]; //went
        for(int i=0; i<n; i++)
        {
            if(pass[i] != now[2] && M[tryn][i] !=MAX + 1)
            {
                if(d[i] > M[tryn][i])
                {
                    d[i] = M[tryn][i];
                    p[i] = tryn;
                    pass[i] = now[1];
                }
            }
        }

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

int main()
{
    now.resize(3);
    for(int i=0; i<3; i++) now[i] = i;
    //0 NON 1TRYING 2DEAD
    //input
    cin >> n;
    M.assign(n, vector<int> (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] = MAX + 1;
        }
    }
    printf("%d\n", buildT());
}