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>

using namespace std;

const int inf = 999999;

int prim(int a[101][101], int n)
{
	int r = 0, mcost[101];
	bool vis[101];
	
	fill(mcost, mcost+101, inf);
	fill(vis, vis+101, false);
	mcost[0] = 0;
	
	while(1){
		int v = -1;
		
		for(int i = 0; i < n; i++){
			if(!vis[i] && (v == -1 || mcost[v] > mcost[i])){
				v = i;
			}
		}
		
		if(v == -1){
			break;
		}
		vis[v] = true;
		r += mcost[v];
		
		for(int i = 0; i < n; i++){
			mcost[i] = min(mcost[i], a[v][i]);
		}
	}
	
	return r;
}

int main()
{
	int n, m, a[101][101], s, t, u;
	
	while(1){
		cin >> n >> m;
		if(n == 0 && m == 0){
			break;
		}
		
		fill(&a[0][0], &a[101][101], inf);
		for(int i = 0; i <= n; i++){
			a[i][i] = 0;
		}
		
		while(m--){
			cin >> s >> t >> u;
			a[s][t] = a[t][s] = u;
		}
		
		cout << prim(a, n) << endl;
	}
	
	return 0;
}

C++: #include<iostream>
#include<algorithm>
 
using namespace std;
 
const int MAX = 101;
const int INF = (1<<20);
 
int cost[MAX][MAX];
int mincost[MAX];
bool used[MAX];
int V;
 
int prim(){
  mincost[0] = 0;
  int res = 0;
 
  while(1){
 
    int v = -1;
    for(int i = 0; i < V; 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 < V; i++)
      mincost[i] = min(mincost[i],cost[v][i]);
 
  }
  return res;
}
 
void init(){
  fill(mincost,mincost+MAX,INF);
  fill(used,used+MAX,false);
  for(int i = 0; i < MAX; i++)
    for(int j = 0; j < MAX; j++)
      cost[i][j] = INF;
}
int main(){
  int m;
  while(cin >> V  >> m && V+m){
    init();
   
    while(m--){
      int f,t,cos;
      cin >> f >> t >> cos;
      cost[f][t] = cost[t][f] = cos;
    }
    cout << prim() << endl;
  }
  return 0;
}

C++: #include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<cctype>
#include<complex>
#include<iostream>
#include<sstream>
#include<algorithm>
#include<functional>
#include<vector>
#include<string>
#include<stack>
#include<queue>
#include<map>

using namespace std;
const int dx[] = {1,0,-1,0},dy[] = {0,1,0,-1};
#define INF 1e+8
#define rep(i,j) for(int i = 0; i < (j); i++)
#define reps(i,j,k) for(int i = j; i < k; i++)
typedef long long ll;
typedef unsigned long long ull;
int par[128];
int rank[128];
struct edge{
	int from,to,cost;
};
bool cmp(const edge& left, const edge& right){
	return left.cost < right.cost;
}
void init(int n){
	rep(i,n){
		par[i] = i;
		rank[i] = 0;
	}
}
int find(int x){
	if(par[x] == x)return x;
	else 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;
		rank[y]++;
	}
	(rank[x] < rank[y]) ? par[x] = y : par[y] = x ;
}
bool same(int x,int y){
	return find(x) == find(y);
}
edge Data[128];
int main(){
	int n,m;
	while(scanf("%d%d",&n,&m),n|m){
		rep(i,m){
			scanf("%d%d%d",&Data[i].from,&Data[i].to,&Data[i].cost);
		}
		sort(Data,Data+m,cmp);
		init(n);
		int ans = 0;
		rep(i,m){
			edge next = Data[i];
			if(!same(next.from, next.to)){
				unite(next.from,next.to);
				ans += next.cost;
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}

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

using namespace std;
#define reps(i,f,n) for(int i=f; i<int(n); ++i)
#define rep(i,n) reps(i,0,n)

typedef vector<int> vi;
typedef vector<vi> vvi;

struct Edge {
	int src, dst, cost;
	Edge(int s, int d, int c) : src(s), dst(d), cost(c) {}
	Edge(){}
	bool operator< (const Edge& e)const {
		return cost < e.cost;
	}
};

struct UnionFind{
	vi par, rank;
	UnionFind(int n) : par(n), rank(n, 0) {
		rep(i, n)
			par[i] = i;
	}

	int find(int a){
		return par[a] == a ? a : (par[a] = find(par[a]));
	}

	bool same(int a, int b){
		return find(a) == find(b);
	}

	void unite(int a, int b){
		a = find(a);
		b = find(b);
		if(a == b)
			return;

		if(rank[a] < rank[b])
			par[a] = b;
		else
			par[b] = a;
		rank[a] += rank[a] == rank[b];
	}
};

int main()
{
	int n, m;
	while(scanf("%d%d", &n, &m), n){
		vector<Edge> E(m);
		rep(i, m){
			int a, b, c;
			scanf("%d%d%d", &a, &b, &c);
			E[i] = Edge(a, b, c);
		}
		sort(E.begin(), E.end());

		int cost = 0;
		UnionFind uf(n);
		rep(i, m){
			if(!uf.same(E[i].src, E[i].dst)){
				uf.unite(E[i].src, E[i].dst);
				cost += E[i].cost;
			}
		}
		printf("%d\n", cost);
	}

	return 0;
}