Metro 2345
Time limit1sMemory limit512 MB
Given three metro lines meeting at three transfer stations with per-line edge times and a transfer cost, find the shortest travel time between two given stations.
- Level
Medium4 of 10
- Topics
- Shortest path, Graph, Math, Implementation
- Solved
- No attempts yet
Problem
Kekopolis has three metro lines. They intersect near the city center, forming a triangle of three tunnels between adjacent stations. The first line has stations, the second line has stations, and the third line has stations. Stations on each line are numbered consecutively starting from .
At stations where the lines intersect, passengers can change between lines. It is possible to change between station of the first line and station of the second line, between station of the first line and station of the third line, and between station of the second line and station of the third line.

The lines have different train speeds. The train on the first line travels between adjacent stations in seconds, the train on the second line does it in seconds, and the train on the third line does it in seconds. It takes seconds to change between lines. Ignore the time to enter or leave a station or a train, and the time a train stops at a station.
Dima needs to get to the Kekopolis Programming Contest. He starts from his home near station of the -th line and needs to reach Kekopolis University near station of the -th line. Dima overslept as usual, so he is running late. Dima wants to reach his destination as soon as possible.
Find the minimum time Dima needs to get from his home to the university. Dima does not enter or exit the metro at an interchange station.
Input
The first line of input contains three integers , , and ().
The second line contains three integers , , and (, , ).
The third line contains four integers , , , and ().
The fourth line contains four integers , , , and (; it is guaranteed that the -th line has station and the -th line has station ; the starting station is different from the destination station, and in particular they are not the two stations of the same interchange).
Output
Output one integer: the minimum time needed to travel from the starting station to the destination station.