City Driving
Time limit1sMemory limit128 MB
In a connected graph with N nodes and N edges, answer many shortest-path queries between pairs of nodes.
- Level
Hard8 of 10
- Topics
- Tree, Graph, Shortest path, DFS
- Solved
- No attempts yet
Problem
You recently started spending your free time in San Francisco and realized that driving around the city is a huge pain. There are only locations that interest you, so you decide to make your driving easier. Because you have no GPS and cannot memorize many routes, you write down directions and travel times between pairs of locations. Each route is bidirectional (it takes the same time in either direction), and using only these routes you can travel between any two locations.
Now you are planning your weekend trips and, for pairs of locations, you need to find the fastest way to travel between them using only the routes you wrote down.
Input
The input contains multiple test cases.
Each test case begins with a line containing a single integer (), the number of locations, which is also the number of routes.
Each of the next lines contains three integers , , and (): a route connecting locations and (0-indexed) that takes time in both directions.
The next line contains a single integer (), the number of queries.
Each of the next lines contains two integers and : find the minimum time to travel from location to location .
The input ends with a line containing , which should not be processed.
Output
For each test case, print lines. The -th line contains a single integer: the minimum time to travel between the -th queried pair of locations and .