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 <bits/stdc++.h>
using namespace std;
#define FOR(i,k,n) for(int i = (k); i < (n); i++)
#define REP(i,n) FOR(i,0,n)
#define INF 114514810
#define MOD 1000000007
#define ALL(a) begin(a),end(a)
#define MS(m,v) memset(m,v,sizeof(m))
#define D10  fixed<<setprecision(10)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> P;
typedef long long ll;
struct edge
{
	int from, to, cost;
	bool operator < (const edge& e) const { return cost < e.cost; }
	bool operator >(const edge& e) const { return cost > e.cost; }
};
///*************************************************************************************///
///*************************************************************************************///
///*************************************************************************************///

int V, E;
bool used[10000];
int d[105][105];

int prim()
{
	memset(used, false, sizeof(used));
	int sum = 0;

	priority_queue<edge, vector<edge>, greater<edge>> que;
	que.push(edge{ -1, 0, 0 });

	while (!que.empty())
	{
		edge e = que.top(); que.pop();
		if (used[e.to]) continue;
		sum += e.cost;
		used[e.to] = true;
		REP(i, V)
		{
			if (!used[i]) que.push(edge{ e.to, i, d[e.to][i] });
		}
	}
	return sum;
}

int main()
{
	cin >> V;
	REP(i, V)REP(j, V)
	{
		int t;
		cin >> t;
		d[i][j] = (t == -1 ? INF : t);
	}
	cout << prim() << endl;
}

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

C++: #include <iostream>
const int MAX_N = 100;
const int INF = 999999999;
const int WHITE = 0;
const int GRAY = 1;
const int BLACK = 2;
using namespace std;

int n;
int color[MAX_N];
int adj[MAX_N][MAX_N];
int d[MAX_N], p[MAX_N];

int prim(void) {
    int u, minv;

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

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

        color[u] = BLACK;
        for (int v = 0; v < n; v++) {
            if (color[v] != BLACK && adj[u][v] != INF) {
                if (d[v] > adj[u][v]) {
                    d[v] = adj[u][v];
                    p[v] = u;
                    color[v] = GRAY;
                }
            }
        }
    }

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

    return sum;
}

int main(void) {
    cin >> n;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            int v; cin >> v;
            if (v == -1) adj[i][j] = INF;
            else adj[i][j] = v;
        }
    }

    cout << prim() << endl;
    return 0;
}

C++: #include <bits/stdc++.h>
/*
5 6
1 2 5
2 3 8
3 4 1
4 5 2
2 5 3
2 4 6

5 6
1 3 4
1 4 3
3 4 5
2 4 1
2 5 2
4 5 6

5 6
0 2 23
0 3 1
2 3 6
3 1 8
1 4 5
3 4 1
*/

using namespace std;
const int maxn = 1e5 + 10;
int n , m;
vector <int> comp [ maxn ];
int num[ maxn ];
int ans = 0;
pair <int , pair < int , int > > edge [ maxn ];

void merge ( int x , int y ){
	if ( comp [ y ].size() < comp [ x ].size() ){
		//cout << "size of " << x << " = "<< comp [ x ].size() << "  " << "|| size of " << y << " = "<< comp [ y ].size() << endl;
		for ( int i = comp [ y ].size() - 1 ; i >= 0 ; i-- ){
			//cout << comp [ y ][ i ] << " " << comp [ x ][ i ] << " ";
			comp [ x ].push_back( comp [ y ][ i ] );
			comp [ y ].pop_back();
			num [ comp [ y ][ i ] ] = num [ comp [ x ][ 0 ] ];
		}
		//cout <<endl;
		/*
		cout << "rasha_e moalefe1 = ";
		for (int i = 0 ; i < comp [ x ].size() ; i++ ){
			cout << comp [ x ][ i ] << " ";
		}
		cout <<endl;
		*/
	}
	else{
		//cout << "size of " << y << " = "<< comp [ y ].size() << "  " << "|| size of " << x << " = "<< comp [ x ].size() << endl;
		for ( int i = comp [ x ].size() - 1 ; i >= 0 ; i-- ){
			//cout << comp [ x ][ i ] << " " << comp [ y ][ i ] << " ";
			comp [ y ].push_back( comp [ x ][ i ] );
			comp [ x ].pop_back();
			num [ comp [ x ][ i ] ] = num [ comp [ y ][ 0 ] ];
		}
		//cout <<endl;
		/*
		cout << "rasha_e moalefe2 = ";
		for (int i = 0 ; i < comp [ y ].size() ; i++ ){
			cout << comp [ y ][ i ] << " ";
		}
		cout <<endl;
		*/
	}
}

int main (){
	cin >> n >> m;
	for ( int i = 0 ; i < m ; i++ ){
		cin >> edge [ i ].second.first >> edge [ i ].second.second >> edge [ i ].first;
	}
	sort ( edge , edge + m );
	for ( int i = 0 ; i <= n ; i++ ){
		num [ i ] = i;
		comp [ i ].push_back ( i );
	}
	for ( int i = 0 ; i <= m ; i++ ){
		//cout << edge [ i ].second.first << " " << edge [ i ].second.second << endl;
		//cerr <<"i= " << i << "   " << num [ edge [ i ].second.first ] <<" "<< num [ edge [ i ].second.second ] << endl;
		//cerr << endl;
		if ( num [ edge [ i ].second.first ] != num [ edge [ i ].second.second ] ){
			//cerr << endl;
			//cerr <<"i= " << i << "   " << num [ edge [ i ].second.first ] <<" "<< num [ edge [ i ].second.second ] << endl;
			ans += edge [ i ].first;
			//cout << ans << " " << edge [ i ].second.first << " " << edge [ i ].second.second << endl;
			//cerr << endl;
			merge ( num [ edge [ i ].second.first ]  , num [ edge [ i ].second.second ] );
			//cerr <<"i= " << i << "   " << num [ edge [ i ].second.first ] <<" "<< num [ edge [ i ].second.second ] << endl;
		}
	}
	/*
	for ( int i = 1 ; i <= n ; i++ ){
		cout << num [ i ] << " ";
	}
	*/
	cout << ans;
}