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 <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <cassert>
#include <iostream>
#include <cctype>
#include <sstream>
#include <string>
#include <list>
#include <vector>
#include <queue>
#include <set>
#include <stack>
#include <map>
#include <utility>
#include <numeric>
#include <algorithm>
#include <iterator>
#include <bitset>
#include <complex>
#include <fstream>
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for(int i = x; i < n; i++)
template<class T> T RoundOff(T a){ return int(a+.5-(a<0)); }
template<class T, class C> void chmax(T& a, C b){ if(a < b) a = b; }
template<class T, class C> void chmin(T& a, C b){ if(b < a) a = b; }
template<class T, class C> pair<T, C> mp(T a, C b){ return make_pair(a, b); }

struct UnionFind
{
	vint data;
	
	UnionFind(int size) : data(size, -1) { }
	
	int unite(int x, int y)
	{
		x = root(x), y = root(y);
		
		if(x != y)
		{
			if(data[y] < data[x]) swap(x, y);
			data[x] += data[y]; data[y] = x;
		}
		
		return x != y;
	}
	
	int root(int x)
	{	return data[x] < 0 ? x : root(data[x]);	}
	
	int size(int x)
	{	return -data[root(x)];	}
	
	bool same(int x, int y)
	{	return root(x) == root(y);	}
};

struct edge{ int u, v, cost;	};

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

int main()
{
    int n, m;
	while(cin >> n >> m && n)
	{
		vector<edge> es(m);
		UnionFind uft(n);
		
		rep(i, m) cin >> es[i].u >> es[i].v >> es[i].cost;
		
		sort(ALL(es), comp);
		
		int res = 0;
		rep(i, m)
		{
			edge e(es[i]);
			
			if(!uft.same(e.u, e.v))
			{
				uft.unite(e.u, e.v);
				res += e.cost;
			}
		}
		
		cout << res << endl;
	}
}

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

int p[100];

int find(int x){
	return x == p[x] ? x : (p[x] = find(p[x]));
}
int merge(int x,int y){
	x=find(x),y=find(y);
	if( x == y ) return 0;
	p[y] = x;
	
	return 1;
}

int main(){
	int n,m;
	while(cin >> n >> m && n){
		for(int i = 0 ; i < n ; i++) p[i] = i;
		vector< pair<int,pair<int,int> > > e;
		for(int i = 0 ; i < m ; i++){
			int a,b,c;
			cin >> a >> b >> c;
			e.push_back(make_pair(c,make_pair(a,b)));
		}
		int ans = 0;
		sort(e.begin(),e.end());
		for(int i = 0 ; i < m ; i++){
			if( merge(e[i].second.first,e[i].second.second) ){
				ans += e[i].first;
			}
		}
		cout << ans << endl;
	}
}

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

const int INF = 100000;
struct Edge { int from, to, cost; };
int dist[100];

int main() {
    int n, m;
    while (cin >> n >> m, n) {
        vector<Edge> G;
        int a, b, cost;
        for (int i=0; i<m; ++i) {
            cin >> a >> b >> cost;
            G.push_back((Edge){ a, b, cost });
            // G.push_back((Edge){ b, a, cost });
        }
        int ret = 0;
        fill(dist, dist+100, INF);
        dist[0] = 0;
        while (1) {
            int k, mn = INF;
            for (int i=0; i<(int)G.size(); ++i) {
                if (dist[G[i].to] == INF && dist[G[i].from] + G[i].cost < mn) {
                    mn = dist[G[i].from] + G[i].cost;
                    k = i;
                }
            }
            if (mn == INF) break;
            dist[G[k].to] = dist[G[k].from] + G[k].cost;
            ret += G[k].cost;
        }
        cout << ret << endl;
    }
    return 0;
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
int n,m,a[10010],b[10010],cost[10010];
int parent[101];
pair<int,int> bridge[10010];
void init(int N){
	for(int i=0;i<N;i++)parent[i]=i;
}
int root(int a){
	if(parent[a]==a)return a;
	return (parent[a]=root(parent[a]));
}
bool is_same_set(int a,int b){
	return (root(a)==root(b));
}
void unite(int a,int b){
	parent[root(a)]=root(b);
}

int main(){
	while(cin>>n>>m&&n>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);
		int ans=0;
		for(int i=0;i<m;i++){
			if(is_same_set(a[bridge[i].second],b[bridge[i].second]))continue;
			unite(a[bridge[i].second],b[bridge[i].second]);
			ans+=bridge[i].first;
		}
		cout<<ans<<endl;
	}
	return 0;
}

C++: //kruskal tree
#include <iostream>
#include <algorithm>
using namespace std;
#define M 101
int parent[M],a[M],b[M];
pair<int,int>node[M];
int root(int a){
	if(parent[a]==a)return a;
	return parent[a]=root(parent[a]);
}
int unite(int a,int b){
	if(root(a)==root(b))return 0;
	parent[root(a)]=root(b);
	return 1;
}
int main(){
	int i,s,n,m;
	for(;cin>>n>>m,n;cout<<s<<endl){
		for(i=0;i<m;i++)cin>>a[i]>>b[i]>>node[i].first,node[i].second=i;
		sort(node,node+m);
		for(i=0;i<n;i++)parent[i]=i;
		for(s=i=0;i<m;i++)if(unite(a[node[i].second],b[node[i].second]))s+=node[i].first;
	}
}