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++: #define _USE_MATH_DEFINES
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <cfloat>
#include <ctime>
#include <cassert>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <numeric>
#include <list>
#include <iomanip>
#include <iterator>
 
using namespace std;
 
typedef long long ll;
typedef pair<int,int> Pii;
typedef pair<ll,ll> Pll;
 
#define FOR(i,n) for(int i = 0; i < (n); i++)
#define sz(c) ((int)c.size())

typedef unsigned long long ull;
const int INF = (int)1e8;

class UnionFind{
	int n;
	vector<int> par;
	vector<int> rank;
public:
	UnionFind(){ }
	UnionFind(int size){ reset(size); }

	void reset(int size){
		n = size;
		par.resize(size);
		rank.resize(size);
		for(int i = 0; i < size; i++) par[i] = i,rank[i] = 0;
	}

	int find(int x){
		if(par[x] == x) return x;
		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]++;
		}
		n--;
	}

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

	int size() {return n;}
};

int main(){
	int n,m;
	while(cin>>n>>m,n||m){
		UnionFind u(n);
		vector<pair<int,Pii> > v;
		FOR(i,m){
			int a,b,c; cin>>a>>b>>c;
			v.push_back(make_pair(c,Pii(a,b)));
		}
		int ans = 0;
		sort(v.begin(),v.end());
		FOR(i,m){
			if(u.size() == 1) break;
			int cost = v[i].first;
			Pii p = v[i].second;
			if(!u.same(p.first,p.second)){
				ans += cost;
				u.unite(p.first,p.second);
			}
		}
		cout << ans << endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;

using Weight = int;
struct Edge {
	int src, dst; Weight weight;
	Edge() {}
	Edge(int s, int d, Weight w) : src(s), dst(d), weight(w) {}
};
using Edges = vector<Edge>;

struct UnionFind {
	vector<int> par;
	int cnt;
	UnionFind(int size_) : par(size_, -1), cnt(size_) {}
	void unite(int x, int y) {
		if((x = find(x)) != (y = find(y))) {
			if(par[y] < par[x]) swap(x, y);
			par[x] += par[y]; par[y] = x; cnt--;
		}
	}
	bool same(int x, int y) { return find(x) == find(y); }
	int find(int x) { return par[x] < 0 ? x : par[x] = find(par[x]); }
	int size(int x) { return -par[find(x)]; }
	int size() { return cnt; }
};

bool operator < (const Edge &e, const Edge &f) { return e.weight < f.weight; }
pair<Weight, Edges> kruskal(int N, Edges es) {
	UnionFind uf(N);
	Weight total = 0; Edges T;
	for(auto &e : es) if(!uf.same(e.src, e.dst)) {
		T.push_back(e); total += e.weight; uf.unite(e.src, e.dst);
	}
	return make_pair(total, T);
}

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

	int n, m;
	while(cin >> n >> m, n + m) {
		Edges es;
		for(int i = 0; i < m; i++) {
			int a, b, c;
			cin >> a >> b >> c;
			es.push_back({ a, b, c });
		}
		sort(es.begin(), es.end());
		auto res = kruskal(n, es);
		cout << res.first << endl;
	}
}

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

struct UF {
  int *p;
  UF(int N):p(new int[N]) {fill(p,p+N,-1);}
  ~UF(){delete[] p;}
  int rt(int x){return p[x]<0?x:(p[x]=rt(p[x])); }
  bool cat(int x,int y) {
    if((x=rt(x))==(y=rt(y)))return false;
    if(p[x]<p[y])swap(x,y);
    p[y]+=p[x];
    p[x]=y;
    return true;
  }
};

int main() {
  for(int tci = 0; ; tci++) {
    int n,m; scanf("%d%d", &n, &m);
    if(n==0) break;
    pair<int,pair<int,int> > *edges = new pair<int,pair<int,int> >[m];
    for(int i = 0; i < m; i++) {
      scanf("%d%d%d", &edges[i].second.first, &edges[i].second.second, &edges[i].first);
    }
    sort(edges,edges+m);
    long long costsum = 0;
    UF uf(n);
    for(int i = 0; i < m; i++) {
      if(uf.cat(edges[i].second.first,edges[i].second.second)) {
        costsum += edges[i].first;
      }
    }
    printf("%lld\n", costsum);
    delete[] edges;
  }
  return 0;
}

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

using namespace std;

const int inf = 999999;

int prim(int a[101][101], int n)
{
	int r = 0, mcost[101];
	bool vis[101];
	
	fill(mcost, mcost+101, inf);
	fill(vis, vis+101, false);
	mcost[0] = 0;
	
	while(1){
		int v = -1;
		
		for(int i = 0; i < n; i++){
			if(!vis[i] && (v == -1 || mcost[v] > mcost[i])){
				v = i;
			}
		}
		
		if(v == -1){
			break;
		}
		vis[v] = true;
		r += mcost[v];
		
		for(int i = 0; i < n; i++){
			mcost[i] = min(mcost[i], a[v][i]);
		}
	}
	
	return r;
}

int main()
{
	int n, m, a[101][101], s, t, u;
	
	while(1){
		cin >> n >> m;
		if(n == 0 && m == 0){
			break;
		}
		
		fill(&a[0][0], &a[100][100], inf);
		for(int i = 0; i <= n; i++){
			a[i][i] = 0;
		}
		
		while(m--){
			cin >> s >> t >> u;
			a[s][t] = a[t][s] = u;
		}
		
		cout << prim(a, n) << endl;
	}
	
	return 0;
}