Go: package main

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

const (
	WHITE int = iota
	GRAY
	BLACK
)

type graph struct {
	G [][]int
	C []int
	D []int
	P []int
}

var INFTY = 1 << 32

func constructGraph() *graph {
	sc := bufio.NewScanner(os.Stdin)
	sc.Scan()
	n, _ := strconv.Atoi(sc.Text())

	g := make([][]int, n)
	c := make([]int, n)
	d := make([]int, n)
	p := make([]int, n)

	for i := 0; i < n; i++ {
		sc.Scan()
		fields := strings.Fields(sc.Text())
		g[i] = toIntSlice(fields)
	}

	return &graph{g, c, d, p}
}

func (g *graph) initMst() {
	for i := 0; i < len(g.G); i++ {
		g.C[i] = WHITE
		g.D[i] = INFTY
	}
	g.D[0] = 0
	g.P[0] = -1
}

func (g *graph) primMst() int {
	g.initMst()

	var u int
	for {
		mincost := INFTY
		for i := 0; i < len(g.G); i++ {
			if g.C[i] != BLACK && g.D[i] < mincost {
				mincost = g.D[i]
				u = i
			}
		}

		if mincost == INFTY {
			break
		}

		g.C[u] = BLACK

		for i := 0; i < len(g.G); i++ {
			if g.C[i] != BLACK && g.existEdge(u, i) {
				if g.G[u][i] < g.D[i] {
					g.D[i] = g.G[u][i]
					g.P[i] = u
				}
			}
		}
	}

	totalCost := 0
	for _, cost := range g.D {
		totalCost += cost
	}
	return totalCost
}

func (g *graph) existEdge(u, v int) bool {
	return g.G[u][v] != INFTY && g.G[v][u] != INFTY
}

func toIntSlice(s []string) []int {
	a := make([]int, len(s))
	for i, v := range s {
		num, _ := strconv.Atoi(v)
		if num == -1 {
			num = INFTY
		}
		a[i] = num
	}
	return a
}

func main() {
	g := constructGraph()
	fmt.Println(g.primMst())
}

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