#C10991. Minimum Road Length
Minimum Road Length
Minimum Road Length
You are given a network of cities and roads. Each road connects two distinct cities and has an associated length. Your task is to select a subset of these roads so as to connect all the cities, either directly or indirectly, with the minimum total length. This problem can be modeled as finding the Minimum Spanning Tree (MST) in a graph.
The total length of the selected roads is expressed as \(\sum_{i=1}^{k} l_i\), where \(l_i\) represents the length of the \(i\)-th road used. If it is impossible to connect all cities, output "Impossible".
inputFormat
The input is given via standard input in the following format:
- The first line contains two integers \(n\) and \(m\), representing the number of cities and the number of roads respectively.
- The next \(m\) lines each contain three integers \(u\), \(v\) and \(l\), indicating there is a road connecting city \(u\) and city \(v\) with length \(l\). Cities are numbered from 1 to \(n\).
outputFormat
If it is possible to connect all cities, output the minimum total road length required. Otherwise, output "Impossible".
## sample4 5
1 2 5
1 3 5
2 4 8
3 4 2
1 4 3
10