Go: package main

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

// I/O
type Scanner struct {
	sc *bufio.Scanner
}

func NewScanner() *Scanner {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 1024), int(1e+9))
	return &Scanner{sc}
}

func (s *Scanner) nextStr() string {
	s.sc.Scan()
	return s.sc.Text()
}

func (s *Scanner) nextInt() int {
	i, e := strconv.Atoi(s.nextStr())
	if e != nil {
		panic(e)
	}
	return i
}

func (s *Scanner) nextFloat() float64 {
	f, e := strconv.ParseFloat(s.nextStr(), 64)
	if e != nil {
		panic(e)
	}
	return f
}

func (s *Scanner) nextRuneSlice() []rune {
	return []rune(s.nextStr())
}

func (s *Scanner) nextIntSlice(n int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextInt()
	}
	return res
}

func (s *Scanner) nextFloatSlice(n int) []float64 {
	res := make([]float64, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextFloat()
	}
	return res
}

// Arithmetic
func max(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m < i {
			m = i
		}
	}
	return m
}

func min(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m > i {
			m = i
		}
	}
	return m
}

func abs(x int) int {
	if x > 0 {
		return x
	}
	return -x
}

func pow(x, y int) int {
	res := 1
	for i := 0; i < y; i++ {
		res *= x
	}
	return res
}

func ceil(a, b int) int {
	if a%b == 0 {
		return a / b
	} else {
		return a/b + 1
	}
}

// Sort
type RuneSlice []rune

func (a RuneSlice) Len() int           { return len(a) }
func (a RuneSlice) Less(i, j int) bool { return a[i] < a[j] }
func (a RuneSlice) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

const INF = 1 << 60

// Main
func main() {
	sc := NewScanner()
	wtr := bufio.NewWriter(os.Stdout)
	N := sc.nextInt()
	G := make([][]int, N)
	for i := 0; i < N; i++ {
		G[i] = make([]int, N)
		for j := 0; j < N; j++ {
			G[i][j] = sc.nextInt()
		}
	}
	d := make([]int, N)
	for i := 0; i < N; i++ {
		d[i] = INF
	}
	seen := make([]bool, N)
	d[0] = 0
	ans := 0
	for {
		cur := 0
		minCost := INF
		for i := 0; i < N; i++ {
			if !seen[i] && d[i] < minCost {
				minCost = d[i]
				cur = i
			}
		}
		if minCost == INF {
			break
		}
		seen[cur] = true
		ans += d[cur]
		for i := 0; i < N; i++ {
			cost := G[cur][i]
			if cost != -1 && cost < d[i] {
				d[i] = cost
			}
		}
	}

	fmt.Fprintln(wtr, ans)
	wtr.Flush()
}

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;