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 <set>
#include <algorithm>
#include <vector>

using namespace std;

int n, m;
vector< set< pair<int, int> > > cost(200,set< pair<int,int> >());

int prim()
{
	int res=0;
	set<int> v;
	v.insert(0);
	while(v.size()<n)
	{
		pair<int, int> mini(100000007, -1);
		for(set<int>::iterator it=v.begin();it!=v.end();it++)
		{
			for(set<  pair<int, int> >::iterator it2=cost[*it].begin();it2!=cost[*it].end();it2++)
				if(v.find((*it2).second)==v.end())
				{
					if(mini.first>(*it2).first)
						mini=(*it2);
					break;
				}
		}
		v.insert(mini.second);
		res+=mini.first;
	}
	return res;
}

int main()
{
	while(1)
	{
		cin >> n >> m;
		if(n==0&&m==0) break;
		for(int i=0;i<m;i++)
		{
			int a, b, c;
			cin >> a >> b >> c;
			cost[a].insert(pair<int, int>(c, b));
			cost[b].insert(pair<int, int>(c, a));
		}
		cout << prim() << endl;
		for(int i=0;i<m;i++)
			cost[i].clear();
	}
	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
const int MAX = 1000;

int n,m,par[MAX],rank[MAX];

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;
  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 kruskal(pair<int,pair<int,int> > es[MAX]){
  sort(es,es+m);
  init(n);
  int res = 0;
  for(int i = 0 ; i < m ; i++){
    int a = es[i].second.first;
    int b = es[i].second.second;
    if(!same(a,b)){
      unite(a,b);
      res += es[i].first;
    }
  }
  return res;
}

int main(){
  while(cin >> n >> m ,n +m){
    pair<int,pair<int,int> > es[MAX];
    for(int i = 0 ; i < m ; i++){
      cin >> es[i].second.first >> es[i].second.second >> es[i].first;
    }
    cout << kruskal(es) << endl;
  }

  return 0;
}

C++: #include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <algorithm>
#include <utility>
#include <functional>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>
#include <fstream>
using namespace std;
inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v;}
template<class T> inline string toStr(T x) { ostringstream sout; sout << x; return sout.str();}
typedef vector<int> vi;
typedef vector<vi>  vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define REP(i,n) FOR(i,0,(n)-1)
const double EPS = 1e-12;
const double PI = acos(-1.0);
const int INF = INT_MAX/10;

struct state {
	int p, c;
	state(int p, int c) : p(p), c(c) {};
	bool operator<(const state &o)const {
		return o.c<c;
	}
};

int main() {
	int n, m;
	while(cin >> n >> m, n) {
		int a, b, c;
		vvi dist(n, vi(n, INF));
		REP(i, m) {
			cin >> a >> b >> c;
			dist[a][b] = min(dist[a][b], c);
			dist[b][a] = min(dist[b][a], c); 
		}

		priority_queue<state> Q;
		vi used(n);
		Q.push(state(0, 0));
		int ans = 0;
		while(!Q.empty()) {
			state st = Q.top();
			Q.pop();

			if(used[st.p]) {
				continue;
			}

			used[st.p] = 1;
			ans += st.c;

			REP(i, n) {
				if(dist[st.p][i] != INF && st.p != i) {
					Q.push(state(i, dist[st.p][i]));
				}
			}
		}

		cout << ans << endl;
	}

	return 0;
}

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

using namespace std;

#define MAX 110
#define MAX_E 10100

struct Edge{
  int u,v,cost;
  Edge(){}
  Edge(int u,int v,int cost) : u(u),v(v),cost(cost) {}
  bool operator < (const Edge &e)const{
    return cost < e.cost;
  }
};

int par[MAX],rank[MAX];

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;
  }
  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 e[MAX_E];

int kruskal(int E){
  sort(e,e+E);
  int res = 0;
  for(int i = 0 ; i < E ; i++){
    if(!same(e[i].u,e[i].v)){
      unite(e[i].u,e[i].v);
      res += e[i].cost;
    }
  }
  return res;
}

int main(){
  int N,M,a,b,c;
  while(cin >> N >> M,N){
    init(N);
    for(int i = 0 ; i < M ; i++){
      cin >> a >> b >> c;
      e[i] = Edge(a,b,c);
    }
    cout << kruskal(M) << endl;
  }
  return 0;
}