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<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<math.h>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<string>
#include<cmath>
#include<set>
#include<stack>

using namespace std;
typedef unsigned long long ll;

int main(){
    int n,m;
    while(cin>>n>>m,n){
        int city[102][102];
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                city[i][j]=INT_MAX;
            }
            city[i][i]=0;
        }
        for(int i=0;i<m;i++){
            int a,b,c;
            cin>>a>>b>>c;
            city[a][b]=c;
            city[b][a]=c;
        }
        vector<int>que;
        vector<int>vec;
        vec.push_back(0);
        for(int i=1;i<n;i++){
            que.push_back(i);
        }
        int ret=0;
        while(!que.empty()){
            int mi=INT_MAX;
            int cur=-1,in;
            for(int j=0;j<que.size();j++){
                for(int i=0;i<vec.size();i++){
                    if(city[vec[i]][que[j]]<mi){
                        mi=city[vec[i]][que[j]];
                        cur=que[j];
                        in=j;
                    }
                }
            }
            ret+=mi;
            que.erase(que.begin()+in);
            vec.push_back(cur);
        }
        cout<<ret<<endl;
    }
    
    return 0;
}

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

#define mp make_pair
#define pb push_back
#define REP(i,a,n) for(int i = a;i < (n);i++)
#define rep(i,n) for(int i = 0;i < (n);i++)
#define all(s) s.begin(), s.end()
#define rall(s) s.rbegin(), s.rend()
#define range(x,min,max) ((min) <= (x) && (x) <= (max))

using namespace std;

typedef long long LL;
typedef vector<int> VI;
typedef vector<vector<int> > VII;
typedef pair<int,int> PII;

const int DX[]={1,0,-1,0},DY[]={0,-1,0,1};

struct Edge{ int from, to, cost; };

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

vector<Edge> es;
VI parent;
VI rank;

void init(int n){
	rep(i,n){
		parent.pb(i);
		rank.pb(0);
	}
}

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

bool unite(int x, int y){
	x = find(x);
	y = find(y);

	if(x == y)
		return false;

	if(rank[x] < rank[y]){
		parent[x] = y;
	}else{
		parent[y] = x;
		if(rank[x] == rank[y]) rank[x]++;
	}
	return true;
}	

int main(){
	cin.tie(0);
   	ios::sync_with_stdio(false);

	while(1){
		int n, m;
		int ans = 0;

		cin >> n >> m;

		if(n == 0 && m == 0) break;

		rep(i,m){
			Edge e;
			cin >> e.from >> e.to >> e.cost;
			es.pb(e);
		}

		init(n);
		sort(all(es), comp);
		
		rep(i,es.size()){
			Edge e = es[i];
			if(!unite(e.from, e.to)){
				continue;
			}
			ans += e.cost;
		}

		cout << ans << endl;

		parent.clear();
		rank.clear();
		es.clear();
	}
	return 0;
}

C++: #include<cstdio>
#include<algorithm>
#include<iostream>
#include<vector>
#include<set>
#include<queue>

using namespace std;

#define rep(i,n) for(int i=0;i<int(n);i++)
#define reps(i,n) for(int i=1;i<=int(n);i++)

#define f first
#define s second

class S{
	public:
	int t;
	int v;
	S(int t,int v):t(t),v(v){}
	bool operator<(const S&a)const{
		return v>a.v;
	}
};

int main(){
	while(1){
		int n,m;
		cin>>n>>m;
		if(n==0)break;
		
		vector<pair<int,int> > load[111];
		
		rep(i,m){
			int a,b,c;
			cin>>a>>b>>c;
			
			load[a].push_back(make_pair(b,c));
			load[b].push_back(make_pair(a,c));
		}
		
		int visit[111]={0};
		
		priority_queue<S> que;
		
		
		visit[0]=1;
		rep(i,load[0].size()){
			pair<int,int> e = load[0][i];
			que.push(S(e.f, e.s));
		}
		
		int ans=0;
		
		while(1){
			if(que.empty())break;
			
			S u = que.top();
			que.pop();
			
			if(visit[u.t]==1)continue;
			visit[u.t]=1;
			
			ans+=u.v;
			
			
			rep(i,load[u.t].size()){
				pair<int,int> e=load[u.t][i];
				que.push(S(e.f,e.s));
			}
		}
		
		printf("%d\n",ans);
	}
}

C++: #include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <list>
#include <iostream>
#include <sstream>
#include <climits>
#include <cfloat>
#include <complex>

using namespace std;

typedef pair<int, int> P;
int main()
{
	int n, m;
	while (scanf("%d %d", &n, &m), n)
	{
		vector<P> edges[100];
		for (int i = 0; i < m; ++i)
		{
			int a, b, c;
			scanf("%d %d %d", &a, &b, &c);
			edges[a].push_back(P(c, b));
			edges[b].push_back(P(c, a));
		}
		bool inTree[100];
		fill(inTree, inTree+n, false);
		priority_queue<P, vector<P>, greater<P> > q;
		inTree[0] = true;
		for (int i = 0; i < edges[0].size(); ++i)
			q.push(edges[0][i]);
		int ans = 0;
		while (!q.empty())
		{
			P e = q.top();
			q.pop();
			if (inTree[e.second])
				continue;

			inTree[e.second] = true;
			ans += e.first;
			for (int i = 0; i < edges[e.second].size(); ++i)
				q.push(edges[e.second][i]);
		}
		printf("%d\n", ans);
	}

	return 0;
}