Travel Plan (Small)
Time limit1sMemory limit128 MB
Given bus lines with fixed departure frequencies, find the earliest arrival time at a target station starting from a station at a given clock time.
- Level
Medium6 of 10
- Topics
- Graph, Shortest path, Implementation, Simulation
- Solved
- No attempts yet
Problem
Consider a diagram of a public transportation network, for example a bus network, tram network, or underground network. The vertices of the diagram, numbered , correspond to stations, and an edge with means there is a direct connection between stations and ().
Transportation lines are numbered . Line is defined by the sequence of stations where its vehicles stop, together with the travel times between consecutive stations: is the time to go from station to (or back), is the time from to , and so on. All stations on one line are distinct (that is, implies ).
Vehicles on line run with a fixed frequency , where belongs to the set . Vehicles depart station at the top of every hour throughout the whole day (an hour mark being minute of hour with ), and then, following the frequency, at minutes, minutes, and so on after each hour mark. Vehicles of line run in both directions, from toward and from toward . The departure times from are the same as from .
In this network we want to travel from a start station to a finish station . Assume the trip is always possible and takes no longer than 24 hours. During the trip you may change lines as many times as you like. A change itself takes time, but you must account for the time spent waiting for the vehicle you want to board. The goal is to reach the finish station from the start station as early as possible.
For example, the picture below shows a network with stations and two lines, and . Vehicles of line run between stations , and vehicles of line run between stations . The frequencies are and . The travel times between stations are written next to the edges, with subscripts and to indicate the line.

Suppose that at 23:30 you are at station and want to reach station . You wait minutes and then board line at 23:40. There are two options. In the first, you reach station at 23:51, wait minutes, change to line at 23:54, and arrive at station at 0:16 the next day. In the second, you stay on line and reach station at 0:08, wait minutes, board line at 0:21, and arrive at station at 0:31. Hence the earliest time to reach station is 0:16.
Write a program that:
- reads from standard input the transportation network, the lines, the start station number , the finish station number , and the hour and minute of the beginning of the trip, and ;
- finds the shortest travel time from the start station to the finish station ;
- writes to standard output the earliest possible arrival time at the finish station , that is the hour and minute .
Input
The first line of standard input contains six integers separated by single spaces:
- the number of stations (),
- the number of lines (),
- the start station number (),
- the finish station number (),
- the hour of the beginning of the trip (),
- the minute of the beginning of the trip ().
Stations are numbered from to , and lines from to . The following lines describe the lines; each description takes three consecutive lines.
- The first line describing line contains two integers: , the number of stations (), and , the frequency ().
- The second line describing line contains the distinct integers , the numbers of the consecutive stations on line ().
- The third line describing line contains the integers , the times in minutes to travel between consecutive stations of the line ().
The total number of stations over all lines is at most (that is, ).
Output
Print a single line with two integers separated by a single space: the hour () and the minute () of the earliest possible arrival at the finish station. If the arrival falls on the next day, print the clock time taken modulo one full day (24 hours).