Problem:
Minimum Spanning Tree

Find the sum of weights of edges of the Minimum Spanning Tree for a given weighted undirected graph G = (V, E).

Input

|V| |E|
s0 t0 w0
s1 t1 w1
:
s|E|-1 t|E|-1 w|E|-1

, where |V| is the number of vertices and |E| is the number of edges in the graph. The graph vertices are named with the numbers 0, 1,..., |V|-1 respectively.

si and ti represent source and target verticess of i-th edge (undirected) and wi represents the weight of the i-th edge.

Output

Print the sum of the weights of the Minimum Spanning Tree.

Constraints

1 ≤ |V| ≤ 10,000

0 ≤ |E| ≤ 100,000

0 ≤ wi ≤ 10,000

The graph is connected

There are no parallel edges

There are no self-loops

Sample Input 1

4 6
0 1 2
1 2 1
2 3 1
3 0 1
0 2 3
1 3 5

Sample Output 1

3

Sample Input 2

6 9
0 1 1
0 2 3
1 2 1
1 3 7
2 4 1
1 4 3
3 4 1
3 5 1
4 5 6

Sample Output 2

5

Solution in Scala (judge verdict: Accepted)

import collection.mutable.ArrayBuffer
import scala.reflect.ClassTag
import scala.language.implicitConversions


trait Graph {
    case class Edge[T:Numeric](from : Int, to : Int, cost : T)

    class Edges[T:Numeric](val data : ArrayBuffer[Edge[T]]) {
        def size = data.size
    }
    object Edges {
        def apply[T:Numeric](xs : Edge[T]*) = new Edges(ArrayBuffer(xs:_*))
        implicit def toUnderLying[T](x:Edges[T]) = x.data
    }

    class WeightedGraph[T:Numeric](val size : Int, val data : Array[Edges[T]]) {
        def this(size : Int) = this(size, Array.fill(size)(Edges()))
    }
    object WeightedGraph {
        def apply[T:Numeric](size : Int) = new WeightedGraph[T](size)
        implicit def toUnderLying[T](x:WeightedGraph[T]) = x.data
    }

    class UnWeightedGraph[T:Numeric](val size : Int, val data: Array[ArrayBuffer[T]]) {
        def this(size : Int) = this(size, Array.fill(size)(ArrayBuffer[T]()))
    }
    object UnWeightedGraph {
        def apply[T:Numeric](size : Int) = new UnWeightedGraph[T](size)
        implicit def toUnderLying[T](x:UnWeightedGraph[T]) = x.data
    }
   
    class Matrix[T:Numeric:ClassTag](val row : Int, val col : Int, val data : Array[Array[T]]) {
        def this(row : Int, col : Int) = this(row, col, Array.fill(row, col)(implicitly[Numeric[T]].zero))
        def this(size : Int) = this(size, size)
    }
    object Matrix {
        def apply[T:Numeric:ClassTag](row : Int, col : Int)(init : T) = 
            new Matrix[T](row, col, Array.fill(row, col)(init))
        def apply[T:Numeric:ClassTag](size : Int)(init : T) : Matrix[T] = Matrix[T](size, size)(init)
        implicit def toUnderLying[T](x:Matrix[T]) = x.data
    }
}

object Prim extends Graph {
    import scala.collection.mutable.PriorityQueue
    def prim[T](g : WeightedGraph[T])(implicit num : Numeric[T]) : T = {
        import num._
        val used = new Array[Boolean](g.size)
        val que = PriorityQueue()(Ordering.Tuple2[T, Int].reverse)
        que += ((zero, 0))
        var res = zero
        while (!que.isEmpty) {
            val (d, v) = que.dequeue
            if (!used(v)) {
                res = res + d
                used(v) = true
                for (e <- g(v)) {
                    que += ((e.cost, e.to))
                }
            }
        }
        res
    }
}

import Prim._

object Main {
    def main(args: Array[String]) = {
        val sc = new java.util.Scanner(System.in)
        val V, E = sc.nextInt
        val edges = WeightedGraph[Int](V)
        for (_ <- 1 to E) {
            val s, t, c = sc.nextInt
            edges(s) += Edge(s, t, c)
            edges(t) += Edge(t, s, c)
        }
        val res = prim(edges)
        println(res)
    }
}