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<string.h>
#include<math.h>
int main(void)
{
	int a,s,d[1001],f[1001],g[1001],h,i,j,size[1001],ban[1001];
	int z,x,q;
	while(1){
		q=0;
		scanf("%d %d",&a,&s);
		if(a==0&&s==0) break;
		for(i=0;i<a;i++){
			ban[i]=i;
			size[i]=1;
		}
		for(i=0;i<s;i++) scanf("%d %d %d",&d[i],&f[i],&g[i]);
		for(i=0;i<s;i++){
			if(d[i]>f[i]) h=d[i]; d[i]=f[i]; f[i]=h;
			for(j=i+1;j<s;j++){
				if(g[i]>g[j]){
					h=g[i]; g[i]=g[j]; g[j]=h;
					h=d[i]; d[i]=d[j]; d[j]=h;
					h=f[i]; f[i]=f[j]; f[j]=h;
				}
			}
		}
		for(i=0;i<s;i++){
			z=d[i]; x=f[i];
			while(ban[z]!=z) z=ban[z];
			while(ban[x]!=x) x=ban[x];
			if(z!=x){
				q+=g[i];
				if(size[z]>size[x]) ban[z]=x;
				else if(size[z]<size[x]) ban[x]=z;
				else{
					ban[x]=z;
					size[x]++;
				}
			}
		}
		printf("%d\n",q);
	}
	return 0;
}

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

using namespace std;

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

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

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,rank[x]+=(rank[x]==rank[y]);
}

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;
}

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

  while(cin >> V >> E && V){
    for(int i=0;i<E;i++){
      cin >> a >> b >> c;
      es[i].u=a,es[i].v=b,es[i].cost=c;
    }
    cout << kruskal() << endl;
  }
  return 0;
}

C++: #include<iostream>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
int main()
{
  int n,m;
  for(;cin>>n>>m,n;)
    {
      int edge[n][n];
      memset(edge,-1,sizeof(edge));
      for(int i=0;i<m;i++)
	{
	  int a,b,cost;
	  cin>>a>>b>>cost;
	  edge[a][b]=cost;
	  edge[b][a]=cost;
	}
      bool closed[n];
      memset(closed,false,sizeof(closed));
      int cnt=n-1;
      int ans=0;
      closed[0]=true;
      priority_queue<pair<int,int>, vector<pair<int,int> > ,greater<pair<int,int> > > qu;
      for(int i=0;i<n;i++)
	{
	  if(edge[0][i]!=-1)
	    qu.push(make_pair(edge[0][i],i));
	}
      while(true)
	{
	  int pos=qu.top().second;
	  int cost=qu.top().first;
	  //  cout<<"#"<<pos<<" "<<cost<<endl;
	  qu.pop();
	  if(closed[pos])
	    continue;
	  closed[pos]=true;
	  ans+=cost;
	  cnt--;
	  if(cnt<=0)
	    break;
	  for(int i=0;i<n;i++)
	    {
	      if(edge[pos][i]!=-1)
		qu.push(make_pair(edge[pos][i],i));
	    }
	}
      cout<<ans<<endl;
    }

}

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

#define N 101

using namespace std;

struct Node{
  vector<int> cons;
  vector<int> costs;
};

struct State{
  int id;
  int cost;
  int sz;
  bool vis[N];
  State():id(0),cost(0),sz(0){
    fill(vis,vis+N,false);
    vis[0]=true;
  }
  State(int id, int cst, int sz):id(id),cost(cst),sz(sz){
    fill(vis,vis+N,false);
    vis[id]=true;
  }
  bool isMST(){
    for(int i = 0; i < sz; ++i){
      if( !vis[i] ) return false;
    }
    return true;
  }
  bool operator>(const State &t)const{
    return cost < t.cost;
  }
};

const int infty = 1<<28;
struct Edge{
  int src,dst;
  Edge():src(0),dst(0){}
  Edge(int s,int d):src(s),dst(d){}
};

int Prim(const vector<Node> &G){
  int ret = 0;
  bool inMST[G.size()];
  vector< Node > MST;
  vector< Edge > MSTE;

  fill( inMST, inMST+G.size(), false );
  MST.push_back( G[0] );
  inMST[0]=true;
  while( MST.size() < G.size() ){
    int from_id = 0;
    int next_id = 0;
    int mincost = infty;

    for(int i = 0; i < MST.size(); ++i){
      for(int j = 0; j < MST[i].cons.size(); ++j){
	if( !inMST[ MST[i].cons[j] ] &&
	    mincost > MST[i].costs[j] ){
	  from_id = i;
	  next_id = MST[i].cons[j];
	  mincost = MST[i].costs[j];
	}
      }
    }
    //cout << "FROM : " << from_id << " NEXT : " << next_id << " COST : " << mincost << endl;

    inMST[ next_id ] = true;
    ret += mincost;
    MST.push_back( G[next_id] );
    MSTE.push_back( Edge(from_id, next_id) );
  }

  /*
  for(int i = 0; i < MSTE.size(); ++i){
    cout << MSTE[i].src << " -> " << MSTE[i].dst << endl;
  }
  */

  return ret;
}

int main(){
  while(true){
    int n,m;
    vector<Node> G;
    cin >> n >> m;
    if( n == 0 && m == 0 ) break;
    G.resize(n);
    for(int i = 0; i < m; ++i){
      int a,b,cost;
      cin>>a>>b>>cost;
      G[a].cons.push_back( b );
      G[a].costs.push_back( cost );
      G[b].cons.push_back( a );
      G[b].costs.push_back( cost );
    }
    cout << Prim(G) << endl;
  }
  return 0;
}