#K76107. Next Train Schedule

    ID: 34569 Type: Default 1000ms 256MiB

Next Train Schedule

Next Train Schedule

You are given a list of train routes and a series of queries. Each route is defined by a start station, an end station, and a sequence of scheduled departure times. For each query, you must determine the earliest departure time that is strictly greater than the provided departure time from the specified start station to the end station. If no such departure time exists, return -1.

Note: The departure times for each route are provided in increasing order.

Formally, for a query with start station S, end station E, and time T, find the minimum time t in the list of departure times for route (S, E) such that t > T. If no such time exists or the route (S, E) is not available, output -1.

This problem tests your ability to process inputs, store data efficiently, and perform binary search (or simple linear scan if preferred) to determine the correct answer.

inputFormat

The input is read from stdin and has the following format:

N
S1 E1 T1
time11 time12 ... time1T1
S2 E2 T2
time21 time22 ... time2T2
...
SN EN TN
timeN1 timeN2 ... timeNTN
Q
S1 E1 P1
S2 E2 P2
...
SQ EQ PQ

Where:

  • N is the number of routes.
  • Each route is defined by a line containing two station names and an integer T (number of departure times), followed by a line with T departure times.
  • Q is the number of queries.
  • Each query consists of a start station, an end station, and a departure time.
  • </p>

    outputFormat

    For each query, output a single integer on a separate line representing the earliest departure time that is strictly greater than the queried time. If there is no valid departure time or the route does not exist, output -1.

    The output is written to stdout.

    ## sample
    3
    A B 5
    5 15 25 35 55
    B C 4
    60 120 180 240
    A C 3
    100 200 300
    3
    A B 10
    A C 50
    B C 80
    15
    

    100 120

    </p>