#K33102. Minimum Cost to Connect Computers
Minimum Cost to Connect Computers
Minimum Cost to Connect Computers
You are given (n) computers and (m) possible connections. Each connection is represented by three integers (u), (v), and (w), where connecting computer (u) and computer (v) incurs a cost (w). Your task is to determine the minimum total cost required to connect all the computers so that every computer is reachable from every other computer. If it is impossible to connect all computers, output "Impossible". This problem can be effectively solved using Kruskal's algorithm to find the Minimum Spanning Tree (MST).
inputFormat
The input is read from standard input (stdin) as follows:
The first line contains two integers (n) and (m), where (n) is the number of computers and (m) is the number of available connections. Each of the following (m) lines contains three space-separated integers (u), (v), and (w), representing a connection between computer (u) and computer (v) with cost (w). Note that the computers are numbered from 1 to (n).
outputFormat
Output a single line to standard output (stdout) that contains the minimum cost to connect all computers. If it is impossible to connect all computers, output "Impossible".## sample
4 5
1 2 1
2 3 4
3 4 3
4 1 2
1 3 3
6