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 "bits/stdc++.h"
using namespace std;
typedef long long ll;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define MAX_N 200
#define INF 999999999


int edge[MAX_N][MAX_N];

int prim(int n){    //n : ???????????°
    bool used[MAX_N];
    int mincost[MAX_N];
    
    rep(i,n){
        mincost[i]=INF;
        used[i]=false;
    }
    mincost[0]=0;
    int res=0;
    
    while(1){
        int v=-1;
        rep(u,n){
            if(used[u]==false && ( v==-1 || mincost[u]<mincost[v] ))
                v=u;
        }
        if(v==-1)break;
        used[v]=true;
        res+=mincost[v];
        
        rep(u,n){
            mincost[u]=min(mincost[u],edge[v][u]);
        }
    }
    return res;
}

int main(){
    int n,m;
    while(cin>>n>>m){
        if(n==0&&m==0)break;
        
        rep(i,200)rep(j,200)edge[i][j]=INF;
        rep(i,m){
            int a,b,c;
            cin>>a>>b>>c;
            edge[a][b]=edge[b][a]=c;
        }
        cout<<prim(n)<<endl;
    }
}

C++: #include<iostream>
#include<stack>
#include<queue>
#include<map>
#include<string>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cstdio>
using namespace std;

#define rep(i,n) for(int i=0;i<n;i++)
#define REP(n) rep(i,n)

const int maxn=101,maxm = 5010, INF = 10010;
int cost[maxn][maxn];
int mincost[maxn];
bool used[maxn];
int n,m,a,b;

int main()
{
    while(cin >> n >> m &&(n&&m))
    {
        memset(cost,INF,sizeof(cost));
        memset(mincost,INF,sizeof(mincost));
        memset(used,false,sizeof(used));
        mincost[0] = 0;

        REP(m)
        {
            cin >> a >> b;
            cin >> cost[a][b];
            cost[b][a] = cost[a][b];
        }

        int ans = 0;
        while(true)
        {
            int minv = INF;
            REP(n)if(!used[i] &&(minv==INF || mincost[i] < mincost[minv])) minv = i;
            if(minv == INF) break;

            used[minv] = true;
            ans += mincost[minv];
            REP(n)mincost[i] = min(mincost[i], cost[minv][i]);
        }

        cout << ans << endl;
    }

    return 0;
}

C++: #include <iostream>
#include <queue>
#include <cstdio>
#include <cstring>
#include <map>
using namespace std;
typedef pair<int,int>PAIR;//cost/pos
int main(){
	int N,M;
	while(cin>>N>>M,N){
		int ans=0,cnt=0;
		bool flag[100]={0};
		int load[100][100];
		memset(load,-1,sizeof(load));
		for(int i=0,A,B,C;i<M;i++){
			cin>>A>>B>>C;
			load[A][B]=load[B][A]=C;
		}
		priority_queue<PAIR,vector<PAIR>,greater<PAIR> > Q;
		Q.push(make_pair(0,0));
		for(int now,cost;!Q.empty();){
			cost=Q.top().first;
			now=Q.top().second;
			Q.pop();
			if(flag[now]==0){
				flag[now]=1;
				ans+=cost;
				cnt++;
				for(int i=0;i<N;i++)if(load[now][i]>0&&flag[i]==0)Q.push(make_pair(load[now][i],i));
			}
			if(cnt==N)break;
		}
		cout<<ans<<endl;
	}
	
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

struct EDGE
{
	int from, to, cost;
	bool operator < (const EDGE& t) const {
		return cost < t.cost;
	}
};

int par[100];
vector<EDGE> v;

void init()
{
	for (int i = 0; i < 100; i++) par[i] = i;
	v.clear();
}

int find(int x)
{
	if (par[x] == x) return x;
	return par[x] = find(par[x]);
}

void _union(int x, int y)
{
	x = find(x);
	y = find(y);
	if (x == y) return;
	par[x] = y;
}

bool same(int x, int y)
{
	return find(x) == find(y);
}

int main()
{
	int n, m;
	while (scanf("%d %d", &n, &m), n + m){
		init();
		
		for (int i = 0; i < m; i++){
			int a, b, c;
			scanf("%d %d %d", &a, &b, &c);
			v.push_back((EDGE){a, b, c});
			v.push_back((EDGE){b, a, c});
		}
		sort(v.begin(), v.end());
		
		int res = 0;
		for (int i = 0; i < v.size(); i++){
			EDGE& e = v[i];
			if (same(e.from, e.to)) continue;
			_union(e.from, e.to);
			res += e.cost;
		}
		
		printf("%d\n", res);
	}
	
	return 0;
}

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

const int INF = 1000000;
int main(){
	int n,m;
	while(cin >> n >> m){
		if(!n && !m)
			break;
		int bridge[100][100];
		int pre_vertex[100][100];
		int a,b,cost;
		for(int i=0;i<100;i++){
			for(int j=0;j<100;j++){
				if(i==j)
					bridge[i][j] = 0;
				else
					bridge[i][j] = INF;
				pre_vertex[i][j] = i;
			}
		}
		for(int i=0;i<m;i++){
			cin >> a >> b >> cost;
			if(bridge[a][b] > cost){
				bridge[a][b] = cost;
				bridge[b][a] = cost;
			}
		}
		//ツフツδ債イツド
		for(int k=0;k<n;k++){
			for(int i=0;i<n;i++){
				for(int j=0;j<n;j++){
					int dis = bridge[i][k]+bridge[k][j];
					if(dis < bridge[i][j]){
						bridge[i][j] = dis;
						pre_vertex[i][j] = pre_vertex[k][j];
					}	
				}
			}
		}
		int min = INF;
		for(int i=0;i<n;i++){
			int sum = 0;
			for(int j=0;j<n;j++){
				sum += bridge[i][j];
				sum -= bridge[i][pre_vertex[i][j]];
			}
			if(sum < min)
				min = sum;
		}
		cout << min << endl;
	}
	return 0;
}