Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func main() {
	fp := os.Stdin
	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(os.Stdout)

	n := getNextInt(scanner)

	g := make([][]int, n)
	ans := make([][]int, n)

	inf := 1 << 30
	for i := 0; i < n; i++ {
		g[i] = make([]int, n)
		ans[i] = make([]int, n)
		for ii := 0; ii < n; ii++ {
			g[i][ii] = getNextInt(scanner)
			if g[i][ii] == -1 {
				g[i][ii] = inf
			}
			ans[i][ii] = -1
		}
	}

	im := make(IntMap, 1)

	im.increment(1)

	for len(im) < n {
		min := inf
		u := -1
		v := -1
		for i := range im {
			for ii := 0; ii < n; ii++ {
				if im.get(ii) != 0 {
					continue
				}
				if min > g[i][ii] {
					min = g[i][ii]
					u = i
					v = ii
				}
			}
		}
		im.increment(v)
		ans[u][v] = min
		ans[v][u] = min
	}

	sum := 0
	for i := 0; i < n-1; i++ {
		for ii := i + 1; ii < n; ii++ {
			if ans[i][ii] != -1 {
				sum += ans[i][ii]
			}
		}
	}

	fmt.Fprintln(writer, sum)
	writer.Flush()
}

// IntMap ...
type IntMap map[int]int

func (m IntMap) get(key int) int {
	if _, ok := m[key]; ok {
		return m[key]
	}
	return 0
}
func (m IntMap) increment(key int) {
	if _, ok := m[key]; ok == false {
		m[key] = 0
	}
	m[key]++
}

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

C++: #include <iostream>
#include <cstring>
#define INF 2000
using namespace std;

int graph[105][105],n,closest[105],color[105],p[105];
int sum=0;
void prim(){
    int i,j;
    while(1){
        int v=-1,minv=INF;
        for(i=0;i<n;i++){
            if(minv>closest[i]&&color[i]!=1){
                minv=closest[i];
                v=i;
            }
        }
        if(v==-1)break;
        color[v]=1;
        closest[v]=INF;
        for(i=0;i<n;i++){
            if(color[i]!=1&&graph[v][i]<closest[i]&&graph[v][i]!=-1){
                closest[i]=graph[v][i];
                p[i]=v;
            }
        }
    }

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

int main()
{
    memset(color,0,sizeof(color));
    memset(graph,-1,sizeof(graph));
    cin>>n;
    int i,j;
    for(i=0;i<n;i++){
        for(j=0;j<n;j++){
            cin>>graph[i][j];
        }
    }
    for(i=0;i<n;i++){
        p[i]=-1;
        closest[i]=INF;
    }
    closest[0]=0;
    prim();
    cout<<sum<<endl;
    return 0;
}

C++: #define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
//#include "MyMath.h"
//#include "MyDisjointset.h"
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <functional>
#include <stdio.h>
using namespace std;
typedef pair<int, int> P;
const int INF = 2000000000;
int main() {
	int n; cin >> n;
	int m[101][101];
	for (int i = 0; i < n; i++)for (int j = 0; j < n; j++)
		cin >> m[i][j];
	int res = 0;
	int *d = new int[n];
	fill(d, d + n, INF); 
	d[0] = 0;
	bool *visit = new bool [n];
	fill(visit, visit + n, false);
	priority_queue <P, vector<P>, greater<P> > q;
	q.push(P(d[0], 0));
	while (!q.empty()) {
		int f = q.top().first, s = q.top().second;
		visit[s] = true;
		res += f;
		q.pop();
		for (int i = 0; i < n; i++) {
			if (!visit[i]) {
				if (m[s][i] != -1) {
					if (m[s][i] < d[i]) {
						d[i] = m[s][i];
						q.push(P(d[i], i));
					}
				}
				}
			}
		}
	cout << res << endl;
	delete[] d, visit;
	return 0;