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 <bits/stdc++.h>

using namespace std;
using ll = long long;
using pii = pair<int, int>;

template <class T>
struct Kruskal {
  struct UnionFind {
    int N;
    vector<int> sz_tree, parent;
    UnionFind() {}
    UnionFind(int N) : N(N), sz_tree(N, 1), parent(N) {
      iota(parent.begin(), parent.end(), 0);
    }
    int root(int x) {
      return parent[x] == x ? x : parent[x] = root(parent[x]);
    }
    void merge(int x, int y) {
      if ((x = root(x)) == (y = root(y))) return;
      if (sz_tree[x] < sz_tree[y]) swap(x, y);
      sz_tree[x] += sz_tree[y];
      parent[y] = x;
    }
    bool same(int x, int y) {
      return root(x) == root(y);
    }
    int size(int x) {
      return sz_tree[root(x)];
    }
  };
  struct edge {
    int from, to; T cost;
    edge() {}
    edge(int f, int t, T c) : from(f), to(t), cost(c) {}
  };
 
  int N;
  vector<edge> es;
  Kruskal() {}
  Kruskal(int N) : N(N) {}
 
  void add_edge(int from, int to, T cost) {
    es.emplace_back(from, to, cost);
  }
  T mst() {
    T res = 0;
    UnionFind uf(N);
    sort(es.begin(), es.end(), [&](edge a, edge b) {
      return a.cost < b.cost;
    });
    for (auto &e : es) {
      if (uf.same(e.from, e.to)) continue;
      uf.merge(e.from, e.to);
      res += e.cost;
    }
    return res;
  }
};

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  
  int n, m;
  while (cin >> n >> m, n) {
    Kruskal<int> K(n);
    for (int i = 0; i < m; i++) {
      int a, b, cost;
      cin >> a >> b >> cost;
      K.add_edge(a, b, cost);
      K.add_edge(b, a, cost);
    }
    cout << K.mst() << endl;
  }

  return 0;
}

C++: #include <iostream>
#include <list>
#include <vector>
#include <algorithm>
#include <climits>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
int main(){
	int n,m,a,b,c,mi,ret;
	int info[100][100];
	while(cin >> n >> m && n){
		ret = 0;
		rep(i,100)rep(j,100)info[i][j] = INT_MAX;
		for(int i=0;i<m;i++){
			cin >> a >> b >> c;
			info[a][b] = info[b][a] = c;
		}
		list<int> X,Y;
		for(int i=1;i<n;i++){
			Y.push_back(i);
		}
		X.push_back(0);
		while(!Y.empty()){
			mi = INT_MAX;
			list<int>::iterator it = X.begin();
			while( it != X.end() ){
				for(int i=0;i<n;i++){
					if( count(Y.begin(),Y.end(),i) )
						if(info[*it][i]<mi){
							a = i;
							mi = info[*it][i];
						}
				}
				++it;
			}
			X.push_back(a);
			it = Y.begin();
			while( it != Y.end() ){
				if((*it)==a){
					Y.erase(it);
					break;
				}
				++it;
			}
			ret+=mi;
		}
		cout << ret << endl;
	}
}

C++: #include<bits/stdc++.h>
using namespace std;
#define r(i,n) for(int i=0;i<n;i++)
#define INF 1<<25
int n,cost[100][100],mincost[100];
bool used[100];
int prim(){
  r(i,n)mincost[i]=INF,used[i]=false;
  mincost[0]=0;
  int res=0;
  while(1){
    int v=-1;
    r(u,n)if(!used[u]&&(v==-1||mincost[u]<mincost[v]))v=u;
    if(v==-1)break;
    used[v]=1;
    res+=mincost[v];
    r(u,n)mincost[u]=min(mincost[u],cost[v][u]);
  }
return res;
}
int main(){
  int m,q,w,e;
  while(cin>>n>>m,n){
  r(i,n)r(j,n)cost[i][j]=INF;
  while(m--)cin>>q>>w>>e,cost[q][w]=e;
  cout<<prim()<<endl;
}
}

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

struct edge{ int from,to,cost; };

int rnk[100],par[100];

void init(int n){
	for(int i = 0;i < n;i++){
		rnk[i] = 0;
		par[i] = i;
	}
}

int find(int x){
	if(par[x] == x) return x;
	return par[x] = find(par[x]);
}

void unite(int a,int b){
	a = find(a);
	b = find(b);
	if(a == b) return;
	if(rnk[a] > rnk[b]){
		par[b] = a;
	}else{
		par[a] = b;
		if(rnk[a] == rnk[b]) rnk[b]++;
	}
}

bool comp(const edge& e1,const edge& e2){
	return e1.cost < e2.cost;
}

int main(){
	int n,m;
	while(1){
		vector<edge> es;
		int sum = 0;
		cin >> n >> m;
		if(!n) break;
		init(n);
		for(int i = 0;i < m;i++){
			int a,b,c;
			cin >> a >> b >> c;
			es.push_back({a,b,c});
		}
		sort(es.begin(),es.end(),comp);
		for(int i = 0;i < m;i++){
			if(find(es[i].to) != find(es[i].from)){
				unite(es[i].to,es[i].from);
				sum += es[i].cost;
			}
		}
		cout << sum << endl;
	}
	return 0;
}

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

class Node
{
	public:
	vector<int> to,c;
};

class Trl
{
	public:
	int p,c,dc;

	Trl(int p, int c, int dc):p(p),c(c),dc(dc){}
	bool operator<(const Trl& t) const
	{
		return c>t.c;
	}
};


int main()
{
	int n,m;
	int a,b,c;
	while(cin >> n >> m , (n||m))
	{
		Node node[100];
		while(m--)
		{
			cin >> a >> b >> c;
			node[a].to.push_back(b);
			node[b].to.push_back(a);
			node[a].c.push_back(c);
			node[b].c.push_back(c);
		}
		
		int total = 0;
		bool visit[100] = {false};
		priority_queue<Trl> q;
		q.push(Trl(0,0,0));

		while(!q.empty())
		{
			Trl t=q.top(); q.pop();
			if(visit[t.p]) continue;

			visit[t.p] = true;
			total += t.dc;

			for(int i=0; i<node[t.p].to.size(); i++)
			{
				int next = node[t.p].to[i];
				int tc   = node[t.p].c[i];
				if(visit[next]) continue;
				
				q.push(Trl(next,tc,tc));
			}
		}

		cout << total << endl;
	}
	return 0;
}