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;
class Graph {
private:
  const static int INF = 1000000; 
  std::vector<std::vector<int> > edge_;
  unsigned int size_;
public:
  Graph(unsigned int size);
  Graph& connect(int from, int to, int weight);
  std::vector<int> neighbours(int node) const;
  unsigned int size() const;
};

const int Graph::INF;

Graph::Graph(unsigned int size) {
  size_ = size;
  edge_ = std::vector<std::vector<int> > (size_, std::vector<int>(size_, INF));
}

Graph& Graph::connect(int from, int to, int weight = 1) {
  edge_[from][to] = weight;
  return *this;
}

std::vector<int> Graph::neighbours(int node) const {
  std::vector<int> neighbours;
  for (int i = 0; i < static_cast<int>(size_); ++i)
    if (edge_[node][i] < INF) neighbours.push_back(i);

  return neighbours;
}

unsigned int Graph::size() const {
  return size_;
}


Graph creat_graph(int n, int m);
int search_prim(Graph prim, int n);

vector<int> ans;
vector<vector<int>> cost(100,vector<int>(100,0));

int main(){
  int n,m;
 
  
  while(cin >> n >> m, n){     
    Graph prim = creat_graph(n,m);   /*creat graph*/
    ans.push_back(search_prim(prim,n));   /*make answer*/
  }
  
  for(int i: ans){
    cout << i << endl;
  }
  return 0;
}


 Graph creat_graph(int n,int m){
    Graph prim(n);
    int a,b,cost_t;

    for(int i=0; i<m; i++){
      cin >> a >> b >> cost_t;
      prim.connect(a,b);
      cost[a][b] = cost_t;
      cost[b][a] = cost_t;
    }
    return prim;
  }
    
int search_prim(Graph prim, int n){
  int current,cost_sum;
  typedef pair<int, int> PII;
  vector<bool> frag(n,false);
  priority_queue < PII, vector < pair <int, int> >, greater < pair <int, int> > > que;
  /*cost,next_node*/
  
  cost_sum = 0;
  current = 0;
  frag[0] = true;
  for(int k=0; k<n-1; ++k){
     vector<int> near = prim.neighbours(current);

     for(int j:near){
       que.push(PII(cost[current][j],j));
     }

     while(1){
       auto top = que.top();
       que.pop();
       if(frag[top.second] == false){
	 cost_sum += top.first;
	 frag[top.second] = true;
	 current = top.second;
	 break;
	 }
     }  
  }
  return cost_sum;
}

C++: #include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <climits>
#include <cfloat>
using namespace std;

class Edge
{
public:
    int to, cost;
    Edge(){};
    Edge(int to0, int cost0){to = to0; cost = cost0;}
};

long long minSpanningTree(const vector<vector<Edge> >& edges)
{
    int n = edges.size();
    vector<bool> used(n, false);
    int usedNum = 0;
    vector<int> minCost(n, INT_MAX);
    priority_queue<pair<int,int> ,vector<pair<int,int> >, greater<pair<int,int> > > q;
    q.push(make_pair(0, 0));

    long long ret = 0;
    while(!q.empty()){
        pair<int, int> p = q.top();
        q.pop();
        int v = p.second;
        if(used[v])
            continue;
        ret += p.first;
        used[v] = true;
        if(++usedNum == n)
            return ret;
        for(unsigned i=0; i<edges[v].size(); ++i){
            Edge e = edges[v][i];
            if(!used[e.to] && e.cost < minCost[e.to]){
                minCost[e.to] = e.cost;
                q.push(make_pair(e.cost, e.to));
            }
        }
    }
    return -1;
}

int main()
{
    for(;;){
        int n, m;
        cin >> n >> m;
        if(n == 0)
            return 0;

        vector<vector<Edge> > edges(n);
        for(int i=0; i<m; ++i){
            int a, b, cost;
            cin >> a >> b >> cost;
            edges[a].push_back(Edge(b, cost));
            edges[b].push_back(Edge(a, cost));
        }

        cout << minSpanningTree(edges) << endl;
    }
}

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

const int MAX_N = 10000;
int Par[MAX_N];
int Rank[MAX_N];

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);
}

struct edge {
	int from,
		to,
		cost;
};

bool comp(const edge& e1, const edge& e2){
	return e1.cost < e2.cost;
}

const int MAX_E = 10000;
edge es[MAX_E];
int V, E;

int kruskal(void){
	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.from, e.to)){
			unite(e.from, e.to);
			res += e.cost;
		}
	}

	return res;
}

int main(void){
	while(true){
		cin >> V >> E;
		if( V == 0 && E == 0 )
			break;
		for(int i=0; i<E; i++){
			cin >> es[i].from >> es[i].to >> es[i].cost;
		}

		cout << kruskal() << endl;
	}
}