Problem Statement | |||||||||||||
There are several cities in the country, and some of them are connected by bidirectional roads. Unfortunately, some of the roads are damaged and cannot be used right now. Your goal is to rebuild enough of the damaged roads that there is a functional path between every pair of cities. You are given String[] roads, each element of which describes a single road. Damaged roads are formatted as "id city1 city2 cost" and non-damaged roads are formatted as "id city1 city2" (all quotes for clarity). In this notation, id is the unique identifier of the road, and city1 and city2 are the case-sensitive names of the two cities directly connected by that road. If the road is damaged, cost represents the price of rebuilding that road. Each id will be formatted "Cx" (quotes for clarity), where C is an uppercase letter and x is a digit. Every city in the country will appear at least once in roads. Return a String containing a single space separated list of the identifiers of the roads that must be rebuilt to achieve your goal. If there are multiple possibilities, select the one with the minimal total reconstruction cost. If a tie still exists, return the String that comes first lexicographically. If it is impossible to achieve your goal, return "IMPOSSIBLE" (quotes for clarity only). | |||||||||||||
Definition | |||||||||||||
| |||||||||||||
Notes | |||||||||||||
- | There can be more than one road between a pair of cities. | ||||||||||||
Constraints | |||||||||||||
- | roads will contain between 1 and 50 elements, inclusive. | ||||||||||||
- | Each element of roads will contain between 6 and 50 characters, inclusive. | ||||||||||||
- | Each element of roads will be formatted as "id city1 city2" or "id city1 city2 cost" (all quotes for clarity). | ||||||||||||
- | Each id will be formatted as "Cx" (quotes for clarity), where C is an uppercase letter ('A'-'Z') and x is a digit ('0'-'9'). | ||||||||||||
- | Each id in roads will be distinct. | ||||||||||||
- | Each city1 and city2 will contain between 1 and 45 letters ('a'-'z', 'A'-'Z'), inclusive. | ||||||||||||
- | In each element of roads, city1 and city2 will be distinct. | ||||||||||||
- | Each cost will be an integer between 1 and 1000, inclusive, with no leading zeroes. | ||||||||||||
Examples | |||||||||||||
0) | |||||||||||||
| |||||||||||||
1) | |||||||||||||
| |||||||||||||
2) | |||||||||||||
| |||||||||||||
3) | |||||||||||||
| |||||||||||||
4) | |||||||||||||
|