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<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<math.h>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<string>
#include<cmath>
#include<set>
#include<stack>
#include<climits>

using namespace std;
typedef unsigned long long ll;

int main(){
    int n,m;
    while(cin>>n>>m,n){
        int city[102][102];
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                city[i][j]=INT_MAX;
            }
            city[i][i]=0;
        }
        for(int i=0;i<m;i++){
            int a,b,c;
            cin>>a>>b>>c;
            city[a][b]=c;
            city[b][a]=c;
        }
        vector<int>que;
        vector<int>vec;
        vec.push_back(0);
        for(int i=1;i<n;i++){
            que.push_back(i);
        }
        int ret=0;
        while(!que.empty()){
            int mi=INT_MAX;
            int cur=-1,in;
            for(int j=0;j<que.size();j++){
                for(int i=0;i<vec.size();i++){
                    if(city[vec[i]][que[j]]<mi){
                        mi=city[vec[i]][que[j]];
                        cur=que[j];
                        in=j;
                    }
                }
            }
            ret+=mi;
            que.erase(que.begin()+in);
            vec.push_back(cur);
        }
        cout<<ret<<endl;
    }
    
    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mp make_pair
#define pb push_back
#define fr first
#define sc second
#define Rep(i,n) for(int i=0;i<(n);i++)
#define Rrep(i, m, n) for(int i=(m);i<(n);i++)
#define All(v) v.begin(),v.end()
#define Uniq(v) v.erase(unique(All(v)),v.end())
typedef pair<int, int> Pii;
typedef pair<int, Pii> Pip;
typedef pair<string, int> Psi;
typedef vector<int> Vi;
const int INF = (1<<30);
const int dx[]={1,0,-1,0}, dy[]={0,-1,0,1};

int n, m;
int cost[101][101];
int mincost[101];
bool used[101];


int prim() {
  fill_n(mincost, 101, INF);
  fill_n(used, 101, false);

  mincost[0] = 0;
  int res = 0;

  while( 1 ) {
    int v = -1;
    Rep(u, n) {
      if( !used[u] && (v == -1 || mincost[u] < mincost[v]) ) v = u;
    }

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

    Rep(u, n) {
      mincost[u] = min(mincost[u], cost[v][u]);
    }
  }

  return res;
}

	  
int main()
{
  while( cin >> n >> m, n+m ) {
    fill_n(*cost, 101*101, INF);

    Rep(i, m) {
      int a, b, c;
      cin >> a >> b >> c;
      cost[a][b] = c;
    }

    cout << prim() << endl;
  }

}

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

using namespace std;

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

struct UnionFind {
    vector<int> data;
    UnionFind(int size) : data(size, -1) { }
    bool unionSet(int x, int y) {
        x = root(x); y = root(y);
        if (x != y) {
            if (data[y] < data[x]) swap(x, y);
            data[x] += data[y]; data[y] = x;
        }
        return x != y;
    }
    bool findSet(int x, int y) {
        return root(x) == root(y);
    }
    int root(int x) {
        return data[x] < 0 ? x : data[x] = root(data[x]);
    }
    int size(int x) {
        return -data[root(x)];
    }
};

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

const int MAX_M = (100 - 1) * 50 + 1;
int N, M;
edge es[MAX_M];

void Kruskal()
{
    sort(es, es + M, comp);
    UnionFind uf(N);
    int res = 0;
    int cnt = 0;

    for (int i = 0; i < M; i++) {
        edge e = es[i];
        if (!uf.findSet(e.u, e.v)) {
            uf.unionSet(e.u, e.v);
            res += e.cost;
            cnt++;
        }
    }

    cout << res << endl;
}

int main()
{
    while (cin >> N >> M, N || M) {
        for (int i = 0; i < M; ++i)
            cin >> es[i].u >> es[i].v >> es[i].cost;
        Kruskal();
    }

    return 0;
}

C++: #include <iostream>
#define rep(i,n) for(int i=0;i<n;i++)
//#define int long long
using namespace std;

#define INF 999999

int n, m;
int a, b, c;

int cost[100][100];
int mincost[100];
bool used[100];

void init()
{
	rep(i,100) rep(j,100)
	{
		cost[i][j] = INF;
	}
}

int prim()
{
	rep(i,n)
	{
		mincost[i] = INF;
		used[i] = false;
	}

	mincost[0] = 0;
	int res = 0;

	while (true)
	{
		int v = -1;
		rep(u,n)
		{
			if (!used[u] && (v == -1 || mincost[u] < mincost[v])) v = u;
		}

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

		rep(u,n)
		{
			mincost[u] = min(mincost[u], cost[v][u]);
		}
	}

	return res;
}

int main()
{
	while (true)
	{
		cin >> n >> m;
		if (n == 0 && m == 0) break;

		init();

		rep(i,m)
		{
			cin >> a >> b >> c;
			cost[a][b] = c;
			cost[b][a] = c;
		}

		printf("%d\n", prim());
	}
}