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 <algorithm>
#include <iostream>
using namespace std;


// int N, M, A[10010], B[10010], COST[10010];
// pair<int, int> bridge[10010];
int P[10010];//村のrootが保存

void init(int N){
  for(int i=0; i< N; ++i){
    P[i]= i;
  }
}

int root(int a){//aのroot(代表元)を決める
  if(P[a]==a) return a;//aはroot
  return (P[a] = root(P[a]));//aの親のrootを求め,aの親とする
}
bool is_same_set(int a, int b){
  return root(a) == root(b);
}
void unite(int a, int b){
   P[root(a)] = root(b);
}


int main(){
  int N, M, A[10010], B[10010], COST[10010];
  pair<int, int> bridge[10010];

  //root決める
  while(cin >> N >> M  && N>0){
    int allcost = 0;
    int bridgenum = 0;
    init(N);
    for (int i = 0; i < M; ++i)
    {
      cin >> A[i] >> B[i] >> COST[i];
      bridge[i].first = COST[i];
      bridge[i].second = i ;
    }
    sort(bridge,bridge+M);
    for (int i = 0; i < M; ++i)
    {
      int cost = bridge[i].first;
      int a = A[bridge[i].second];
      int b = B[bridge[i].second];

      if (bridgenum == M-1)
      {
        unite(a,b);
      }
      if(is_same_set(a,b)){

      }else{
        unite(a,b);
        allcost += cost;
        // cout <<  allcost << endl;
        bridgenum += 1;
      }


      // 「a から b に cost の橋がかかっている」と表示
    }
    cout <<  allcost << endl;
  }
}

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

#define	mp(a,b)		make_pair(a,b)
#define	mt(a,b,c)	mp(mp(a,b),c)
#define	_1st	first.first
#define	_2nd	first.second
#define	_3rd	second

using namespace std;

typedef	pair<int,int> pii;
typedef pair<pii,int> tiii;

vector<int> city;

int UF_Root(int i)
{
	if (city[i]==-1)	return i;
	return city[i]=UF_Root(city[i]);
}

bool UF_Find(int i,int j)
{
	return UF_Root(i)==UF_Root(j);
}

void UF_Union(int i,int j)
{
	i=UF_Root(i);
	j=UF_Root(j);
	if(i!=j)	city[j]=i;
}

int main()
{
	for(int n,m;cin>>n>>m,n;){
		city=vector<int>(n,-1);

		vector<tiii> data(m);
		while(m--){
			int a,b,c;	cin>>a>>b>>c;
			data[m]=mt(c,a,b);
		}
		sort(data.begin(),data.end());

		int totalcost=0;
		for(int i=0;i<data.size();i++){
			if(!UF_Find(data[i]._2nd,data[i]._3rd)){
				UF_Union(data[i]._2nd,data[i]._3rd);
				totalcost+=data[i]._1st;
			}
		}
		cout<<totalcost<<endl;
	}

	return 0;
}

C++: #include <bits/stdc++.h>

#define reps(v, f, l) for (int v = (f), v##_ = (l); v < v##_; ++v)
#define rep(v, n) reps(v, 0, n)
#define lep(v, n) reps(v, 1, n + 1)

using namespace std;

typedef long long int lint;

static inline int in(){ int x; scanf("%d", &x); return x; }
static inline lint inl(){ lint x; scanf("%lld", &x); return x; }
template<typename T> void show(T& a, char del='\n', char last='\n'){ rep(i, a.size() - 1) cout << a[i] << del; cout << a[a.size() - 1] << last; }

struct Edge {
  int from, to, w;
  Edge(int from, int to, int w) : from(from), to(to), w(w) {}
  Edge(){}
  bool operator<(const Edge& x) const {
    return w != x.w ? w > x.w : from != x.from ? from < x.from : to < x.to;
  }
};

typedef vector<Edge> Edges;
typedef vector<Edges> Graph;

int prim(const Graph& g, int r=0){
  int n = g.size();
  int total = 0;
  vector<bool> visited(n);
  priority_queue<Edge> Q;
  Q.push(Edge(-1, r, 0));
  while (!Q.empty()){
    Edge e = Q.top();
    Q.pop();
    if (visited[e.to]) continue;
    total += e.w;
    visited[e.to] = true;
    for (Edges::const_iterator f = g[e.to].begin(); f != g[e.to].end(); f++){
      if (!visited[f->to]) Q.push(*f);
    }
  }
  return total;
}

int main()
{
  int n, m;
  while (n = in(), m = in(), n){
    Graph g(n);
    rep(i, m){
      int from, to, w;
      from = in();
      to = in();
      w = in();
      g[from].push_back(Edge(from, to, w));
      g[to].push_back(Edge(to, from, w));
    }
    printf("%d\n", prim(g));
  }
  return 0;
}