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 <vector>
#include <queue>
#define INF (1e9)
#define cost first
#define node second
using namespace std;
typedef pair <int, int> P;
int n,m;
vector<P> G[101];

int prim(){
  int res = 0;
  bool visited[101]={};
  int D[101];
  for(int i=0;i<n;i++) D[i] = INF;
  priority_queue <P, vector<P>, greater<P> > Q;
  Q.push(P(0,0));
  while(!Q.empty()){
    P t = Q.top(); Q.pop();
    if(visited[t.node]) continue;
    visited[t.node] = true;
    res += t.cost;
    for(int i=0;i<G[t.node].size();i++){
      int nnode = G[t.node][i].node;
      int ncost = G[t.node][i].cost;
      if(D[nnode] > ncost) Q.push(P(ncost,nnode)),D[nnode]=ncost;
    }
  }
  return res;
}
void init(int n){
  for(int i=0;i<n;i++) G[i].clear();
}

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

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

const int INF = 100000;
struct Edge { int from, to, cost; };
int dist[100];

int main() {
    int n, m;
    while (cin >> n >> m, n) {
        vector<Edge> G;
        int a, b, cost;
        for (int i=0; i<m; ++i) {
            cin >> a >> b >> cost;
            G.push_back((Edge){ a, b, cost });
            G.push_back((Edge){ b, a, cost });
        }
        int ret = 0;
        fill(dist, dist+100, INF);
        dist[0] = 0;
        while (1) {
            int k, mn = INF;
            for (int i=0; i<(int)G.size(); ++i) {
                if (dist[G[i].to] == INF && dist[G[i].from] + G[i].cost < mn) {
                    mn = dist[G[i].from] + G[i].cost;
                    k = i;
                }
            }
            if (mn == INF) break;
            dist[G[k].to] = dist[G[k].from] + G[k].cost;
            ret += G[k].cost;
        }
        cout << ret << endl;
    }
    return 0;
}

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 ] ){
				city[ x ] = city[ y ] = id++;
			}else if( city[ x ] == 0 ){
				city[ x ] = city[ y ];
			}else if( city[ y ] == 0 ){
				city[ y ] = city[ x ];
			}else{
				for( int i = 0; i < n; ++i ){
					if( city[ i ] == city[ y ] ){
						city[ i ] = city[ x ];
					}
				}
			}
			ans += b.cost;
			++cntBridge;
			++index;
		}
		cout << ans << endl;
	}

	return 0;
}

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

using namespace std;

#define INF (1 << 30)
#define MAX_V 100

int cost[MAX_V][MAX_V]; // cost[u][v] ?????????u??????v???????????????
int mincost[MAX_V];
bool used[MAX_V];
int V;// V???????????°

// ????°???¨?????¨??§?????????O(V^2)

// ???????????????????????????????????????cost????¨?????????????????????¨

int prim(){
	
	for(int i = 0; i < V; i++){
		mincost[i] = INF;
		used[i] = false;
	}
	
	mincost[0] = 0;
	
	int total = 0;
	
	while(true){
		
		int add_v = -1;
		
		for(int u = 0; u < V; u++){
			if(!used[u] && (add_v == -1 || mincost[u] < mincost[add_v])){
				add_v = u;
			}
		} // ??????????????????????±????
		
		if(add_v == -1){
			break;
		} // ??´??°???????????????????????´????????????
		used[add_v] = true;
		total += mincost[add_v];
		
		for(int u = 0; u < V; u++){
			mincost[u] = min(mincost[u], cost[add_v][u]);
		} // ???????????????????????????????°???????????????´??°
	}
	return total;
}

int main(){
	
	int ans, m;
	
	while(true){
		cin >> V >> m;
		if(V == 0 && m == 0){
			break;
		}
		for(int i = 0; i < V; i++){
			for(int j = 0; j < V; j++){
				cost[i][j] = INF;
			}
		}
		
		int a, b, c;
		
		for(int i = 0; i < m; i++){
			cin >> a >> b >> c;
			cost[a][b] = c;
			cost[b][a] = c;
		}
		
		ans = prim();
		printf("%d\n", ans);
	}
	
	return 0;
}