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 <algorithm>

constexpr int MAX_N = 100;

int n, m;

struct edge {
	int from_, to_, cost_;
	edge() = default;
	edge(int from, int to, int cost) : from_(from), to_(to), cost_(cost) {}
	bool operator<(const edge& rhs) {
		return (cost_ == rhs.cost_) ? from_ < rhs.from_ : cost_ < rhs.cost_;
	}
};

std::vector<edge> edges;

class Union_find {
	int par[MAX_N], rank[MAX_N];
public:
	Union_find() {
		for (int i = 0; i < n; ++i) {
			par[i] = i;
			rank[i] = 0;
		}
	}
	int find(int x) {
		if (x == par[x])
			return x;
		return par[x] = find(par[x]);
	}
	bool same(int x, int y) {
		return find(x) == find(y);
	}
	void unite(int x, int y) {
		x = find(x);
		y = find(y);
		if (rank[x] < rank[y])
			std::swap(x, y);
		par[y] = x;
		rank[x] += rank[y];
	}
};

int main() {
	while (1) {
		std::cin >> n >> m;
		if (n + m == 0)
			break;
		edges.clear();
		Union_find uni;
		int a, b, c;
		for (int i = 0; i < m; ++i) {
			std::cin >> a >> b >> c;
			edges.emplace_back(a, b, c);
		}
		std::sort(edges.begin(), edges.end());
		int ans = 0;
		for (int i = 0; i < edges.size(); ++i) {
			if (!uni.same(edges[i].from_, edges[i].to_)) {
				uni.unite(edges[i].from_, edges[i].to_);
				ans += edges[i].cost_;
			}
		}
		std::cout << ans << std::endl;
	}
	return 0;
}

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

#define N 101

using namespace std;

const int infty = 1<<28;

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

struct Edge{
  int src,dst;
  Edge():src(0),dst(0){}
  Edge(int s,int d):src(s),dst(d){}
};

int BruteForceMST(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];
	}
      }
    }

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

  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 << BruteForceMST(G) << endl;
  }
  return 0;
}

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

struct Bridge
{
  int a, b, cost;
};

int n, m, ans, parent[10001];
Bridge bridge[10001];

int bridge_comp(const void *a, const void *b)
{
  return ((Bridge *)a)->cost - ((Bridge *)b)->cost;
}

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

int main()
{
  while((cin >> n >> m), n && m)
  {
    for (int i = 0; i < n; i++)
      parent[i] = i;
    for (int i = 0; i < m; i++)
      cin >> bridge[i].a >> bridge[i].b >> bridge[i].cost;
    qsort(bridge, m, sizeof(Bridge), bridge_comp);
    ans = 0;
    for (int i = 0; i < m; i++)
    {
      if ((n = root(bridge[i].a)) != root(bridge[i].b))
      {
	parent[bridge[i].b] = n;
	ans += bridge[i].cost;
      }
    }
    cout << ans << endl;
  }
  return 0;
}