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 <algorithm>
#include <vector>
using namespace std;

struct Bridge
{
	int cost;
	int edge[ 2 ];
};

class BridgeAsc
{
public:
	bool operator()( const Bridge& a, const Bridge& b ) const
	{
		if( a.cost < b.cost ) return true;
		return false;
	};
};

int main()
{
	int n, m;
	while( cin >> n >> m && ( n || m ) ){
		vector< Bridge > bridge;
		for( int i = 0; i < m; ++i ){
			Bridge b;
			cin >> b.edge[ 0 ] >> b.edge[ 1 ] >> b.cost;
			bridge.push_back( b );
		}
		sort( bridge.begin(), bridge.end(), BridgeAsc() );

		int city[ 100 ] = {0};
		int id = 1;
		int cntBridge = 0;
		int index = 0;
		int ans = 0;
		while( cntBridge < n - 1 ){
			Bridge b = bridge[ index ];
			int con = 0;
			int x = b.edge[ 0 ];
			int y = b.edge[ 1 ];
			if( city[ x ] != 0 && city[ x ] == city[ y ] ){
				++index;
				continue;
			}
			if( city[ x ] == 0 && city[ y ] == 0 ){
				city[ x ] = city[ y ] = id++;
			}else if( city[ x ] == 0 ){
				city[ x ] = city[ y ];
			}else if( city[ y ] == 0 ){
				city[ y ] = city[ x ];
			}else{
				int tmp = city[ y ];
				for( int i = 0; i < n; ++i ){
					if( city[ i ] == tmp ){
						city[ i ] = city[ x ];
					}
				}
			}
			ans += b.cost;
			++cntBridge;
			++index;
		}
		cout << ans << endl;
	}

	return 0;
}

C++: //kruskal tree
#include <iostream>
#include <algorithm>
using namespace std;
#define M 999
int parent[M],a[M],b[M];
pair<int,int>node[M];
int root(int a){return parent[a]==a?a:parent[a]=root(parent[a]);}
int unite(int a,int b){
	int x=root(a),y=root(b);
	if(x==y)return 0;
	parent[x]=y;
	return 1;
}
int main(){
	int i,s,n,m;
	for(;cin>>n>>m,n;cout<<s<<endl){
		for(i=0;i<m;i++)cin>>a[i]>>b[i]>>node[i].first,node[i].second=i;
		sort(node,node+m);
		for(i=0;i<n;i++)parent[i]=i;
		for(s=i=0;i<m;i++)if(unite(a[node[i].second],b[node[i].second]))s+=node[i].first;
	}
}

C++: #include<bits/stdc++.h>
#define rep(i,n)for(int i=0;i<n;i++)
using namespace std;

int cost[100][100];
int mincost[100];
bool used[100];

int main() {
	int n, m;
	while (scanf("%d%d", &n, &m), n) {
		memset(cost, 0x3f, sizeof(cost));
		rep(i, m) {
			int a, b, c; scanf("%d%d%d", &a, &b, &c);
			cost[a][b] = c;
		}
		memset(mincost, 0x3f, sizeof(mincost));
		memset(used, 0, sizeof(used));
		mincost[0] = 0;
		int ans = 0;
		while (true) {
			int v = -1;
			rep(i, n) {
				if (!used[i] && (v == -1 || mincost[i] < mincost[v]))
					v = i;
			}
			if (v == -1)break;
			used[v] = true;
			ans += mincost[v];
			rep(i, n)mincost[i] = min(mincost[i], cost[v][i]);
		}
		printf("%d\n", ans);
	}
}

C++: #include <cstdio>
#include <iostream>
#include <string>
#define INF (1<<21)
using namespace std;

int main(){
  int i,j,n,m,s,t,cost,a[100][100],d[100],f[100],min,p,sum;
  while(1){
    cin >> n >> m;
    if(n == 0 && m == 0) break;
    for(i=0;i<n;i++){
      d[i] = INF;
      f[i] = 0;
      for(j=0;j<n;j++){
	a[i][j] = INF;
      }
    }

    for(i=0;i<m;i++){
      cin >> s >> t >> cost;
      a[s][t] = a[t][s] = cost;
    }

    d[0] = 0;
    while(1){
      min = INF;
      for(i=0;i<n;i++){
	if(d[i]<min && f[i] == 0){
	  min = d[i];
	  p = i;
	}
      }
      if(min == INF) break;
      f[p] = 1;
      for(i=0;i<n;i++){
	if(f[i] == 1) continue;
   	if(a[p][i] < d[i]) d[i] = a[p][i];
      }
    }
    
    sum = 0;
    for(i=0;i<n;i++){
      sum += d[i];
    }

    cout << sum << endl;
  }
  return 0;
}

C++: #include <iostream>
#include <vector>
#include <queue>

using namespace std;

struct Edge {
	int to, cost;
};
bool operator >(const Edge& e1, const Edge& e2) {
	return e1.cost > e2.cost;
}

int main() {
	int n, m;
	while ( cin >> n >> m, n || m ) {
		vector< vector<Edge> > edge(n);
		for (int i = 0; i < m; ++i) {
			int a, b, cost; cin >> a >> b >> cost;
			edge[a].push_back( (Edge){ b, cost } );
			edge[b].push_back( (Edge){ a, cost } );
		}

		int ans = 0;

		priority_queue<Edge, vector<Edge>, greater<Edge> > Q;
		Q.push( (Edge){ 0, 0 } );
		vector<bool> f(n, false);
		while ( !Q.empty() ) {
			Edge e = Q.top(); Q.pop();

			if (f[e.to]) continue;
			f[e.to] = true;

			ans += e.cost;

			for (int i = 0; i < edge[e.to].size(); ++i) {
				Q.push(edge[e.to][i]);
			}
		}
		cout << ans << endl;
	}
}