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<string>
#include<algorithm>
#include<cmath>
#include<queue>
typedef long long int ll;
#define FOR(i,a,n) for(int i=a;i<n;++i)
#define TFOR(i,n)FOR(i,0,n)
#define ALL(x) x.begin(),x.end();
const int INF = 1LL << 30;
const ll LLINF = 1LL << 60;
int V;//??????
int E;//???
struct edge { int from, to, cost; };
typedef std::pair<int, int> PP;
std::vector<edge> e;
int Par[100000];
int Rank[100000];
void init(int n) {
	for (int i = 0; i < n; i++) {
		Par[i] = i;
		Rank[i] = 0;
	}
}
int find(int x) {
	if (Par[x] == x) {
		return x;
	}
	else {
		return Par[x] = find(Par[x]);
	}
}
void unite(int  x, int y) {
	x = find(x);
	y = find(y);
	if (x == y) return;
	if (Rank[x] < Rank[y]) {
		Par[x] = y;
	}
	else {
		Par[y] = x;
		if (Rank[x] == Rank[y])Rank[x]++;
	}
}
bool same(int x, int y) {
	return find(x) == find(y);
}
// ??????????????????????????????????????¢??° 
bool comp(const edge& e1, const edge& e2) {
	return e1.cost < e2.cost;
}
int kruskal() {
	init(V);
	sort(e.begin(), e.end(), comp);
	int ans = 0;
	for (int i = 0; i<E; i++) {
		if (!same(e[i].from, e[i].to)) {
			unite(e[i].from, e[i].to);
			ans += e[i].cost;
		}
	}
	return ans;
}
int main() {
	while (std::cin >> V>>E&&V) {
		int a, b, c;
		for (int i = 0; i < E; i++) {
			std::cin >> a >> b >> c;
			e.push_back(edge{ a,b,c });
		}
		std::cout << kruskal()<< std::endl;
		e = {};
	}
	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]));
}
void unite(int a,int b) {
	P[root(a)] = root(b);
}

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) {
			int br2 = bridge[i].second;
			if(P[root(b[br2])] != P[root(a[br2])]) {
				unite(a[br2],b[br2]);
				sum += bridge[i].first;
			}
		}
		cout << sum << endl;
		cin >> n >> m;
		sum =0;
	}
}

C++: #include<bits/stdc++.h>

#define MAX_E 501
#define MAX_V 101
#define INF 1e9

using namespace std;

int cost[MAX_V][MAX_V]; // cost[u][v]テ」ツ?ッティツセツコe=(u,v)テ」ツ?ョテ」ツつウテ」ツつケテ」ツδ?テ・ツュツ佚・ツ慊ィテ」ツ?療」ツ?ェテ」ツ??・ツ?エテ・ツ青暗」ツ?ッINF)
int mincost[MAX_V]; // テゥツ崢?・ツ青?テ」ツ?凝」ツつ嘉」ツ?ョティツセツコテ」ツ?ョテヲツ慊?・ツーツ湘」ツつウテ」ツつケテ」ツδ?
bool used[MAX_V];   // テゥツ?づァツつケiテ」ツ?傾テ」ツ?ォテ・ツ青ォテ」ツ?セテ」ツつ古」ツ?ヲテ」ツ??」ツつ凝」ツ??
int V;

int prim() {
  for (int i = 0; i < V; ++i) {
    mincost[i] = INF;
    used[i] = false;
  }
  mincost[0] = 0;
  int res = 0;

  while (true) {
    int v = -1;
    // Xテ」ツ?ォテ・ツアツ榲」ツ?陛」ツ?ェテ」ツ??ゥツ?づァツつケテ」ツ?ョテ」ツ??」ツ?。Xテ」ツ?凝」ツつ嘉」ツ?ョティツセツコテ」ツ?ョテ」ツつウテ」ツつケテ」ツδ暗」ツ?古ヲツ慊?・ツーツ湘」ツ?ォテ」ツ?ェテ」ツつ凝ゥツ?づァツつケテ」ツつ津ヲツ篠「テ」ツ??
    for (int u = 0; u < V; u++) {
      if (!used[u] && (v == -1 || mincost[u] < mincost[v]))
        v = u;
    }

    if (v == -1)
      break;
    used[v] = true;    // テゥツ?づァツつケvテ」ツつ湛テ」ツ?ォティツソツステ・ツ環?
    res += mincost[v]; // ティツセツコテ」ツ?ョテ」ツつウテ」ツつケテ」ツδ暗」ツつ津・ツ環?」ツ?暗」ツつ?

    for (int u = 0; u < V; u++) {
      mincost[u] = min(mincost[u], cost[v][u]);
    }
  }

  return res;
}

int main(){
    int n, m;
    while(cin >> n >> m, n != 0 || m != 0){
        V = n;
        for(int i=0;i<MAX_V;++i)
            for(int j=0;j<MAX_V;++j)
                cost[i][j] = INF;
        for(int i=0;i<m;++i){
            int a, b, c;
            cin >> a >> b >> c;
            cost[a][b] = c;
            cost[b][a] = c;
        }
        cout << prim() << endl;
    }

    return 0;
}

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

const int N = 10000;
int par[N]; int hukasa[N];

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);
	if (x == y) return;
	if (hukasa[x] < hukasa[y]) par[x] = y;
	else
	{
		par[y] = x;
		if (hukasa[x] == hukasa[y]) hukasa[x]++;
	}
}

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

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

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