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 ostringstream OSS;
typedef istringstream ISS;

typedef long long LL;
typedef pair<int, int> PII;

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef vector<string> VS;
typedef vector<VS> VVS;
typedef vector<bool> VB;
typedef vector<VB> VVB;

typedef vector<PII> VPII;

#define fst first
#define snd second
#define MP make_pair
#define PB push_back
#define ALL(x) (x).begin(),(x).end()
#define RANGE(x,y,maxX,maxY) (0 <= (x) && 0 <= (y) && (x) < (maxX) && (y) < (maxY))
#define DUMP( x ) cerr << #x << " = " << ( x ) << endl

template < typename T > inline T fromString(const string &s) { T res; ISS iss(s); iss >> res; return res; };
template < typename T > inline string toString(const T &a) { OSS oss; oss << a; return oss.str(); };

const int INF = 0x3f3f3f3f;
const LL INFL = 0x3f3f3f3f3f3f3f3fLL;
const int DX[]={1,0,-1,0},DY[]={0,-1,0,1};

/***** Union-Find *****/

const int MAX_N = 10000;
int Par[MAX_N];
int Rank[MAX_N];

void union_find_init(int n){
	for(int i=0; i<n; ++i){
		Par[i] = i;
		Rank[i] = 0;
	}
}

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

void unite(int x, int y){
	x = find(x);
	y = find(y);

	if(x == y)
		return ;

	if(Rank[x] < Rank[y]){
		Par[x] = y;
	} else{
		Par[y] = x;
		if(Rank[x] == Rank[y]){
			++Rank[x];
		}
	}
}

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

/***** /Union-Find *****/

/***** Kruskal *****/

struct edge {
	int from,
		to,
		cost;
};

bool comp(const edge& e1, const edge& e2){
	return e1.cost < e2.cost;
}

const int MAX_E = 10000;
edge es[MAX_E];
int V, E;

int kruskal(void){
	sort(es, es + E, comp);
	union_find_init(V);

	int res = 0;
	for(int i=0; i<E; ++i){
		edge e = es[i];
		if(!same(e.from, e.to)){
			unite(e.from, e.to);
			res += e.cost;
		}
	}

	return res;
}

/***** /Kruskal *****/

int main(void) {
	while (cin >> V >> E, V) {
		E *= 2;
		for (int i = 0; i < E / 2; i++) {
			int a, b, cost;
			cin >> a >> b >> cost;
			es[i].from = a;
			es[i].to = b;
			es[i].cost = cost;
			es[i].from = b;
			es[i].to = a;
			es[i].cost = cost;
		}

		int res = kruskal();

		cout << res << endl;
	}

	return 0;
}

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

#define INF 1<<20
#define MAX_V 100

bool done[MAX_V];
int d[MAX_V];
int V;
int cost[MAX_V+1][MAX_V+1];

int dijkstra(int s) {
  int total = 0;
  
  fill(done, done+V, false);
  fill(d, d+V, INF);
  
  d[s] = 0;

  while(1) {
    int v = -1;
    for(int u = 0; u < V; u++) {
      if(!done[u] && ( v == -1 || d[u]<d[v] )) {
	v = u;
      }
    }
    if(v == -1) break;

    done[v] = true;

    for(int u = 0; u < V; u++) {
      d[u] = min(d[u], d[v] + cost[v][u]);
    }
  }

  for(int i=0; i<V; i++) {
    total += d[i];
  }
  
  return total;
}

int main() {
  // テ・ツ、ツ姪」ツ?療」ツ?淌」ツつ嘉ヲツ篠・テァツカツ堙」ツ?古」ツ?ェテ」ツ??ゥツδステ・ツクツづ」ツつ津」ツ?陛」ツ?古」ツ??
  
  int m;
  
  while(cin>>V>>m && (V||m)) {
    
    fill(cost[0], cost[MAX_V], INF);
    
    for(int i=0; i<m; i++) {
      int a, b, cst;
      cin >> a >> b >> cst;
      cost[a][a] = cost[b][b] = 0;
      cost[a][b] = cost[b][a] = cst;
    }

    int res = INF;
    
    for(int i=0; i<V; i++) {
      for(int j=0; j<V; j++) {
	if(i  == j) continue;
	// i <-> j テ・ツ、ツ姪」ツ??
	int temp = cost[i][j];
	cost[i][j] = cost[j][i] = INF;
	
	// テ」ツ?ゥテ」ツ?禿」ツ?ォテ」ツつづァツケツ凝」ツ?古」ツつ嘉」ツ?ェテ」ツ??」ツつ津ァツ「ツコティツェツ?
	int all = 0, cnt = 0;
	for(int k=0; k<V; k++) {
	  all ++;
	  if(cost[i][k] == INF) cnt ++;
	}
	if(all == cnt) { goto lpend; }
	
	all = 0, cnt = 0;
	for(int k=0; k<V; k++) {
	  all ++;
	  if(cost[j][k] == INF) cnt ++;
	}
	if(all == cnt) { goto lpend; }

	/*
	for(int k=0; k<V; k++) {
	  int all = 0, cnt = 0;
	  for(int l=0; l<V; l++) {
	    all ++;
	    // テ」ツ?ゥテ」ツ?禿」ツ?ォテ」ツつづァツケツ凝」ツ?古」ツつ嘉」ツ?ェテ」ツ??」ツつ津ァツ「ツコティツェツ?
	    if(cost[k][l]==INF) cnt ++;
	  }
	  if(all == cnt) {
	    cost[i][j] = cost[j][i] = temp;
	    goto bad;
	  }
	}
	*/
	
	for(int I=0; I<V; I++) {
	  res = min(dijkstra(I), res);
	}
	
      lpend:
	cost[i][j] = cost[j][i] = temp;
      }
    }
    cout << res << endl;
  }
  
  return 0;
}