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 <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) (a).begin(),(a).end()
#define pb(x) push_back(x)
#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; };

int par[10000]; int hukasa[10000];

int find(int x)
{
	if (par[x] == x) return x;
	else par[x] = find(par[x]);
}

void unite(int x, int y)
{
	x = find(x); y = find(y);
	par[x] = y;
}

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

void init_UFT(int n)
{
	REP(i, n)
	{
		par[i] = i;
	}
}

bool comp(const edge& e1, const edge& e2)
{
	return e1.cost < e2.cost;
}

vector<edge> es;
int main()
{
	int n, m;
	while (cin >> n >> m, n)
	{
		init_UFT(n);
		es.clear();
		int a, b, c;
		REP(i, m)
		{
			cin >> a >> b >> c;
			edge e = { a, b, c };
			es.push_back(e);
		}
		sort(ALL(es), comp);
		int sum = 0;
		REP(i, es.size())
		{
			if (!same(es[i].from, es[i].to))
			{
				unite(es[i].from, es[i].to);
				sum += es[i].cost;
			}
		}
		cout << sum << endl;
	}

	return 0;
}

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

int n,m,sum,r;
int P[110],a[10010],b[10010],cost[10010];
pair<int,int> bridge[10010];
void init(int N) {
	for(int i=0; i<N; ++i) P[i] = i;
}
int root(int a) {
	if(P[a] == a) return a;
	return (P[a] = root(P[a]));
}

int main() {
	cin >> n >> m;
	while(n!=0 && m!=0) {
		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);
		init(n);
		for(int i=0; i<m; ++i) {
			if(P[b[bridge[i].second]] != P[a[bridge[i].second]]) {
				if(P[a[bridge[i].second]] == a[bridge[i].second]) {
					P[a[bridge[i].second]] = P[b[bridge[i].second]];
					r = a[bridge[i].second];
				} else {
					P[b[bridge[i].second]] = P[a[bridge[i].second]];
					r = b[bridge[i].second];
				}
				for(int k=0; k<n; ++k) {
					if(P[k] == r) {
						root(k);
					}
				}
				sum += bridge[i].first;
				//}
			}
		}
		cout << sum << endl;
		cin >> n >> m;
		sum =0;
	}
}