#K40997. Minimum Maintenance Cost

    ID: 26766 Type: Default 1000ms 256MiB

Minimum Maintenance Cost

Minimum Maintenance Cost

You are given a network of N cities connected by M bidirectional roads. Each road connects two distinct cities and has an associated maintenance cost. Your task is to calculate the minimum total maintenance cost required to ensure that every city is reachable from any other city.

This problem can be solved using Kruskal's algorithm to find the minimum spanning tree (MST) of the graph. The total cost of maintaining the network is given by the sum of the weights of the edges in the MST:

$$ MST_{cost} = \sum_{e \in MST} w(e) $$

If it is not possible to connect all cities (i.e. the graph is disconnected), output Impossible.

inputFormat

The input begins with a line containing two integers, N and M, where N is the number of cities and M is the number of roads. The next M lines each contain three integers u, v, and w, where u and v denote the connected cities and w is the maintenance cost for that road.

outputFormat

Output a single line containing the minimum total maintenance cost if all cities can be connected, or output 'Impossible' if they cannot.## sample

4 5
1 2 3
2 3 4
3 4 5
1 3 8
2 4 2
9