#K86022. Minimum Delivery Cost

    ID: 36772 Type: Default 1000ms 256MiB

Minimum Delivery Cost

Minimum Delivery Cost

You are given a network of warehouses and possible delivery routes between them. Each route has an associated cost, and your task is to connect all warehouses with the minimum total delivery cost. In other words, you need to find the cost of the Minimum Spanning Tree (MST) for the given graph.

The graph is undirected and each warehouse is numbered from 1 to n. You will be given the number of warehouses (nodes) and the number of available routes (edges). For each route, you are provided two warehouse numbers and the cost to deliver between them.

This problem can be solved using famous algorithms such as Kruskal's algorithm or Prim's algorithm. In this problem, you are encouraged to use Kruskal's algorithm along with the Union-Find data structure to efficiently compute the MST.

Input Constraints: It is guaranteed that the graph is connected or can be connected using the provided routes.

inputFormat

The first line of input contains two integers n and m, where n is the number of warehouses and m is the number of available delivery routes.

The following m lines each contain three integers u, v and w, representing a delivery route between warehouses u and v with cost w.

outputFormat

Print a single integer representing the minimum total cost required to connect all the warehouses.

## sample
4 5
1 2 3
2 3 4
3 4 5
4 1 6
1 3 7
12