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>

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

const int INF = 10000000000;

int main(void)
{
	edge edges[10005];
	int ch[105];
	int n, m, ind;
	int i, j, a, b, v;
	int min, sum_cost;
	
	while (1){
		scanf("%d %d", &n, &m);
		
		if (n == 0 && m == 0) break;
		
		for (i = 0; i < n; i++) ch[i] = 0;
		
		for (i = 0; i < m; i++){
			scanf("%d %d %d", &a, &b, &v);
			edges[i].from = a;
			edges[i].to = b;
			edges[i].cost = v;
		}
		
		sum_cost = 0;
		ch[0] = 1;
		
		for (i = 0; i < n; i++){
			min = INF;
			
			for (j = 0; j < m; j++){
				if ( (ch[edges[j].from] == 1 && ch[edges[j].to] == 0) || (ch[edges[j].from] == 0 && ch[edges[j].to] == 1)){
					if (edges[j].cost < min){
						min = edges[j].cost;
						ind = j;
					}
				}
			}
			
			if (min == INF) break;
			
			if (ch[edges[ind].from] == 1 && ch[edges[ind].to] == 0){
				ch[edges[ind].to] = 1;
			}
			else{
				ch[edges[ind].from] = 1;
			}
			sum_cost += edges[ind].cost;
		}
		
		
		printf("%d\n", sum_cost);
	}
	
	return (0);
}

C++: #include <iostream>
#include <algorithm>
#include <cassert>
#include <cctype>
#include <complex>
#include <cstdio>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

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

struct edge{int u,v,cost;};

int par[100000],rank[100000],V,E;
edge es[100000];

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;else return 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);}

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

int kruskal(){
	sort(es,es+E,comp);
	init(V);
	int res=0;
	rep(i,E){
		edge e=es[i];
		if(!same(e.u,e.v)){
			unite(e.u,e.v);
			res+=e.cost;
		}
	}
	return res;
}

int main(){
	while(cin>>V>>E&&V){
		rep(i,E)cin>>es[i].u>>es[i].v>>es[i].cost;
		cout<<kruskal()<<endl;
	}
}

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

const int MAX_N = 100;
const int INF = 100000;

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

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

		for (int i = 0;  i < n; ++i)
			mincost[i] = std::min(mincost[i], cost[v][i]);
	
	}
	return res;
}

int main(){
	while (1){
		std::cin >> n >> m;
		if (n == 0 && m == 0)
			break;
		std::memset(used, 0, n);
		for (int i = 0; i < n; ++i){
			for (int j = 0; j < n; ++j)
				cost[i][j] = INF;
		}
		int a, b, c;

		for (int i = 0; i < m; ++i){
			std::cin >> a >> b >> c;
			cost[a][b] = c;
			cost[b][a] = c;
		}
		std::cout << prim() << std::endl;
	}
	return 0;
}

C++: #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <utility>
using namespace std;

#define rep(i,n) for(int (i)=0; (i)<(int)(n); ++(i))
#define foreach(c,i) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)

const int MAX_V = 102, INF = 1 << 30;
int cost[MAX_V][MAX_V];
int mincost[MAX_V];
bool used[MAX_V];
int V, E;

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

int solve() {
  int a, b, c;
  rep (i,V) rep (j,V) cost[i][j] = INF;
  rep (i,E) {
    cin >> a >> b >> c;
    cost[a][b] = cost[b][a] = c;
  }
  return prim();
}

int main() {
  while (cin >> V >> E, V|E) {
    cout << solve() << endl;
  }
  return 0;
}

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

const int INF = 1e+9;
const int MAX_V = 100;

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

int prim(){
	for(int i=0 ; i < n ; i++ ){
		mincost[i] = INF;
		used[i] = false;
	}
	mincost[0] = 0;
	int res = 0;
	while( true ){
		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;
}

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