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 <fstream>
#include <sstream>
#include <iomanip> 
#include <complex> 
#include <string>
#include <vector> 
#include <list>
#include <deque> 
#include <stack> 
#include <queue> 
#include <set>
#include <map>
#include <bitset>
#include <functional>
#include <utility>
#include <algorithm> 
#include <numeric> 
#include <typeinfo> 
#include <cstdio>
#include <cstdlib> 
#include <cstring>
#include <cmath>
#include <climits> 
#include <ctime>

using namespace std;

const int INF=1000000;
int n,m;
int cost[101][101];
int mincost[101];
bool used[101];

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

int main(void){
	int a,b;
	while(1){
		for(a=0;a<=100;a++){
			for(b=0;b<=100;b++){
				cost[a][b]=INF;
				if(a==b)cost[a][b]=0;
			}
		}
		cin >> n >> m;
		if(n==0 && m==0)break;
		for(a=0;a<m;a++){
			int b,c,d;
			cin >> b >> c >> d;
			if(cost[b][c]>d){
				cost[b][c]=d;
				cost[c][b]=d;
			}
		}
		int ans=prim();
		cout << ans << endl;
	}
	return 0;
}

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

#define INF 1e9


// UnionFind??¨ ??????????????????
class UnionFindTree{
private:
  int m_size, unite_count;
  vector<int> parent;
  vector<int> rank;
  int find(int a);
public:
  void init(int size);
  void unite(int a, int b);
  bool same(int a, int b);
  bool isConnected(void);
};

// UF??¨????????????
void UnionFindTree::init(int size){
  m_size = size;
  unite_count = 0;
  parent.clear();
  rank.clear();
  for(int i = 0; i < size; i++){
    parent.push_back(i);
    rank.push_back(0);
  }
  return;
}

// UF??¨?????????????????????
int UnionFindTree::find(int a){
  if(parent[a] != a){ parent[a] = find(parent[a]); }
  return parent[a];
}

// UF??¨??§???????????£?????????
void UnionFindTree::unite(int a, int b){
  int x = find(a);
  int y = find(b);
  // ?????§???????????£???????????????????????????
  if(x == y){ return; }

  unite_count++;
  if(rank[x] < rank[y]){ parent[x] = y; }
  else{
    parent[y] = x;
    if(rank[x] == rank[y]){ rank[x]++; }
  }
  return;
}

// UF??¨??§???????????????????????????????????????
bool UnionFindTree::same(int a, int b){
  return find(a) == find(b);
}

// UF??¨??§?????????????????????????????¨???????????????????????????
bool UnionFindTree::isConnected(void){
  return unite_count == m_size - 1;
}

int kruskal(int v, vector<pair<int, pair<int, int> > > sorted_edgelist){
  int sum = 0;
  UnionFindTree uf;
  uf.init(v);

  for(int i = 0; i < (int)sorted_edgelist.size() && !uf.isConnected(); i++){
    int cost = sorted_edgelist[i].first;
    int a = sorted_edgelist[i].second.first;
    int b = sorted_edgelist[i].second.second;

    if(!uf.same(a, b)){
      sum += cost;
      uf.unite(a, b);
    }
  }
  
  return sum;
}


int main(void){
  while(true){
    int n, m;
    cin >> n >> m;
    if(n == 0 && m == 0){ break; }
    
    int a, b, cost;
    vector<pair<int, pair<int, int> > > edgelist(n);
    for(int i = 0; i < m; i++){
      cin >> a >> b >> cost;
      edgelist.push_back(make_pair(cost, make_pair(a, b)));
    }
    sort(edgelist.begin(), edgelist.end());
    
    cout << kruskal(n, edgelist) << endl;
  }

  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
int V, m;
int cost[200][200];
int mincost[200];
bool used[200];

int prim(){
  for(int i=0; i < V; ++i){
    mincost[i] = INT_MAX/2;
    used[i] = false;
  }
  mincost[0] = 0;
  int res = 0;

  while(true){
    int v = -1;
    for(int u=0; u < V; 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 < V; u++){
      mincost[u] = min(mincost[u], cost[v][u]);
    }
  }
  return res;
}

int main(void){
  while(cin >> V >> m, V | m){
    for(int i=0; i < V; i++)
      for(int j=0; j < m; j++)
        cost[i][j] = INT_MAX/2;
    for(int i=0; i < m; i++){
      int a, b, c; cin >> a >> b >> c;
      cost[a][b] = c;
      cost[b][a] = c;
    }
    cout << prim() << endl;
  }
  return 0;
}

C++: #include <iostream>
#include <algorithm>
using namespace std;
int parent[10010];
void init(int N){int i=0;for(;i<N;i++)parent[i]=i;}
int root(int a){
	if(parent[a]==a)return a;
	return parent[a]=root(parent[a]);
}
bool is_same_set(int a,int b){return root(a)==root(b);}
void unite(int a,int b){parent[root(a)] = root(b);}

int i, s, N, M, A[10010], B[10010], COST[10010];
pair<int,int> bridge[10010];
int main(){
	for(;cin>>N>>M,N;cout<<s<<endl){
		for(i=0;i<M;i++){
			cin>>A[i]>>B[i]>>COST[i];
			bridge[i].first = COST[i];
			bridge[i].second = i;
		}
		sort(bridge, bridge+M);
		init(N);
		for(s=i=0;i<M;i++){
			int cost = bridge[i].first;
			int a = A[bridge[i].second];
			int b = B[bridge[i].second];
			if(is_same_set(a,b))continue;
			unite(a,b);
			s+=cost;
		}
	}
}