Transfer Train
Time limit8sMemory limit512 MB
Find a route from station A to B minimizing total ride time, breaking ties by fewest transfers, where each line can be ridden in either direction and repeated station names on one line require a transfer.
- Level
Medium7 of 10
- Topics
- Graph, Shortest path, Hash map, Greedy
- Solved
- No attempts yet
Problem
Eel likes riding trains. Right now Eel is trying to travel from station A to station B by train. Eel is in a hurry, so Eel decided to take a route with the shortest travel time. However, Eel is not good at transferring, so if there are several routes with the shortest travel time, Eel decided to take the route with the fewest transfers.
There are lines. The -th line passes through stations. The names of the stations the -th line passes through, in order, are , and the travel times between consecutive stations are . Trains run along a line in both directions, so you can also ride the stations in the reverse of the order given in the input. The same station name on multiple lines denotes the same station, and you can transfer between them. A transfer takes minutes regardless of the station or line.
A single line may pass through the same station more than once. If you want to move between two stations that have the same name and are on the same line but at different positions within the line, you must transfer. For example, when using the line C - D - E - F - D - G to go from C to G, you can ride a single train from the start to the end, or you can transfer at station D and ride C - D and D - G separately.
Find the time and the number of transfers Eel needs to travel from station A to station B. Trains come very frequently, so waiting time can be ignored.
Input
The input is given in the following format:
...
...
...
...
...
Output
Print the time Eel needs to travel from station A to station B and the number of transfers, separated by a space, on one line.
Constraints
- will be between 1 and 50,000, inclusive.
- will be an integer between 1 and 1,000, inclusive.
- At least one train stops at A.
- At least one train stops at B.
- A and B will be distinct.
- will be bigger than or equal to 2.
- will be between 2 and 100,000, inclusive.
- will contain between 1 and 10 characters, inclusive.
- Each character in will be a letter ('A'-'Z', 'a'-'z').
- will be an integer between 1 and 1,000, inclusive.