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<vector>
#include<list>
#include<algorithm>
#include<iostream>
#include<string>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
using namespace std;
int f(int a[][100],int b[],int c[],int n){
  int i,j;
  int mn=-1;
  for(i=0;i<n;i++){
    if(c[i]){
      for(j=0;j<n;j++){
	if(a[i][j]!=-1&&c[j]==0&&(b[j]==-1||b[j]>a[i][j]))
	  b[j]=a[i][j];
      }
    }
  }
  for(i=0;i<n;i++){
    if(b[i]!=-1&&c[i]==0&&(mn==-1||b[mn]>b[i]))
      mn=i;
  }
  c[mn]=1;
  return b[mn];
}
int main(){
  int h,i,j;
  int a[100][100],b[100],c[100],m,n;
  int sm;
  while(cin>>n>>m&&n+m){
    memset(a,-1,sizeof(a));
    memset(c,0,sizeof(c));
    for(i=0;i<m;i++){
      int p,q,r;
      cin>>p>>q>>r;
      a[p][q]=r;
      a[q][p]=r;
      c[p]++;
      c[q]++;
    }
    sm=0;
    do{
      h=0;
      for(i=0;i<n;i++){
	if(c[i]==1){
	  for(j=0;a[i][j]==-1;j++);
	  sm+=a[i][j];
	  a[i][j]=-1;
	  a[j][i]=-1;
	  c[i]--;
	  c[j]--;
	  h=1;
	}
      }
    }while(h);
    for(i=0;c[i]==0;i++);
    memset(b,-1,sizeof(b));
    memset(c,0,sizeof(c));
    b[i]=0;
    c[i]=1;
    for(i=1;i<n;i++)
      sm+=f(a,b,c,n);
    cout<<sm<<endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

#define dump(n) cout<<"# "<<#n<<'='<<(n)<<endl
#define repi(i,a,b) for(int i=int(a);i<int(b);i++)
#define peri(i,a,b) for(int i=int(b);i-->int(a);)
#define rep(i,n) repi(i,0,n)
#define per(i,n) peri(i,0,n)
#define all(c) begin(c),end(c)
#define mp make_pair
#define mt make_tuple

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;

const int INF=1e9;
const int MOD=1e9+7;
const double EPS=1e-9;

template<typename T1,typename T2>
ostream& operator<<(ostream& os,const pair<T1,T2>& p){
	return os<<'('<<p.first<<','<<p.second<<')';
}
template<typename T>
ostream& operator<<(ostream& os,const vector<T>& a){
	os<<'[';
	rep(i,a.size()) os<<(i?" ":"")<<a[i];
	return os<<']';
}

struct UnionFind{
	vi data;
	UnionFind(int n):data(n,-1){}
	int Find(int i){
		return data[i]<0?i:(data[i]=Find(data[i]));
	}
	bool Unite(int a,int b){
		a=Find(a),b=Find(b);
		if(a==b) return false;
		if(-data[a]<-data[b]) swap(a,b);
		data[a]+=data[b];
		data[b]=a;
		return true;
	}
	int Size(int i){
		return -data[Find(i)];
	}
};

struct Edge{
	int src,dst,weight;
	Edge(){}
	Edge(int s,int d,int w):src(s),dst(d),weight(w){}
};
typedef vector<vector<Edge>> Graph;
bool operator<(const Edge& a,const Edge& b){return a.weight<b.weight;}
bool operator>(const Edge& a,const Edge& b){return a.weight>b.weight;}

int Kruskal(const Graph& g,vector<Edge>& forest)
{
	int n=g.size();
	vector<Edge> es;
	rep(i,n) es.insert(end(es),all(g[i]));
	sort(all(es));
	
	UnionFind uf(n);
	int res=0;
	for(auto e:es)
		if(uf.Unite(e.src,e.dst)){
			res+=e.weight;
			forest.push_back(e);
		}
	return res;
}

int main()
{
	for(int n,m;cin>>n>>m && n|m;){
		Graph g(n);
		rep(i,m){
			int u,v,w; cin>>u>>v>>w;
			g[u].emplace_back(u,v,w);
			g[v].emplace_back(v,u,w);
		}
		vector<Edge> forest;
		cout<<Kruskal(g,forest)<<endl;
	}
}

C++: #include <iostream>
#include <list>
#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;

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

		list<int> X,Y;
		X.push_back(0);
		for(int i=1;i<n;i++){
			Y.push_back(i);
		}

		while(!Y.empty()){
			mi = INT_MAX;
			for(list<int>::iterator it = X.begin(); it != X.end(); ++it){
				rep(i,n){
					if( count(Y.begin(),Y.end(),i) ){
						if(info[*it][i]<mi){
							a = i;
							mi = info[*it][i];
						}
					}
				}
			}
			X.push_back(a);
			for(list<int>::iterator it = Y.begin(); it != Y.end(); ++it){
				if((*it)==a){
					Y.erase(it);
					break;
				}
			}
			ret+=mi;
		}
		cout << ret << endl;
	}
}