Go: // Find the minimum spanning tree by the Kruskal algorithm
// -------------------------------------------------------
// Kruskal(G):
//     A = ∅
//     foreach v ∈ G.V:
//        MAKE-SET(v)
//     foreach (u, v) in G.E ordered by weight(u, v), increasing:
//         if FIND-SET(u) ≠ FIND-SET(v):
//             A = A ∪ {(u, v)}
//             UNION(u, v)
//     return A
//
// Ref: https://en.wikipedia.org/wiki/Kruskal%27s_algorithm

package main

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

var scanner = bufio.NewScanner(os.Stdin)
var (
	matrix [][]int
)

type Edge struct {
	a, b, cost, added int
}
type ByCost []Edge

var edges ByCost

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

func main() {
	for scanner.Scan() {
		buf := strings.Split(scanner.Text(), " ")
		n, _ := strconv.Atoi(buf[0])
		m, _ := strconv.Atoi(buf[1])
		if n == 0 && m == 0 {
			break
		}
		matrix = make([][]int, n)
		for i := range matrix {
			matrix[i] = make([]int, n)
		}
		for i := 0; i < m; i++ {
			scanner.Scan()
			buf_ := strings.Split(scanner.Text(), " ")
			a, _ := strconv.Atoi(buf_[0])
			b, _ := strconv.Atoi(buf_[1])
			cost, _ := strconv.Atoi(buf_[2])
			matrix[a][b] = cost
			matrix[b][a] = cost
		}
		sum := Kruskal(n)
		fmt.Println(sum)
		if len(edges) > 0 {
			edges = []Edge{}
		}
	}
}

func Kruskal_func(size, edge_num int) int {
	for i := 0; i < size; i++ {
		for j := 0; j < size; j++ {
			if matrix[i][j] > 0 {
				tmp := Edge{i, j, matrix[i][j], 0}
				edges = append(edges, tmp)
			}
		}
	}
	sort.Sort(ByCost(edges))
	UF_Init(edge_num)
	for i := 0; i < edge_num; i++ {
		a := edges[i].a
		b := edges[i].b
		if UF_Find(a) != UF_Find(b) {
			edges[i].added = 1
			UF_Union(UF_Find(a), UF_Find(b))
		}
	}
	sum := 0
	for i := 0; i < edge_num; i++ {
		if edges[i].added != 0 {
			// log.Printf("edge(%d,%d) is added\n", edges[i].a, edges[i].b)
			sum += edges[i].cost
		}
	}
	return sum
}

func Kruskal(size int) int {
	edge_num := 0
	for i := 0; i < size; i++ {
		for j := 0; j < size; j++ {
			if matrix[i][j] > 0 {
				edge_num++
			}
		}
	}
	return Kruskal_func(size, edge_num)
}

/*** Union-Find functions ***/
var UF_Parent []int
var UF_Rank []int

func UF_Init(n int) {
	UF_Parent = make([]int, n)
	UF_Rank = make([]int, n)
	for i := 0; i < n; i++ {
		UF_Parent[i] = i
		UF_Rank[i] = 0
	}
}
func UF_Union(a, b int) {
	if UF_Rank[a] < UF_Rank[b] {
		UF_Parent[a] = b
	} else {
		UF_Parent[b] = a
	}
	if UF_Rank[a] == UF_Rank[b] {
		UF_Rank[a]++
	}
}
func UF_Find(a int) int {
	if UF_Parent[a] == a {
		return a
	}
	return UF_Find(UF_Parent[a])
}
/*** Union-Find functions ***/

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

class union_find {
public:
    union_find(int n)
        : par_(n, -1)
    {}
    void init(int n) {
        par_.assign(n, -1);
    }

    int root(int x) {
        return par_[x] < 0 ? x : par_[x] = root(par_[x]);
    }

    bool unite(int x, int y) {
        x = root(x); y = root(y);
        if(x == y) {
            return false;
        } else {
            if(par_[x] < par_[y]) {
                par_[x] += par_[y];
                par_[y] = x;
            } else {
                par_[y] += par_[x];
                par_[x] = y;
            }
            return true;
        }
    }

    bool same(int x, int y) {
        return root(x) == root(y);
    }

    int size(int x) {
        return -par_[root(x)];
    }

private:
    std::vector<int> par_;
};

