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 <list>
#include <queue>
#include <vector>
#include <iostream>
#include <algorithm>
#include <functional>
#define INF (1LL << 60)
using namespace std;
bool used[100]; long long dist[100];
long long prim(int V, vector<pair<int, long long> > X[]) {
	priority_queue<pair<long long, int>, vector<pair<long long, int> >, greater<pair<long long, int> > > Q;
	for (int i = 0; i < V; i++) dist[i] = INF, used[i] = false;
	dist[0] = 0; Q.push(make_pair(0, 0));
	while (!Q.empty()) {
		pair<int, long long> pa = Q.top(); Q.pop();
		long long a1 = pa.first, a2 = pa.second; used[a2] = true;
		for (int i = 0; i < X[a2].size(); i++) {
			int to = X[a2][i].first; long long leng = X[a2][i].second;
			if (dist[to] > leng && !used[to]) {
				dist[to] = leng; Q.push(make_pair(dist[to], to));
			}
		}
	}
	long long sum = 0;
	for (int i = 0; i < V; i++) sum += dist[i];
	return sum;
}
int n, m, u, v, w; vector<pair<int, long long> > G[100];
int main() {
	while(cin >> n >> m, n) {
		for(int i = 0; i < n; i++) G[i].clear();
		for(int i = 0; i < m; i++) {
			cin >> u >> v >> w;
			G[u].push_back(make_pair(v, w));
			G[v].push_back(make_pair(u, w));
		}
		cout << prim(n, G) << endl;
	}
}

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

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

int par[101],rank[101];
edge es[150];
int V,E;

void init_union_find(int);
int find(int);
void unite(int,int);
bool same(int,int);
bool comp(const edge& e1,const edge& e2);
int kruskal(void);

using namespace std;

int main(void){
  int i,a,b,c;

  while(cin >> V >> E && V){
    for(i=0;i<E;i++){
      cin >> a >> b >> c;
      es[i].u=a;
      es[i].v=b;
      es[i].cost=c;
    }
    printf("%d\n",kruskal());
  }
  return 0;
}

void init_union_find(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 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;
  }
  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_union_find(V);
  int res=0;
  for(int i=0;i<E;i++){
    edge e=es[i];
    if(!same(e.u,e.v)){
      unite(e.u,e.v);
      res+=e.cost;
    }
  }
  return res;
}

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

using namespace std;

#define rep(i,k,n) for(int i = k ; i < n ; i++ )
#define INF 10000000

bool used[101];
int mincost[101];
int cost[101][101];
int V;

int prim(){

  fill(mincost,mincost + 101,INF);
  fill(used,used+101,false);

  int re = 0;
  mincost[0] = 0;
  while(1){

    int v = -1;
    for(int i = 0 ; i < V ; i++ ){
      if(!used[i] && (v == -1 || mincost[i] < mincost[v]))v = i;
    }

    re += mincost[v];
    if(v == -1)break;
    used[v] = true;


    for(int i = 0 ; i < V ; ++i){
      if(!used[i])mincost[i] = min(mincost[i],cost[v][i]);
    }
  }
  return re;
}

int main(){
  int n;
  while(cin >> V >> n , V+n){

    fill(cost[0],cost[101],INF);
    for(int i = 0 ; i < n ; ++i ){
      int a,b,c;
      cin >> a >> b >> c;
      cost[a][b] = cost[b][a] = c;
    }
    int res = prim();
    cout << res << endl;
  }

  return 0;
}

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

int cost[100][100];
int mincost[100];
bool used[100];
const int INF = 1000000;

void init()
{
	for(int i=0;i<100;i++){
		for(int j=0;j<100;j++){
			if(i==j)
				cost[i][j] = 0;
			else
				cost[i][j] = INF;
		}
	}
}

int main()
{
	int n,m;
	while(cin >> n >> m){
		if(n==0 && m==0)
			break;
		init();
		for(int i=0;i<m;i++){
			int a,b,c;
			cin >> a >> b >> c;
			cost[a][b] = c;
			cost[b][a] = c;
		}
		
		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]);
			}
		}
		cout << res << endl;
	}
	return 0;
}