cho.sh
Notes
Loading...

Minimum Spanning Tree

Time limit

1s

Memory limit

128 MB

Problem

You are given an undirected weighted graph. Among all subgraphs that connect every vertex, find a tree whose total edge weight is as small as possible.

Such a tree is called a minimum spanning tree.

Input

The first line contains the number of vertices V and the number of edges E.

  • 1 <= V <= 10,000
  • 1 <= E <= 100,000

Each of the next E lines contains three integers A, B, and C, describing one edge. It means that vertex A and vertex B are connected by an edge with weight C.

  • Vertices are numbered from 1 to V.
  • C may be negative, and |C| <= 1,000,000.
  • There is always a path between any two vertices.
  • The total weight of the minimum spanning tree is within the range of a signed 32-bit integer.

Output

Print the total weight of the minimum spanning tree on one line.