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 C++11 (judge verdict: Accepted)

#include <bits/stdc++.h>
using namespace std;

#define INF 1LL << 60

using Weight = long long;
struct Edge {
	int src, dst;
	Weight weight;
	Edge(int src_, int dst_, Weight weight_) : src(src_), dst(dst_), weight(weight_) { }
};
bool operator<(const Edge &e, const Edge &f) {
	return e.weight != f.weight ? e.weight > f.weight : e.dst < f.dst;
}
using Edges = vector<Edge>;
using Graph = vector<Edges>;

void edgesToGraph(const Edges &edges, int V, Graph &g) {
	g.resize(V);
	for (auto e : edges) g[e.src].push_back(e);
}

void edgesToGraphUndir(const Edges &edges, int V, Graph &g) {
	g.resize(V);
	for (auto e : edges) {
		g[e.src].push_back(e);
		g[e.dst].push_back(Edge(e.dst, e.src, e.weight));
	}
}

//edgesToGraphUndir
pair<Weight, Edges> prim(const Graph &g, int r = 0) {
	int n = g.size();
	Edges T;
	Weight total = 0;

	vector<bool> visited(n);
	priority_queue<Edge> Q;
	Q.push(Edge(-1, r, 0));
	while (!Q.empty()) {
		Edge e = Q.top(); Q.pop();
		if (visited[e.dst]) continue;
		T.push_back(e);
		total += e.weight;
		visited[e.dst] = true;
		for(auto f : g[e.dst]) if (!visited[f.dst]) Q.push(f);
	}
	return pair<Weight, Edges>(total, T);
}

int main(void) {
	int V, E;
	cin >> V >> E;
	Edges edges;
	for (int i = 0; i < E; ++i) {
		long long s, t, w;
		cin >> s >> t >> w;
		edges.emplace_back(s, t, w);
	}
	Graph g;
	edgesToGraphUndir(edges, V, g);
	pair<Weight, Edges> p = prim(g);
	cout << p.first << endl;
	return 0;
}