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 <iostream>
#include <vector>
#include <queue>
using namespace std;
	
typedef int Weight;
struct Edge {  //src:辺の始点,dst:辺の終点,weight:辺の重さ
  int src, dst;
  Weight weight;
  Edge(int src, int dst, Weight weight) :
    src(src), dst(dst), weight(weight) { }
};
bool operator < (const Edge &e, const Edge &f) {
  return e.weight != f.weight ? e.weight > f.weight : //辺は重さが重いものを"小さい"と定義する
    e.src != f.src ? e.src < f.src : e.dst < f.dst;
}
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;
 
//引数 g:隣接リスト,r:最小全域木の始点
//戻値 total:最小全域木の重み,T:最小全域木の辺集合
pair<Weight, Edges> prim(const Graph &g, int r = 0) {
  int n = g.size();
  Edges T;
  Weight total = 0;
  vector<bool> visited(n);
  priority_queue<Edge> Q;
  Q.push( Edge(-1, r, 0) );    //始め、-1とrを結ぶ辺があると考える
  while (!Q.empty()) {
    Edge e = Q.top(); Q.pop();
    if (visited[e.dst]) continue;
    T.push_back(e);
    total += e.weight;
    visited[e.dst] = true;
    for(auto f=g[e.dst].begin();f!=g[e.dst].end();++f) if(!visited[f->dst]) Q.push(*f);
  }
  return pair<Weight, Edges>(total, T);
}

//-----------------
int main()
{ 
    while(true){
        int i, a, b, n, m;
        Weight c;
        cin >> n >> m;
        if((n==0)&(m==0))break;
        Graph g(n);    //頂点数vの空隣接リストgを生成
        for(i=0; i<m; i++){
            cin >> a >> b >> c;    
            g[a].push_back(Edge(a, b, c));    //隣接リストgにsからtに向かう重さwの辺を追加
            g[b].push_back(Edge(b, a, c));
        }
        pair<Weight,Edges>ans=prim(g);
        cout << ans.first << endl;
    }
  
    return 0;
}

C++: // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0180

#include <stdio.h>
#include <algorithm>

int A[10010], B[10010];
std::pair<int, int> bridge[10010];

int parent[110]; //代表元

int root(int a) {
	if(parent[a] == a) { return a; }
	parent[a] = root(parent[a]);
	return parent[a];
}

int main(void) {
	int n, m;
	
	while(true) {
		scanf("%d %d", &n, &m);
		if ( n == 0 ) { break; }
		
		for(int i=0; i<n; i++) {
			parent[i] = i;
		}
		
		for(int i=0; i<m; i++) {
			bridge[i].second = i;
			scanf("%d %d %d", &A[i], &B[i], &(bridge[i].first));
		}
		
		sort(bridge, bridge+m);
		
		int sum = 0;
		for(int i=0; i<m; i++) {
			int bid = bridge[i].second;
			if ( root(A[bid]) == root(B[bid]) ) { continue; } //既に連結
			
			sum += bridge[i].first;
			parent[root(A[bid])] = root(B[bid]);
		}
		
		printf("%d\n", sum);
	}
}

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

using namespace std;

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

#define f first
#define s second

class E{
	public:
	int a,b,v;
	E(int a,int b,int v):a(a),b(b),v(v){}
	bool operator<(const E&a)const{
		return v<a.v;
	}
};


class T{
	public:
	int bef;
	vector<int> ko;
};

T uf[111];


void ufto(int a,int b){
	uf[b].bef = a;
	uf[a].ko.push_back(b);
	
	rep(i,uf[b].ko.size()){
		ufto(a,uf[b].ko[i]);
	}
	uf[b].ko.clear();
}

int find(int a){
	return uf[a].bef;
}
void unit(int a,int b){
	ufto(find(a),find(b));
}
int check(int a,int b){
	return find(a)==find(b);
}

int main(){
	while(1){
		rep(i,111){
			uf[i].bef=i;
			uf[i].ko.clear();
		}
		int n,m;
		cin>>n>>m;
		
		if(n==0)break;
		
		vector<E> ed;
		
		rep(i,m){
			int a,b,c;
			cin>>a>>b>>c;
			
			ed.push_back(E(a,b,c));
		}
		
		
		sort(ed.begin(),ed.end());
		
		
		int ans=0;
		rep(i,m){
			if(check(ed[i].a,ed[i].b)==0){
				ans+=ed[i].v;
				unit(ed[i].a,ed[i].b);
			}
		}
		
		printf("%d\n",ans);
	}
}
/*
5 6
0 2 1
2 1 3
2 3 8
1 3 2
3 4 5
1 4 4

*/

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

#define MAX_N 10000

int par[MAX_N];
int rank[MAX_N];

#define MAX_E 100
struct edge { int u, v, cost; };
edge es[MAX_E];

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

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() {

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