Tangled in Cables
Time limit1sMemory limit128 MB
Compute the minimum spanning tree of a town map and compare its total length against the available spool of cable.
- Level
Medium5 of 10
- Topics
- Minimum spanning tree, Graph, Union-find, Sorting
- Solved
- No attempts yet
Problem
You are the owner of SmallCableCo and have just bought the franchise rights for a small town. Unfortunately you do not have enough money to start the business properly, so you are relying on parts you found in an old warehouse you purchased. Among your finds is a single spool of cable and a large pile of connectors.
You want to work out whether you have enough cable to connect every house in town. You have a map of the town listing every path you may use to run cable between houses, together with the distance of each path. Compute the shortest total length of cable you need in order to connect all of the houses together.
Input
Only one town is given in the input.
- The first line gives the length of cable on the spool as a real number.
- The second line contains the number of houses, .
- The next lines each give the name of one house's owner. Each name is at most 20 characters from {a–z, A–Z, 0–9} and contains no whitespace or punctuation.
- The next line contains , the number of paths between houses.
- The next lines each have the form
houseA houseB distance. The two house names match two different names in the list above, and the distance is a positive real number. No two paths connect the same pair of houses.
Output
The output consists of a single line.
If there is not enough cable to connect all of the houses in the town, output
Not enough cable
If there is enough cable, output
Need <X> miles of cable
where is the shortest total length of cable needed to connect all of the houses, printed to the nearest tenth of a mile (0.1).