Roads Scholar
Time limit1sMemory limit128 MB
Given a small weighted graph, cities, and signs placed on roads, list every city whose shortest path from the intersection behind the sign starts along that road, printing rounded distances.
- Level
Medium7 of 10
- Topics
- Shortest path, Graph, Sorting, Implementation
- Solved
- No attempts yet
Problem
The Hines Sign company supplies roadside mile-marker signs for a state highway system. For one class of signs, each sign lists nearby cities together with the distance a traveller must go to reach each one.
A sign is placed at a fixed point on a road and faces the direction of travel along that road. Let be the intersection immediately behind the sign — the one the traveller has just left. A city is listed on the sign exactly when the shortest path from to begins by travelling along the very road the sign sits on. You may assume the shortest path between any two intersections is unique.
The distance printed for a listed city is the length of the shortest route from the sign itself to ; that is, the shortest distance from to minus the distance from to the sign.
Input
The first line contains a single integer : the number of test cases. A blank line precedes each test case.
Each test case describes one highway system and then a list of sign placements.
The first line of a test case has three integers , , : is the number of intersections (numbered ), is the number of roads, and is the number of intersections that are also cities.
The next lines each contain i1 i2 d: a two-way road between intersections i1 and i2 whose length is d.
The next lines each contain i name: intersection i is a city called name.
The next line contains a single integer : the number of signs. Each of the following lines contains i1 i2 d: a sign placed on the road from i1 toward i2, at distance d from i1 (where and d is strictly less than the length of that road).
Every name has length at most , , and every distance is positive and given to the nearest hundredth of a mile.
Output
For each test case, print the result for every sign in the order the signs are given. For a single sign, print one line per listed city:
name distance
Here name is the city name, followed by one space, followed by distance — the distance from the sign to that city rounded to the nearest mile (a value ending in exactly rounds up; for example becomes ).
Within one sign, sort the lines by rounded distance in ascending order, breaking ties by city name in alphabetical order. Separate consecutive signs with a blank line, and separate the outputs of consecutive test cases with a blank line. Every sign lists at least one city.