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;

class Bridge {
public:
	int u, v, cost;
};

Bridge MapData[500];
int Point[100];
int Height[100];


int find(int root) {

	while (root != Point[root]) {
		root = Point[root];
	}

	return root;
}

bool judge(int x, int y) {
	x = find(x);
	y = find(y);
	if (x == y) {
		return false;
	}

	if (Height[x] < Height[y]) {
		Point[x] = y;
	}
	else {
		Point[y] = x;
		if (Height[x] == Height[y]) {
			Height[x]++;
		}	
	}

	return true;
}

bool cmp(Bridge &b1, Bridge &b2) {
	return b1.cost < b2.cost;
}

int solve(int n, int m) {
	int cnt, sum;

	sort(MapData, MapData + m, cmp);
	for (int i = 0; i < n; i++) {
		Point[i] = i;
		Height[i] = 1;
	}

	cnt = sum = 0;
	for (int i = 0; i < m; i++) {
		if (cnt == n - 1) {
			return sum;
		}

		if (judge(MapData[i].u, MapData[i].v)) {
			cnt++;
			sum += MapData[i].cost;
		}
	}

	return sum;
}

int main() {
	int n, m;

	while (1) {
		cin >> n >> m;
		if (n == 0 && m == 0) {
			break;
		}

		for (int i = 0; i < m; i++) {
			cin >> MapData[i].u >> MapData[i].v >> MapData[i].cost;
		}

		cout << solve(n, m) << endl;
	}
	

	return 0;
}

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

typedef pair<pair<int,int>,int> ppi;

struct Edge { int to, cost; };
vector<Edge> edges[100];

int main() {
    int n, m;
    while (cin >> n >> m, n) {
        fill(edges, edges+100, vector<Edge>());
        int a, b, cost;
        for (int i=0; i<m; ++i) {
            cin >> a >> b >> cost;
            edges[a].push_back((Edge){b, cost});
        }
        int res = 0;
        bool used[100] = {};
        priority_queue<ppi, vector<ppi>, greater<ppi> > q;
        q.push(make_pair(make_pair(0,0),0));
        while ( ! q.empty()) {
            int d = q.top().first.first;
            int now = q.top().first.second;
            int c = q.top().second;
            q.pop();
            if (used[now]) {
                continue;
            } else {
                used[now] = true;
                res += c;
                for (int i=0; i<edges[now].size(); ++i) {
                    Edge &e = edges[now][i];
                    q.push(make_pair(make_pair(d + e.cost, e.to), e.cost));
                }
            }
        }
        cout << res << endl;
    }
    return 0;
}

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

#define REP(i,k,n) for(int i=k;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)

using namespace std;

struct edge
{
	int from;
	int to;
	int cost;

	edge (int from, int to, int cost) : from(from),to(to),cost(cost) {}
};

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

int par[101];
int rank[101];

void init(int n)
{
	rep(i,n)
	{
		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);
}

int main()
{
	int n,m;
	while(cin >> n >> m)
	{
		if(n == 0 && m == 0) break;
			
		
		vector<edge> v;

		rep(i,m)
		{
			int t1,t2,temp;
			cin >> t1 >> t2 >> temp;

			v.push_back(edge(t1,t2,temp));
		}

		sort(v.begin(),v.end(),cmp);

		init(n);
		int res = 0;
		

		rep(i,m)
		{
			edge e = v[i];

			if(!same(e.from,e.to))
			{
				unite(e.from,e.to);
				res += e.cost;
			}
		}

		cout  << res << endl;
	}
	return 0;
}

C++: #include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<cctype>
#include<complex>
#include<iostream>
#include<sstream>
#include<algorithm>
#include<functional>
#include<vector>
#include<string>
#include<stack>
#include<queue>
#include<map>

using namespace std;
const int dx[] = {1,0,-1,0},dy[] = {0,1,0,-1};
#define INF 1e+8
#define rep(i,j) for(int i = 0; i < (j); i++)
#define reps(i,j,k) for(int i = j; i < k; i++)
typedef long long ll;
typedef unsigned long long ull;
int par[128];
int rank[128];
struct edge{
	int from,to,cst;
};
bool cmp(const edge& left, const edge& right){
	return left.cst < right.cst;
}
void init(int n){
	rep(i,n){
		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);
}
edge Data[128];
int main(){
	int n,m;
	while(1){
		scanf("%d%d",&n,&m);
		if(n == m && n == 0)break;
		memset(Data,0,sizeof(Data));
		rep(i,m){
			scanf("%d%d%d",&Data[i].from,&Data[i].to,&Data[i].cst);
		}
		sort(Data,Data+m,cmp);
		init(n);
		int ans = 0;
		rep(i,m){
			edge next = Data[i];
			if(!same(next.from, next.to)){
				unite(next.from,next.to);
				ans += next.cst;
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}