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;
typedef pair<int,int> PA;
const int INF = (1<<30)-1;
int main(){
  int n,m,a,b,c,sum,ma,hiku;
  priority_queue<PA,vector<PA>,greater<PA> > pq;
  while(cin >> n >> m,n!=0||m!=0){
    sum = 0;
    vector<PA> vec[100];
    for(int i=0;i<m;i++){
      cin >> a >> b >> c;
      vec[a].push_back(PA(c,b));
      vec[b].push_back(PA(c,a));
      sum += c;
    }
    bool town[100]={false};
    hiku = 0;
    pq.push(PA(0,0));
    while(!pq.empty()){
      PA p = pq.top();
      pq.pop();
      if(town[p.second]) continue;
      
      hiku += p.first;
      town[p.second] = true;
      
      for(int i=0;i<vec[p.second].size();i++){
	int to = vec[p.second][i].second;
	if(!town[to]) pq.push(PA(vec[p.second][i].first,to));
      }
      
    }
    printf("%d\n",hiku);
  }
  return(0);
}

C++: // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0180
// PCK2008yosen-08
// パソコン甲子園・・・チームrand()・・・うっ・・・高校時代も今もあいつらとは目を合わせないように生きてきたんだ・・・

#include <cstdio>
#include <algorithm>
using namespace std;

struct bridge {
	int a;
	int b;
	int cost;
	
	bool operator < ( const bridge& com ) const {
		return (this->cost < com.cost);
	};
};

int root[110];
bridge br[10010];

int getroot(int city) {
	if ( root[city] == city ) {
		return city;
	}
	root[city] = getroot(root[city]);
	return root[city];
}

int main(void) {
	
	while(true) {
		int n, m;
		scanf("%d %d", &n, &m);
		if ( n == 0 ) { return 0; }
		for(int i=0; i<m; i++) {
			scanf("%d %d %d", &br[i].a, &br[i].b, &br[i].cost);
		}
		
		sort(br, br+m);
		
		for(int i=0; i<n; i++) {
			root[i] = i;
		}
		
		int total = 0;
		for(int i=0; i<m; i++) {
			if ( root[br[i].a] == root[br[i].b] ) { continue; }
			
			// 橋を追加
			total += br[i].cost;
			int ra = getroot(br[i].a);
			int rb = getroot(br[i].b);
			root[ra] = rb;
			
			int j;
			for(j=1; j<n; j++) {
				if (getroot(j) != getroot(0)) {
					break;
				}
			}
			/*
			for(int aa=0; aa<n; aa++) {
				printf("%d ", root[aa]);
			}
			printf("\n");
			*/
			if ( j == n ) { break; }
		}
		
		printf("%d\n", total);
	}

}

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

vector<int> city(200,-1);
int getId(int i){
	if(i==city[i]) return i;
	else return getId(city[i]);
}
int main(){
	for(;;){
		int n,m;
		cin>>n>>m;
		if(n+m==0) break;
		priority_queue< pair< int, pair<int,int> >, vector< pair< int, pair<int,int> > >, greater< pair< int, pair<int,int> > > > bridge;
		for(int i=0; i<n; i++){
			city[i]=i;
		}
		for(int x,y,c;m--;){
			cin>>x>>y>>c;
			bridge.push(make_pair(c, make_pair(x,y)));
		}
		int total=0;
		for(;bridge.size();){
			int cost = bridge.top().first;
			pair<int,int> br = bridge.top().second;
			bridge.pop();
			if(getId(br.first) != getId(br.second)){
				city[getId(br.first)] = getId(br.second);
				total += cost;
			}
		}
		cout<<total<<endl;
	}
}

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

struct Edge
{
  int to, cost;
};
bool operator<(const Edge& lht, const Edge& rht)
{
  return lht.cost > rht.cost;
}

#define MN 100

int N, M;
bool done[MN];

int main()
{
loop:
  fill(done, done + MN, false);
  vector<Edge> edges[MN];
  cin >> N >> M;
  if(!N)
    return 0;
  for(int i = 0; i < M; i++)
  {
    int a, b, c;
    cin >> a >> b >> c;
    edges[a].push_back({b, c});
    edges[b].push_back({a, c});
  }

  priority_queue<Edge> q;
  q.push({0, 0});
  int cost = 0;
  while(!q.empty())
  {
    auto e = q.top(); q.pop();
    if(done[e.to])
      continue;
    cost += e.cost;
    done[e.to] = true;
    for(auto ee: edges[e.to])
      q.push(ee);
  }
  cout << cost << endl;
  goto loop;
}

C++: #include<cstdio>
#include<iostream>
#include<string>
#include<map>
#include<queue>
#include<set>

using namespace std;

#define rep(i,n) for(int i=0;i<n;i++)
#define reps(i,n) for(int i=1;i<=n;i++)

class S{
	public:
	int x,now,bef;
	S(int x,int now,int bef):x(x),now(now),bef(bef){}
	bool operator<(const S&a)const{
		return now>a.now;
	}
};

int main(){
	while(1){
		int n,m;
		cin>>n>>m;
		if(n==0)break;
		
		int to[111][111]={0};
		rep(i,m){
			int a,b,c;
			cin>>a>>b>>c;
			
			to[a][b]=c;
			to[b][a]=c;
		}
		
		priority_queue<S> que;
		que.push(S(0,0,-1));
		
		int ans=0;
		
		int visit[111]={0};
		while(1){
			if(que.empty())break;
			
			S u = que.top();
			que.pop();
			
			if(visit[u.x]==1)continue;
			visit[u.x]=1;
			
			if(u.bef!=-1){
				ans += to[u.x][u.bef];
			}
			
			rep(i,n){
				if(to[u.x][i]==0)continue;
				que.push( S(i ,to[u.x][i]+u.now ,u.x) );
			}
		}
		printf("%d\n",ans);
	}
}