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++: // 2011/02/15 Tazoe

#include <iostream>
using namespace std;

struct bridge{
	int a;
	int b;
	int c;
	bool chk;
};

int main()
{
	while(true){
		int n, m;
		cin >> n >> m;
		if(n==0&&m==0)
			break;

		struct bridge brd[4950];
		for(int i=0; i<m; i++){
			cin >> brd[i].a >> brd[i].b >> brd[i].c;
			brd[i].chk = false;
		}

		int ttl = 0;
		int num = m;
		while(num>0){
			int i_min = -1;
			int c_min = 200;
			for(int i=0; i<m; i++){
				if(brd[i].chk)
					continue;
				if(brd[i].c<c_min){
					i_min = i;
					c_min = brd[i].c;
				}
			}

			ttl += brd[i_min].c;
			int n_fr = brd[i_min].a;
			int n_to = brd[i_min].b;
			brd[i_min].chk = true;
			num--;

			for(int i=0; i<m; i++){
				if(brd[i].chk)
					continue;
				if(brd[i].a==n_fr)
					brd[i].a = n_to;
				else if(brd[i].b==n_fr)
					brd[i].b = n_to;
				else
					continue;
				if(brd[i].a==brd[i].b){
					brd[i].chk = true;
					num--;
				}
			}
		}

		cout << ttl << endl;
	}

	return 0;
}

C++: #include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
struct Bri{
	int fn;
	int tn;
	int cost;
	bool operator <(const Bri &bBri)const{
		return cost>bBri.cost;
	}
};
int main(void){
	while (true){
		int citynum, brinum;
		cin >> citynum >> brinum;
		if (citynum == 0 && brinum == 0)break;
		int CostCol[100][100];
		for (int i = 0; i < 100; ++i){
			for (int j = 0; j < 100; ++j){
				CostCol[i][j] = 10000000;
			}
		}
		vector<int> IsSelected;
		for (int i = 0; i < citynum; ++i){
			IsSelected.push_back(0);
		}
		vector<Bri> BriQue;
		for (int i = 0; i < brinum; ++i){
			int a, b, cost;
			cin >> a >> b >> cost;
			BriQue.push_back({ a, b, cost });
		}
		sort(BriQue.begin(), BriQue.end());
		reverse(BriQue.begin(),BriQue.end());
		int CostSum=0;
		IsSelected[BriQue[0].fn] = 1; IsSelected[BriQue[0].tn] = 1;
		CostSum += BriQue[0].cost; 
		while (1){
			for (auto it = BriQue.begin(); it != BriQue.end(); ++it){
				if (IsSelected[(*it).fn] + IsSelected[(*it).tn] == 1){
					IsSelected[(*it).fn] = 1;
					IsSelected[(*it).tn] = 1;
					CostSum += (*it).cost;
					break;
				}
				else{

				}
			}
			if (all_of(IsSelected.begin(), IsSelected.end(), [](int x){return x == 1; })){
				cout << CostSum << endl;
				break;
			}
		}
	}
	return 0;
}

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

int n,m,sum;
int P[110],a[10010],b[10010],cost[10010];
pair<int,int> bridge[10010];
void init(int N) {
	for(int i=0; i<N; ++i) P[i] = i;
}
int root(int a) {
	if(P[a] == a) return a;
	return (P[a] = root(P[a]));
}

int main() {
	cin >> n >> m;
	while(n!=0 && m!=0) {
		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);
		init(n);
		for(int i=0; i<m; ++i) {
			if(P[b[bridge[i].second]] == b[bridge[i].second]) {
				if(P[a[bridge[i].second]] != b[bridge[i].second]) {
					P[b[bridge[i].second]] = a[bridge[i].second];
					root(b[bridge[i].second]);
					sum += bridge[i].first;
				}
			}
		}
		cout << sum << endl;
		cin >> n >> m;
		sum =0;
	}
}

C++: #include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cmath>
#include <queue>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1<<30;

typedef int Weight;
struct Edge {
  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 : // !!INVERSE!!
    e.src != f.src ? e.src < f.src : e.dst < f.dst;
}

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

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) );
  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(f, g[e.dst]) if (!visited[f->dst]) Q.push(*f);
  }
  return pair<Weight, Edges>(total, T);
}

int main() {
  int n, m;
  while(cin>>n>>m, n||m) {
    Graph g(n);
    while(m--) {
      int a, b, cost;
      cin >> a>> b >> cost;
      g[a].push_back(Edge(a,b,cost));
      g[b].push_back(Edge(b,a,cost));
    }
    cout << prim(g).first << endl;
  }
}