int main()
{
  typedef std::pair<int,std::pair<int,int> > bridge;
  int n,m;
  while (true) {
    std::cin >> n >> m;
    if (n==0 && m==0) {
      break;
    }
    int totalcost = 0;
    std::vector<bridge> bridges(m);
    for (int i = 0; i < m; i++) {
      int a,b,cost;
      std::cin >> a >> b >> cost;
      bridges[i] = bridge(cost,std::make_pair(a,b));
    }
    std::sort(bridges.begin(),bridges.end());
    union_find reachable(n);
    for (int i = 0; i < m; i++) {
      if (!(reachable.same(bridges[i].second.first,bridges[i].second.second))) {
	totalcost += bridges[i].first;
	reachable.unite(bridges[i].second.first,bridges[i].second.second);
      }
    }
    std::cout << totalcost << std::endl;
  }
}

C++: #include <stdio.h>
#include <iostream>
#include <string>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <vector>

using namespace std;

#define MAX_N 10010

int P[10010];
int A[1001];
int B[1001];
int COST[1001];
int N;
int M;
pair<int,int> bridge[1001];

void init(int N) { // 初期化 はじめは全ての頂点はバラバラ
	for (int i=0; i<N; ++i) P[i] = i;
}

int root(int a) { // a の root(代表元) を求める
	if (P[a] == a) return a; // aはroot
	return (P[a] = root(P[a])); // a の親の root を求め,a の親とする
	
}

bool is_same_set(int a, int b) { // a と b が同じグループに属するか?
	return root(a) == root(b);
}

void unite(int a, int b) { // a と b を同一グループにまとめる
	P[root(a)] = root(b);
}

int main()
{
	while(cin >> N >> M && N){
		init(1001);
		for(int i=0; i<M; ++i)
		{
			cin >> A[i] >> B[i] >> COST[i];
			bridge[i].first = COST[i];
			bridge[i].second = i;
		}
		
		sort(bridge, bridge+M);
		for(int i=0; i<M; ++i)
		{
			int cost = bridge[i].first;
			int a = A[bridge[i].second];
			int b = B[bridge[i].second];
			//cout << a << "から" << b << "に" << cost << "コスト橋が架かっている" << endl;
		}
		
		int total = 0;
		for(int i=0; i<M; ++i)
		{
			int a = A[bridge[i].second];
			int b = B[bridge[i].second];
			if(is_same_set(a,b)) continue;
			unite(a,b);
			total += bridge[i].first;
		}
		cout << total << endl;
	}

}

C++: #include <iostream>
#define rep(i,n) for(int i=0;i<n;i++)
//#define int long long
using namespace std;

#define INF 999999

int n, m;
int a, b, c;

int cost[100][100];
int mincost[100];
bool used[100];

void init()
{
	rep(i,100) rep(j,100)
	{
		cost[i][j] = INF;
	}
}

int prim()
{
	rep(i,n)
	{
		mincost[i] = INF;
		used[i] = false;
	}

	mincost[0] = 0;
	int res = 0;

	while (true)
	{
		int v = -1;
		rep(u,n)
		{
			if (!used[u] && (v == -1 || mincost[u] < mincost[v])) v = u;
		}

		if (v == -1) break;
		used[v] = true;
		res += mincost[v];

		rep(u,n)
		{
			mincost[u] = min(mincost[u], cost[v][u]);
		}
	}

	return res;
}

signed main()
{
	while (true)
	{
		cin >> n >> m;
		if (n == 0 && m == 0) break;

		init();

		rep(i,m)
		{
			cin >> a >> b >> c;
			cost[a][b] = c;
			cost[b][a] = c;
		}

		printf("%d?\n", prim());
	}
}

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

using namespace std;

int data[101][101];

int prim( int n )
{
	vector<int> key( n, numeric_limits<int>::max() );
	key[0] = 0;
	vector<int> pq;
	vector<bool> inQueue( n, true );
	for( int i = 0;i < n;i++ ){
		pq.push_back( key[i] );
	}

	while( !pq.empty() ){
		vector<int>::iterator it = min_element( pq.begin(), pq.end() );
		int u = distance( pq.begin(), it );
		pq.erase( it );
		inQueue[u] = false;

		for( int i = 0;i < n;i++ ){
			if( data[u][i] ){
				if( inQueue[i] ){
					int w = data[u][i];
					if( w < key[i] ){
						key[i] = w;
						pq[i] = w;
					}
				}
			}
		}
	}

	int answer = 0;
	for( int i = 0;i < n;i++ ){
		answer += key[i];
	}

	return answer;
}

int main(int argc, char const* argv[])
{
	int n,m;
	int a,b,cost;

	while( cin >> n >> m && n != 0 ){
		for( int i = 0;i < 101;i++ ){
			for( int j = 0;j < 101;j++ ){
				data[i][j] = 0;
			}
		}
		for( int i = 0;i < m;i++ ){
			cin >> a >> b >> cost;
			data[a][b] = cost;
			data[b][a] = cost;
		}

		int answer = prim( n );
		cout << answer << endl;
	}

	return 0;
}