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 <list>
#include <queue>
#include <vector>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
bool used[100]; long long dist[100];
long long prim(int V, vector<pair<int, long long> > X[]) {
	priority_queue<pair<long long, int>, vector<pair<long long, int> >, greater<pair<long long, int> > > Q;
	for (int i = 0; i < V; i++) dist[i] = INF, used[i] = false;
	dist[0] = 0; Q.push(make_pair(0, 0));
	while (!Q.empty()) {
		pair<int, long long> pa = Q.top(); Q.pop();
		long long a1 = pa.first, a2 = pa.second; used[a2] = true;
		for (int i = 0; i < X[a2].size(); i++) {
			int to = X[a2][i].first; long long leng = X[a2][i].second;
			if (dist[to] > leng && !used[to]) {
				dist[to] = leng; Q.push(make_pair(dist[to], to));
			}
		}
	}
	long long sum = 0;
	for (int i = 0; i < V; i++) sum += dist[i];
	return sum;
}
int n, m, u, v, w; vector<pair<int, long long> > G[100];
int main() {
	while(cin >> n >> m, n) {
		for(int i = 0; i < n; i++) G[i].clear();
		for(int i = 0; i < m; i++) {
			cin >> u >> v >> w;
			G[u].push_back(make_pair(v, w));
			G[v].push_back(make_pair(u, w));
		}
		cout << prim(n, G) << endl;
	}
}

C++: #include <iostream>
#include <vector>
#include <queue>
using namespace std;
const int INF = (1<<21);

class State{
public:
  int d,n;
  bool f;
  State(int d = 0, int n = 0, bool f = false):d(d),n(n),f(f){}
  bool operator < (const State &s) const {return d > s.d;}
};

int Dijkstra(int start/*, int goal*/, vector<vector<int> > Graph){

  int size = Graph.size();
  vector<State> d(size);
  priority_queue<State> Q;
  State u,v;

  for(int i=0;i<size;i++){
    d[i].d = INF;
    d[i].f = false;
    }
  u.d = d[start].d = 0;
  u.n = start;
  Q.push(u);

  while(!Q.empty()){
    u = Q.top();
    Q.pop();

    if(d[u.n].f) continue;
    d[u.n].f = true;

    for(int i=0;i<size;i++){
      if(!d[i].f && /*d[u.n].d +*/ Graph[u.n][i] < d[i].d){
	v = u;
	v.d = d[i].d = /*d[u.n].d +*/ Graph[u.n][i];
	v.n = i;
	Q.push(v);
      }
    }
  }
  /*return d[goal].d;*/
  int ans = 0;
  for(int i=0;i<size;i++) ans += d[i].d;
  return ans;

}

int main(){
  int n,m;
  while(cin >> n >> m && n && m){
    vector<vector<int> > G(n);
    for(int i=0;i<n;i++){
      vector<int> v(n);
      v.assign(n,INF);
      G[i] = v;
    }
    for(int i=0;i<m;i++){
      int a,b,c;
      cin >> a >> b >> c;
      G[a][b] = G[b][a] = c;
    }
    cout << Dijkstra(0,G) << endl;
  }
  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define r(i,n) for(int i=0;i<n;i++)
#define INF 1<<27
int n,cost[110][110],mincost[110];
bool used[110];
int prim(){
  r(i,n)mincost[i]=INF,used[i]=false;
  mincost[0]=0;
  int res=0;
  while(1){
    int v=-1;
    r(u,n)if(!used[u]&&(v==-1||mincost[u]<mincost[v]))v=u;
    if(v==-1)break;
    used[v]=1;
    res+=mincost[v];
    r(u,n)mincost[u]=min(mincost[u],cost[v][u]);
  }
return res;
}
int main(){
  int m,q,w,e;
  while(cin>>n>>m,n){
  r(i,n)r(j,n)cost[i][j]=INF;
  while(m--)cin>>q>>w>>e,cost[w][q]=cost[q][w]=e;
  cout<<prim()<<endl;
}
}

C++: #include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <utility>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>

using namespace std;

#define rep(i,n) for(int i=0; i<(n); i++)
#define repc(i,s,e) for(int i=(s); i<(e); i++)
#define pb(n) push_back((n))
#define mp(n,m) make_pair((n),(m))
#define all(r) r.begin(),r.end()
#define fi first
#define se second

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;




const int INF = 1000000;
const int mod = 1e9 + 7;



const int MAX_E = 300100;
const int MAX_V = 100100;
 
int par_uft[MAX_V];
int rank_uft[MAX_V];
 
void init_uft(int n) {
	int i;
	for (i = 0; i < n; i++) {
		par_uft[i] = i;
		rank_uft[i] = 0;
	}
}
 
int find_uft(int x) {
	if (par_uft[x] == x) {
		return x;
	}
	else {
		return par_uft[x] = find_uft(par_uft[x]);
	}
}
 
void unite_uft(int x, int y) {
	x = find_uft(x);
	y = find_uft(y);
	if (x == y)return;
 
	if (rank_uft[x] < rank_uft[y]) {
		par_uft[x] = y;
	}
	else {
		par_uft[y] = x;
		if (rank_uft[x] == rank_uft[y]) {
			rank_uft[x]++;
		}
	}
}
 
bool same_uft(int x, int y) {
	return find_uft(x) == find_uft(y);
}
 
 
 
struct edge { 
	int u, v, cost;
	edge(){}
	edge(int u,int v,int cost):u(u),v(v),cost(cost){}
};
 
bool comp(const edge& e1, const edge& e2) {
	return e1.cost < e2.cost;
}
 
edge es[MAX_E];
int V, E;
 
ll kruskal() {
	sort(es, es + E, comp);
	init_uft(V);
	ll res = 0LL;
	for (int i = 0; i < E; i++) {
		edge e = es[i];
		if (!same_uft(e.u, e.v)) {
			unite_uft(e.u, e.v);
			res += e.cost;
		}
	}
	return res;
}



int main(){
	int n,m;
	while(cin>>n>>m && n>0 ) {
		V=n; E=m*2;
		int a,b,c;
		for(int i=0; i<m; i++){
			cin>>a>>b>>c;
			es[i*2] =edge(a,b,c);
			es[i*2+1] = edge(b,a,c);
		}
		cout<<kruskal()<<endl;
	}
}