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<queue>
#include<vector>

using namespace std;

class Load{
	public:
	int n,m,o,p;
	load(){p=0;}
	load(int a,int b){n=a;m=b;p=0;}
};

int n,m;
Load load[100];

int prim(){
	vector<int> visit;
	visit.push_back(0);
	int cont=0;
	
	while(1){
		if(visit.size()==n){
			break;
		}
		int maxi=1000000000;
		int maxo=0;
		int maxa=0;
		for(int i=0;i<m;i++){
			for(int j=0;j<visit.size();j++){
				if(load[i].n==visit[j]&&load[i].p==0){
					if(maxi>load[i].o){
						load[i].p=1;
						maxi=load[i].o;
						maxo=load[i].m;
						maxa=i;
					}
				}
				if(load[i].m==visit[j]&&load[i].p==0){
					if(maxi>load[i].o){
						load[i].p=1;
						maxi=load[i].o;
						maxo=load[i].n;
						maxa=i;
					}
				}
			}
		}
		//printf("%d %d %d\n",maxa,maxo,maxi);
		cont+=maxi;
		visit.push_back(maxo);
	}
	return cont;
}

int main(){
	while(1){
	cin>>n>>m;
		if(!n){
			break;
		}
		for(int i=0;i<m;i++){
			int a,b,c;
			cin>>a>>b>>c;
			load[i].n=a;
			load[i].m=b;
			load[i].o=c;
			load[i].p=0;
		}
		printf("%d\n",prim());
	}
	
}

C++: #include <bits/stdc++.h>
#include<stdio.h>
using namespace std;
#define FOR(i,k,n) for(int i = (k); i < (n); i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(a) begin(a),end(a)
#define MS(m,v) memset(m,v,sizeof(m))
#define D10  fixed<<setprecision(10)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> P;
typedef long long ll;
const ll INF = 114514810;
const ll MOD = 1000000007;
const double EPS = 1e-8;
const double PI = acos(-1.0);
struct edge
{
	int from, to, cost;
	bool operator < (const edge& e) const { return cost < e.cost; }
	bool operator >(const edge& e) const { return cost > e.cost; }
};
int dx[] = { -1, 0, 0, 1 }; int dy[] = { 0, -1, 1, 0 };
bool valid(int x, int y, int h, int w) { return (x >= 0 && y >= 0 && x < h&&y < w); }
///*************************************************************************************///
///*************************************************************************************///
///*************************************************************************************///

int V, E;
vector<vector<edge>> es;
bool used[10000];

pair<int, vector<edge>> prim()
{
	memset(used, false, sizeof(used));
	vector<edge> res;
	int sum = 0;

	priority_queue<edge, vector<edge>, greater<edge>> que;
	que.push(edge{ -1, 0, 0 });

	while (!que.empty())
	{
		edge e = que.top(); que.pop();
		if (used[e.to]) continue;
		if (e.from >= 0) res.push_back(e);
		sum += e.cost;
		used[e.to] = true;
		for (edge i : es[e.to])
		{
			if (!used[i.to]) que.push(i);
		}
	}
	return pair<int, vector<edge>>(sum, res);
}

int main()
{
	while (cin >> V >> E, V)
	{
		es.clear();
		es.resize(V);
		REP(i, E)
		{
			int a, b, c;
			cin >> a >> b >> c;
			es[a].push_back(edge{ a, b, c });
			es[b].push_back(edge{ b, a, c });
		}
		cout << prim().first << endl;
	}
}

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

int main(){
  int br[100][100];
  bool f[100];
  int tr[100];
  int n,m;

  while(1){
    cin>>n>>m;
    for(int i=0;i<n;i++){
      for(int j=0;j<n;j++)
	br[i][j] = -1;
      tr[i]=INT_MAX;
      f[i]=0;
    }    
    for(int k=0;k<m;k++){
      int a,b,cost;
      cin>>a>>b>>cost;
      br[a][b]=cost;
      br[b][a]=cost;
    }
    int comin=0,r,trmin;
    tr[0] = 0;

    for(int k=0;k<n;k++){
      trmin=INT_MAX;
      for(int i=0;i<n;i++)
	if(f[i]==0 && trmin>tr[i]) r=i,trmin=tr[i];
      //cout << trmin<<endl;
      comin+=trmin;
      f[r]=1;
      for(int i=0;i<n;i++){
	if(f[i]==0 && tr[i]>br[r][i] && br[r][i]!=-1) tr[i]=br[r][i];
      }
    }
    cout <<comin<<endl;
  }
  
  return 0;
}

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

using namespace std;

const int INF = 1000000000;

int n, m;
int cost[100][100];
bool used[100];
int mincost[100];

int prim(){
  for(int i=0;i<n;++i){
    mincost[i] = INF;
    used[i] = false;
  }
  mincost[0] = 0;
  int res = 0;
  while(1){
    int v = -1;
    for(int u=0;u<n;++u){
      if(!used[u] && (v == -1 || mincost[u] < mincost[v])) v = u;
    }
    if(v == -1) break;
    used[v] = true;
    res += mincost[v];
    for(int u=0;u<n;++u){
      mincost[u] = min(mincost[u], cost[v][u]);
    }
  }
  return res;
}

main(){
  while(cin >> n >> m && (n || m)){
    fill(cost[0], cost[100], INF);
    for(int i=0;i<m;++i){
      int a, b, c;
      cin >> a >> b >> c;
      cost[a][b] = cost[b][a] = c;
    }
    cout << prim() << endl;
  }
  return 0;
}