Go: package main

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

const (
	white int = iota
	gray
	black
)
const INFTY int = 2001

var G [][]int
var color []int
var d []int
var p []int
var N int

func prim() int {
	u := 0
	d[u] = 0
	for {
		minCost := INFTY
		for i:=0; i<N; i++ {
			if color[i] != black && d[i] < minCost {
				minCost = d[i]
				u = i
			}
		}

		if minCost == INFTY {
			break
		}

		color[u] = black

		for v:=0; v<N; v++ {
			if color[v] != black && G[u][v] != -1 {
				if G[u][v] < d[v] {
					d[v] = G[u][v]
					p[v] = u
					color[v] = gray
				}
			}
		}
	}
	// sum
	sum := 0
	for i:=0; i<N; i++ {
		if p[i] != -1 {
			sum += G[i][p[i]]
		}
	}
	return sum
}




func initVariable(N int) {
	G = make([][]int, N)
	for i:=0; i<N; i++ {
		G[i] = make([]int, N)
	}
	color = make([]int, N)
	d = make([]int, N)
	p = make([]int, N)
	for i:=0; i<N; i++ {
		d[i] = INFTY
		p[i] = -1
	}
}

func nextInt(scanner *bufio.Scanner) int {
	scanner.Scan()
	num, _ := strconv.Atoi(scanner.Text())
	return num
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	N = nextInt(scanner)
	initVariable(N)
	for i:=0; i<N; i++ {
		for j:=0; j<N; j++ {
			G[i][j] = nextInt(scanner)
		}
	}
	fmt.Printf("%d\n", prim())
}

C++: #include <iostream>
#include <queue>
#include <utility>
#include <vector>

#define MAX_NODE 10000
#define INF 1000000000

using namespace std;

struct Edge {
	int to, w;
};
typedef pair<int, int> P;

vector<Edge> adj[MAX_NODE];
int minW[MAX_NODE];
bool flag[MAX_NODE];

int prim(int n, int start) {
	priority_queue<P, vector<P>, greater<P>> que;
	fill(minW, minW + n, INF);
	fill(flag, flag + n, false);

	int sum = 0;
	minW[start] = 0;
	que.push({0, start});
	while (!que.empty()) {
		P p = que.top();
		que.pop();
		int from = p.second;

		if (flag[from])
			continue;

		flag[from] = true;
		sum += p.first;

		for (Edge& e : adj[from])
			if (e.w < minW[e.to]) {
				minW[e.to] = e.w;
				que.push({minW[e.to], e.to});
			}
	}

	return sum;
}

int main() {
	int n;

	cin >> n;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			int x;
			cin >> x;
			if (x != -1) {
				adj[i].push_back({j, x});
			}
		}
	}

	printf("%d\n", prim(n, 0));

	return 0;
}

C++: #include <bits/stdc++.h>
#define r(i,a,n) for(int i=a;i<n;i++)
using namespace std;
int main(){
	int n,p;
	while(cin>>n>>p,n){
		int q,a[10001][1262],ch[10001][1262],b,c,d,e;
		r(i,0,p)r(j,0,1261)ch[i][j]=a[i][j]=0;
		cin>>q;
		r(i,0,q){
			cin>>b>>c>>d>>e;
			if(e==1){a[d-1][b]++;ch[d-1][b]=c;}
			else{
				for(int j=b-1;j>=0;j--){
					if(ch[d-1][j]==c)break;
					a[d-1][j]++;
				}
			}
		}
		cin>>q;
		while(q--){
			int s=0;
			cin>>b>>c>>d;
			r(i,b,c)if(a[d-1][i])s++;
			cout<<s<<endl;
		}
	}return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>

using namespace std;
struct edge {
    edge() {};
    edge(int s, int e, int d) : start {s}, end {e}, d {d} {};
    int start;
    int end;
    int d;
};

void dfs(vector<vector<int>>& graph, bool* used, int id, int n){
    used[id] = true;
    for(int i = 0; i < n; ++i){
        if(graph[id][i] == -1 || used[i]){
            continue;
        }
        dfs(graph, used, i, n);
    }
}

bool is_connected(vector<vector<int>>& graph){
    int n = graph.size();
    bool used[n];
    for(int i = 0; i < n; ++i){
        used[i] = false;
    }
    dfs(graph, used, 0, n);
    for(int i = 0; i < n; ++i){
        if(!used[i]){
            return false;
        }
    }
    return true;
}

void delete_edges(vector<vector<int>>& graph){
    int edges_num = 0;
    vector<edge> vec {};
    int n = graph.size();
    for(int i = 0; i < n; ++i){
        for(int j = i+1; j < n; ++j){
            if(graph[i][j] != -1){
                ++edges_num;
                vec.push_back(edge(i, j, graph[i][j]));
            }
        }
    }
    sort(vec.begin(), vec.end(), [](edge x, edge y){
            return y.d < x.d;
         });

    for(auto it = vec.begin(); it != vec.end(); ++it){
        edge e = *it;
        graph[e.start][e.end] = -1;
        graph[e.end][e.start] = -1;
        if(is_connected(graph)){
            --edges_num;
            if(edges_num == n-1){
                return;
            }
        } else {
            graph[e.start][e.end] = e.d;
            graph[e.end][e.start] = e.d;
        }
    }
}

vector<vector<int>> gen_spanning_tree(vector<vector<int>>& graph){
    vector<vector<int>> tree {graph};
    delete_edges(tree);
    return tree;
}

int calc_weight(vector<vector<int>> graph){
    int sum = 0;
    int n = graph.size();
    for(int i = 0; i < n; ++i){
        for(int j = i+1; j < n; ++j){
            if(graph[i][j] == -1){
                continue;
            }
            sum += graph[i][j];
        }
    }
    return sum;
}

int main(){
    int n;
    cin >> n;
    vector<vector<int>> graph {n, vector<int>(n, 0)};
    for(int i = 0; i < n; ++i){
        for(int j = 0; j < n; ++j){
            cin >> graph[i][j];
        }
    }
    auto tree = gen_spanning_tree(graph);

    cout << calc_weight(tree) << endl;
}

C++: #include <tuple>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

vector<int> union_tree;

int root(int leave)
{
	int tmp = leave;
	if (leave == union_tree[leave])
	{
		return leave;
	}
	union_tree[tmp] = root(union_tree[tmp]);
	return union_tree[tmp];
}

int is_same_set(int a, int b)
{
	return root(a) == root(b) ? 1 : 0;
}

void unite(int a, int b)
{
	union_tree[root(a)] = root(b);
}

int main()
{
	int n;
	cin >> n;
	vector<tuple<int, int, int>> bridges;
	for (int i = 0; i < n; i++)
	{

		for (int j = 0; j < n; j++)
		{
			int cost;
			cin >> cost;
			if (cost == -1)
			{
				continue;
			}
			else
			{
				bridges.push_back(make_tuple(cost, i, j));
			}
		}
		union_tree.push_back(i);
	}
	sort(bridges.begin(), bridges.end());
	int index(0),cost(0);
	for (int i = 0; i < n - 1; i++)
	{
		while (is_same_set(get<1>(bridges[index]), get<2>(bridges[index])))
		{
			index++;
		}
		cost += get<0>(bridges[index]);
		unite(get<1>(bridges[index]), get<2>(bridges[index]));
	}
	cout << cost << 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;
		q.pop();
		if (visit[s]) continue;
		visit[s] = true;
		res += f;
		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;
}