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<cstdio>
#include<iostream>
using namespace std;
int main(void){
	int n,m,a[501],b[501],cost[501],i,j,temp,root[101],size[101],x,y,sum;
	while(1){
		cin>>n>>m;
		if(n==0) break;
		for(i=0;i<n;i++){
			size[i]=1;
			root[i]=i;
		}
		for(i=0;i<m;i++) cin>>a[i]>>b[i]>>cost[i];
		for(i=0;i<m-1;i++){
			for(j=i+1;j<m;j++){
				if(cost[i]>cost[j]){
					temp=cost[i]; cost[i]=cost[j]; cost[j]=temp;
					temp=a[i]; a[i]=a[j]; a[j]=temp;
					temp=b[i]; b[i]=b[j]; b[j]=temp;
				}
			}
		}
//		for(i=0;i<m;i++)cout<<cost[i]<<endl;
		sum=0;
		for(i=0;i<m;i++){
			x=a[i]; y=b[i];
			while(root[x]!=x) x=root[x];
			while(root[y]!=y) y=root[y];
			if(x!=y){
				sum+=cost[i];
				if(size[x]>size[y]) root[y]=x;
				else if(size[x]<size[y]) root[x]=y;
				else {
					root[x]=y;
					size[y]++;
				}
			}
		}
		cout<<sum<<endl;
	}
	return 0;
}

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

typedef pair<int, int> P;

int main()
{
	int n, m, a, b, cost;
	while (cin >> n >> m, n || m) {
		vector< queue< P > > bridge(n);
		vector<int> group(n,0);
		for (int i = 0; i < m; i++) {
			cin >> a >> b >> cost;
			bridge[a].push(P(-cost,-b));
			bridge[b].push(P(-cost,-a));
		}
		group[0] = 1;
		priority_queue< P > pq;
		int ncost = 0;
		while (!bridge[0].empty()) {
			pq.push(bridge[0].front());
			bridge[0].pop();
		}
		for (int i = 1; i < n; i++) {
			while (group[-pq.top().second]) {
				pq.pop();
			}
			P tmp = pq.top();
			pq.pop();
			group[-tmp.second] = 1;
			ncost -= tmp.first;
			while (!bridge[-tmp.second].empty()) {
				pq.push(bridge[-tmp.second].front());
				bridge[-tmp.second].pop();
			}
		}
		cout << ncost << endl;
	}
	return 0;
}

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

int use[100],m,n,c;
struct poi { int n,f[100];};
struct bri { int a,b,c; };
bri b[5000],g;
poi p[100];
void check(int v) {
	int i; c++;
	for (i=0;i<p[v].n;i++) if (p[v].f[i]>-1) if (use[p[v].f[i]]==0) {use[p[v].f[i]]=1; check(p[v].f[i]);}
	}

int main() {
	int i,j,s,x,y;
    while(cin >> n >> m) {
		if (n==0) break;
		for (i=0;i<100;i++) p[i].n=0;
		for (s=0,i=0;i<m;i++) { cin >> b[i].a >> b[i].b >> b[i].c;
		 p[b[i].a].f[p[b[i].a].n]=b[i].b;  p[b[i].a].n++;
		 p[b[i].b].f[p[b[i].b].n]=b[i].a;  p[b[i].b].n++;  
		 s+=b[i].c;}
		for (i=m-1;i>0;i--) for (j=0;j<i;j++) if (b[j].c<b[j+1].c) { g=b[j]; b[j]=b[j+1]; b[j+1]=g;}
		for (i=0;i<m;i++) {
			for (j=0;j<p[b[i].a].n;j++) if (p[b[i].a].f[j]==b[i].b) { x=j; p[b[i].a].f[j]=-1; }
			for (j=0;j<p[b[i].b].n;j++) if (p[b[i].b].f[j]==b[i].a) { y=j; p[b[i].b].f[j]=-1; }
			for (j=0;j<n;j++) use[j]=0; 
			use[0]=1; c=0; check(0); 
			if (c==n) s-=b[i].c; else { p[b[i].a].f[x]=b[i].b; p[b[i].b].f[y]=b[i].a;}
		}  
		cout << s << endl;
	}
	return 0;
}

C++: #include<cstdio>
#include<iostream>
#include<string>
#include<map>
#include<queue>
#include<set>

using namespace std;

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

class S{
	public:
	int x,now,bef;
	S(int x,int now,int bef):x(x),now(now),bef(bef){}
	bool operator<(const S&a)const{
		return now>a.now;
	}
};

int main(){
	while(1){
		int n,m;
		cin>>n>>m;
		if(n==0)break;
		
		int to[111][111]={0};
		rep(i,m){
			int a,b,c;
			cin>>a>>b>>c;
			
			to[a][b]=c;
			to[b][a]=c;
		}
		
		priority_queue<S> que;
		que.push(S(0,0,-1));
		
		int ans=0;
		
		int visit[111]={0};
		while(1){
			if(que.empty())break;
			
			S u = que.top();
			que.pop();
			
			if(visit[u.x]==1)continue;
			if(visit[u.bef]==1)continue;
			visit[u.x]=1;
			
			if(u.bef!=-1){
				ans += to[u.x][u.bef];
			}
			
			rep(i,n){
				if(to[u.x][i]==0)continue;
				if(visit[i]==1)continue;
				que.push( S(i ,to[u.x][i]+u.now ,u.x) );
			}
		}
		printf("%d\n",ans);
	}
}

C++: #include <iostream>
#include <utility>
#include <queue>
#include <vector>
#include <cstring>
#include <functional>
using namespace std;
typedef pair<int,int> P;

const int MAX = 100,INF = 1000000;
int edge[MAX][MAX];

bool check(int a[],const int n){
	for(int i = 1;i < n;++i){
		if(a[i - 1] != a[i]) return true;
	}
	return false;
}

int prim(int n){
	priority_queue<P,vector<P>,greater<P> > pq;
	int done[n],cost;
	P tmp;
	memset(done,0,sizeof(done));
	for(int i = 0;i < n;++i){
		if(edge[0][i] != INF) pq.push(make_pair(edge[0][i],i));
	}
	done[0] = 1;
	cost = 0;
	while(check(done,n) && !pq.empty()){
		tmp = pq.top();
		pq.pop();
		if(!done[tmp.second]){
			cost += tmp.first;
			done[tmp.second] = 1;
			for(int i = 0;i < n;++i){
				if(edge[tmp.second][i] != INF) pq.push(make_pair(edge[tmp.second][i],i));
			}
		}
	}
	return cost;
}

int main(){
	int n,m,a,b,cost;
	while(cin >> n >> m,n){
		for(int i = 0;i < MAX;++i){
			for(int j = 0;j < MAX;++j){
				edge[i][j] = MAX;
			}
		}
		for(int i = 0;i < m;++i){
			cin >> a >> b >> cost;
			edge[a][b] = edge[b][a] = cost;
		}
		cout << prim(n) << endl;
	}
	return 0;
}