Go: package main

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

const BIG_COST = 3000

const (
	ST_NOT_YET = iota
	ST_JOINED
)

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

	nNodes := scanInt(sc)
	costs := make([][]int, nNodes)

	for r := 0; r < nNodes; r++ {
		costs[r] = make([]int, nNodes)
		for c := 0; c < nNodes; c++ {
			costs[r][c] = scanInt(sc)
		}
	}

	states := make([]int, nNodes)
	minCosts := make([]int, nNodes)

	for i := 1; i < nNodes; i++ {
		minCosts[i] = BIG_COST
	}

	for {
		var u int
		minCost := BIG_COST

		for i := 0; i < nNodes; i++ {
			if states[i] != ST_JOINED && minCosts[i] < minCost {
				minCost = minCosts[i]
				u = i
			}
		}

		// 全てのノードが MST に JOIN できたので終了。
		if minCost == BIG_COST {
			break
		}

		states[u] = ST_JOINED

		for v := 0; v < nNodes; v++ {
			cost := costs[u][v]

			// MST内の u と MST外の v との辺の重みをそれぞれ minCosts[v] として
			// 保存しておく。このうち辺の重みが最小となる v が次のループで MST に JOIN する。
			if states[v] != ST_JOINED && cost != -1 && cost < minCosts[v] {
				minCosts[v] = cost
			}
		}
	}

	sum := 0
	for _, cost := range minCosts {
		sum += cost
	}
	fmt.Println(sum)
}

func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	v, _ := strconv.Atoi(sc.Text())
	return v
}

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