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 <iostream>
using namespace std;
int n,m;
int mp[101][101];

int check() {
  int flg[101] ={};
  int dis[101];
  int sum = 0;
  for(int i=0;i<n;i++) dis[i] = 1000000000;
  dis[0]  = 0;

  for(int k=0;k<n;k++) {
    int mincost = 1000000000;
    int ima;
    for(int i=0;i<n;i++) {
      if(mincost > dis[i] && flg[i] == 0) {
	mincost = dis[i];
	ima = i;
      }
    }
    sum += mincost;
    flg[ima] = 1;
    // cout << mincost<<endl;;
    
    for(int i=0;i<n;i++) 
      if(mp[ima][i] < dis[i] && flg[i] == 0 )
	dis[i] = mp[ima][i];
  }


  return sum;

}

int main() {
  
  while(1) {
    cin >> n >> m;

    if(n == 0 && m ==0) break;

    for(int i=0;i<n;i++) 
      for(int j=0;j<n;j++)
	mp[i][j] = 1000000000;

    int a,b,cost;
    for(int i=0;i<m;i++){
      cin >> a >> b >> cost;
      mp[a][b] = cost;
      mp[b][a] = cost;
    }
    
    
    int ans = check();
    
    cout << ans << endl;
    
  }

  return 0;
}

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

vector<int> uni;
int find(int at){
    if(at==uni[at]) return at;
    else return uni[at]=find(uni[at]);
}
void unite(int a,int b){
    uni[find(a)]=find(b);
}

int main(){
    for(;;){
        int n,m;
        scanf("%d%d",&n,&m);
        if(n==0&&m==0) break;
        vector<pair<int,pair<int,int> > > costs;
        uni=vector<int>(n);
        for(int i=0;i<n;i++){
            uni[i]=i;
        }
        for(int i=0;i<m;i++){
            int a,b,c;
            scanf("%d%d%d",&a,&b,&c);
            costs.push_back(make_pair(c,make_pair(a,b)));
        }
        sort(costs.begin(),costs.end());
        int ret=0;
        for(int i=0;i<costs.size();i++){
            int a=costs[i].second.first,b=costs[i].second.second,co=costs[i].first;
            if(find(a)!=find(b)){
                ret+=co;
                unite(a,b);
            }
        }
        printf("%d\n",ret);
    }
}

C++: import Control.Applicative
import Control.Monad
import Control.Monad.ST
import Data.Functor
import Data.Function
import Data.Monoid
import Data.Maybe
import Data.List
import qualified Data.Foldable as Foldable
import qualified Data.Set as Set
--import qualified Data.Sequence as Sequence
import Data.List.Split
import Data.Bits
import Data.Char
import Data.Ix
import Data.Ratio
import Data.Ord
import Data.Tuple
--import Data.Array
--import Data.Array.Unboxed
import Data.Array.IArray
import Data.Array.MArray
import Data.Array.IO
import Data.Array.ST
import Data.IORef
import Data.STRef
-- import System.IO.Unsafe

-- templete
readInt = read :: String -> Int
readInteger = read :: String -> Integer
readDouble = read :: String -> Double
getInt = readLn :: IO Int
getInts = map readInt . words <$> getLine
getInteger = readLn :: IO Integer
getIntegers = map readInteger . words <$> getLine
getDouble = readLn :: IO Double
sjoin :: (Show a) => [a] -> String
sjoin = unwords . map show
cond :: a -> a -> Bool -> a
cond t f c = if c then t else f
apply2 :: (a -> a -> b) -> [a] -> b
apply2 f [x,y] = f x y
apply3 :: (a -> a -> a -> b) -> [a] -> b
apply3 f [x,y,z] = f x y z
apply4 :: (a -> a -> a -> a -> b) -> [a] -> b
apply4 f [x,y,z,w] = f x y z w
fnTuple :: (a -> b, a -> c) -> a -> (b, c)
fnTuple (f,g) a = (f a, g a)
replace :: (Eq a) => a -> a -> [a] -> [a]
replace x y = map (\z -> if z==x then y else z)
binMap :: (a -> a -> b) -> [a] -> [b]
binMap f (x:xs@(y:_)) = f x y : binMap f xs
binMap _ _ = []
splitRec :: Int -> [a] -> [[a]]
splitRec _ [] = []
splitRec n xs = let (y,ys) = splitAt n xs in y : splitRec n ys
infixl 7 `divCeil`
divCeil :: Integral a => a -> a -> a
x `divCeil` y = (x+y-1) `div` y
-- templete

newUF n = newArray (0,n-1) (-1)

root uf i = do
  c <- readArray uf i
  if c==(-1) then return i else root uf c >>= writeArray uf i >> readArray uf i

same uf i j = (==) <$> root uf i <*> root uf j

unite uf i j = root uf i >>= writeArray uf j

kruskal n es = runST $ do
    uf <- newUF n :: ST s (STUArray s Int Int)
    cost <- newSTRef 0
    forM_ es $ \(c, (a, b)) -> do
      s <- same uf a b
      unless s $ do
          modifySTRef cost (+c)
          unite uf a b
    readSTRef cost

type Edge = (Int, (Int,Int))
edge a b cost = (cost, (a, b))

main = do
    [n, m] <- getInts
    when (n/=0 || m/=0) $ do
        es <- sort . map (apply3 edge) <$> replicateM m getInts
        print $ kruskal n es
        main