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

int N = 10010;
int M,A[10010],B[10010],COST[10010];
pair<int,int> bridge[10010]; //コストと橋番号のペア
int P[10010]; //0から10000までの取り扱い可能
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){ //aとbが同じグループに属するか?
    return root(a) == root(b);
	}
  void unite(int a, int b) { //aとbを同一グループにまとめる
    P[root(a)] = root(b);
	}
	
int main(){
  while(cin >> N >> M && (N>0)){
 //(NとMを読み込み,Nが0より大きければ処理する)
    init(N);
	int sum = 0;
    for (int i=0; i<M; ++i) {
	  //A[i]とB[i]とCOST[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];
	//「aからbにcostの橋がかかっている」と表示
	for (int i=0; i<M; ++i){ //安い橋から順に
	if (!is_same_set(a,b)){ //(端の両端の節点が既に連結だったら)
	//端の両端の節点を同一グループに併合する
	unite (a,b);
	//合計に,橋のコストを加える
	sum += cost;
	  }
	 }
	}
	//合計を出力
	cout << sum << endl;
 }
}

C++: #include <iostream>
#include <cmath>

using namespace std;

#define INF 10000000
int cost[110][110];
int mincost[110];
bool used[110];

int prim(int V) {
	for (int i = 0; i < V; i++) {
		mincost[i] = INF;
		used[i] = false;
	}
	mincost[0] = 0;
	int res = 0;

	while (true) {
		int v = -1;
		for (int u = 0; u < V; u++) {
			if (!used[u] && (v == -1 || mincost[u] < mincost[v])) {
				v = u;
			}
		}

		if (v == -1) {
			break;
		}
		used[v] = true;
		res += mincost[v];

		for (int u = 0; u < V; u++) {
			mincost[u] = min(mincost[u], cost[v][u]);
		}
	}
	return res;
}
int main() {
	int n, m;
	while (cin >> n >> m) {
		if (n == 0 &&  m == 0) {
			break;
		}
		for (int i = 0; i < 110; i++) {
			for (int j = 0; j < 110; j++) {
				cost[i][j] = INF;
			}
		}
		int a,b,c;
		for (int i = 0; i < m; i++) {
			cin >> a >> b >> c;
			cost[a][b] = c;
			cost[b][a] = c;
		}
		cout << prim(n) << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <utility>
#include <sstream>
#include <complex>

using namespace std;

#define FOR(i,a,b) for(long long i=(a);i<(b);i++)
#define REP(i,N) for(long long i=0;i<(N);i++)
#define ALL(s) (s).begin(),(s).end()
#define fi first
#define se second

#define PI acos(-1.0)
#define INF 1000000007
#define EPS 1e-10

typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<double, double> PD;
typedef pair<string, ll> PS;
typedef vector<ll> V;
typedef pair<P, char> PC;

set<int> si;

int n, m;
int a, b, cost;
int d[110];
bool used[110];

struct edge{
	int to, cost;
	edge(int _to, int _cost) :to(_to), cost(_cost){};
};

vector<edge> G[110];

void prim(int s){
	fill(d, d + 110, INF);
	REP(i, n)used[i] = false;
	d[s] = 0;
	priority_queue< P,vector<P>,greater<P> > que;
	que.push(P(0, s));

	while (!que.empty()){
		P p = que.top(); que.pop();
		int v = p.second;

		if (used[v])continue;

		used[v] = 1;

		for (int i = 0; i < G[v].size(); i++){
			edge e = G[v][i];
			if (used[e.to])continue;
			if (d[e.to] > e.cost){
				d[e.to] = e.cost;
				que.push(P(d[e.to], e.to));
			}
		}
	}
}

int main(){
	while (cin >> n >> m&&n + m){
		REP(i, m){
			cin >> a >> b >> cost;
			G[a].push_back(edge(b, cost));
			G[b].push_back(edge(a, cost));
		}
		prim(0);
		int ans = 0;
		REP(i, n){
			ans += d[i];
		}
		cout << ans << endl;
		REP(i, n)G[i].clear();
	}
}