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>

struct node {
    int weight;
    int to_id;

    node(int t, int w) {
        this->weight = w;
        this->to_id = t;
    }
    bool operator>(node const &rhs) const {
        if (this->weight > rhs.weight) { return true; }
        else if (this->weight < rhs.weight) { return false; }
        if (this->to_id > rhs.to_id) { return true; }
        else if (this->to_id < rhs.to_id) { return false; }
        return false;
    }
};

// Graph:
typedef std::vector<std::vector<node>> Graph;

int prim(Graph const &input) {
    std::vector<bool> used(input.size(), false);
    int used_count = 0;
    int total_weight = 0;
    std::priority_queue<node, std::vector<node>, std::greater<node>> que;

    used[0] = true; used_count++;
    for (auto const &e: input[0]) que.push(e);
    while (used_count < input.size()) {
        // std::cout << que.size() << "; " << used_count << std::endl;
        assert(!que.empty());
        node s = que.top();
        que.pop();
        if (used[s.to_id]) continue;
        // std::cout << "marked " << s.to_id << " as used." << std::endl;
        used[s.to_id] = true; used_count++;
        total_weight += s.weight;
        for (auto const &e: input[s.to_id]) que.push(e);
    }
    return total_weight;
}

int main(void)
{
    int n, m;
    while ( std::cin >> n >> m, n) {
        Graph g(n);
        for (int i = 0; i < m; i++) {
            int f, t, c;
            std::cin >> f >> t >> c;
            g[f].emplace_back(t, c);
            g[t].emplace_back(f, c);
        }

        std::cout << prim(g) << std::endl;
    }
    
    return 0;
}

C++: //14
#include<iostream>
#include<algorithm>
#include<numeric>

using namespace std;

int main(){
  for(int n,m;cin>>n>>m,n|m;){
    int d[100][100];
    fill_n(d[0],10000,1<<29);
    for(int i=0;i<m;i++){
      int a,b,c;
      cin>>a>>b>>c;
      d[a][b]=d[b][a]=c;
    }
    int ds[100];
    fill_n(ds,100,1<<29);
    ds[0]=0;
    bool u[100]={};
    for(int i=0;i<n-1;i++){
      int m=1<<29;
      int v;
      for(int i=0;i<n;i++){
	if(!u[i]&&ds[i]<m){
	  m=ds[i];
	  v=i;
	}
      }
      u[v]=true;
      for(int i=0;i<n;i++){
	if(!u[i]){
	  ds[i]=min(ds[i],d[v][i]);
	}
      }
    }
    cout<<accumulate(ds,ds+n,0)<<endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

#define MAX_V 150
#define INF 1e9

bool used[MAX_V];
int cost[MAX_V][MAX_V], mincost[MAX_V], V;

int prim(){
  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 E, a, b, c;

  while(cin >> V >> E, (V|E)){
    fill(cost[0],cost[MAX_V],INF);
    for(int i = 0 ; i < E ; i++){
      cin >> a >> b >> c;
      cost[a][b] = cost[b][a] = c;
    }
    cout << prim() << endl;
  }

  return 0;
}

C++: #include <list>
#include <queue>
#include <vector>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
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;
	}
}