This page is still under construction.

Parts of this page are still being built. What you see may change.

Railroad

Interview

Time limit1sMemory limit128 MB

Summary
Given train schedules with stop times, find the journey from a start city to a destination leaving no earlier than a given time, minimizing arrival time and then maximizing departure.
Level

Medium6 of 10

Topics
Graph, Shortest path, Sorting, Implementation
Solved
No attempts yet

Problem

Early tomorrow morning Jill must travel from one city to another to reach a programming contest. To avoid arriving too late and being shut out, she wants to reach her destination as early as possible. But she also dislikes waiting at the station, so if several journeys arrive at the same time she picks the one that departs from the start city as late as possible.

You are given several train schedules. For a given start city, destination city, and earliest possible departure time, find the connection that reaches the destination at the earliest possible arrival time. If several connections share that earliest arrival time, choose the one whose departure from the start city is as late as possible.

Changing trains takes no time at all: Jill can switch trains instantaneously (in zero time). She may board or get off a train at any of the stops it makes.

Input

The input contains several scenarios. Each scenario has three parts.

Part 1 — cities. A line with an integer CC (1≤C≤1001 \le C \le 100), followed by CC lines, each containing one city name. City names consist of letters only.

Part 2 — trains. A line with an integer TT (T≤1000T \le 1000), followed by TT train descriptions. Each train description begins with a line containing an integer tit_i (ti≤100t_i \le 100), followed by tit_i lines, each containing a time and a city name. Such a line means the train stops at that city at that time, where a passenger may board or get off. Times use the 24-hour format hhmm.

Part 3 — the query. Three lines: the earliest possible departure time, the name of the start city, and the name of the destination city. The start and destination cities are always different.

A line containing a single 00 in place of CC marks the end of the input and must not be processed.

Output

For each scenario, first print a line Scenario #n, where nn is the scenario number starting from 11.

If a connection exists, print two more lines. The first line is Departure, the departure time (four digits, zero-padded), and the start city. The second line is Arrival, the arrival time (four digits, zero-padded), and the destination city. Pad with spaces so the times line up: exactly as in the example, Departure is followed by one space and Arrival by three spaces.

If no connection reaches the destination on the same day (that is, before midnight), print a single line No connection instead.

Separate consecutive scenarios with a single blank line.

Examples3

  1. Example 1

    Input
    3
    Tuttlingen
    Constance
    Freiburg
    3
    2
    0949 Tuttlingen
    1006 Constance
    2
    1325 Tuttlingen
    1550 Freiburg
    2
    1205 Constance
    1411 Freiburg
    0800
    Tuttlingen
    Freiburg
    2
    Ulm
    Vancouver
    1
    2
    0100 Ulm
    2300 Vancouver
    0800
    Ulm
    Vancouver
    0
    
    Expected output
    Scenario #1
    Departure 0949 Tuttlingen
    Arrival   1411 Freiburg
    
    Scenario #2
    No connection
    
  2. Example 2

    Input
    2
    Alpha
    Beta
    3
    2
    0800 Alpha
    0900 Beta
    2
    0830 Alpha
    0900 Beta
    2
    0700 Alpha
    1000 Beta
    0600
    Alpha
    Beta
    0
    
    Expected output
    Scenario #1
    Departure 0830 Alpha
    Arrival   0900 Beta
    
  3. Example 3

    Input
    3
    North
    South
    East
    1
    2
    0800 North
    0900 East
    0700
    North
    South
    0
    
    Expected output
    Scenario #1
    No connection