This page is still under construction.

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

Young, Poor and Busy

Time limit1sMemory limit128 MB

Summary
Two travelers starting from Hakodate and Tokyo meet in one city for at least 30 minutes and both return home between 08:00 and 18:00 at the lowest total fare.
Level

Medium7 of 10

Topics
Shortest path, Graph, Dynamic programming
Solved
No attempts yet

Problem

Ken and Keiko are students buried in part time jobs, so they have neither time nor money to spare. Ken lives in Hakodate, Keiko lives in Tokyo. They want to see each other, but each has to be back at work the same day, and the fare matters. Find the cheapest way for the two of them to meet.

Ken starts in Hakodate and Keiko starts in Tokyo. Both know the time and the fare of every train, and they may meet in any city, including the cities they live in. Neither of them may depart before 08:00, and both must be back in their own city by 18:00. A transfer takes no time, so a traveler may board the next train in the same minute he or she arrives. They want to spend at least 30 minutes together in one city.

A timetable can hold up to 100 cities and up to 2000 direct connections. Building every possible itinerary one by one does not finish inside the time limit.

Input

The input is a sequence of data sets.

The first line of a data set holds one integer, the number of connections in the timetable. It is at most 2000.

The following lines hold one connection each, in this format.

StartCity HH:MM ArrivalCity HH:MM price

A city name is at most 16 letters long and only its first letter is upper case. Departure and arrival times are written with two digits for the hour and two digits for the minute, separated by a colon, and range from 00:00 to 23:59. The arrival time is strictly later than the departure time. The price of one connection is an integer between 1 and 10000. Fields are separated by spaces.

A line holding a single zero ends the input.

Output

For each data set print the lowest possible total fare on its own line. This is the sum of the fares of every connection the two of them use.

Print 0 for a data set that has no way for them to meet.

Examples1

  1. Example 1

    Input
    5
    Hakodate 08:15 Morioka 12:30 2500
    Morioka 14:05 Hakodate 17:30 2500
    Morioka 15:30 Hakodate 18:00 3000
    Morioka 14:30 Tokyo 17:50 3000
    Tokyo 08:30 Morioka 13:35 3000
    4
    Hakodate 08:15 Morioka 12:30 2500
    Morioka 14:04 Hakodate 17:30 2500
    Morioka 14:30 Tokyo 17:50 3000
    Tokyo 08:30 Morioka 13:35 3000
    18
    Hakodate 09:55 Akita 10:53 3840
    Hakodate 14:14 Akita 16:09 1920
    Hakodate 18:36 Akita 19:33 3840
    Hakodate 08:00 Morioka 08:53 3550
    Hakodate 22:40 Morioka 23:34 3550
    Akita 14:23 Tokyo 14:53 2010
    Akita 20:36 Tokyo 21:06 2010
    Akita 08:20 Hakodate 09:18 3840
    Akita 13:56 Hakodate 14:54 3840
    Akita 21:37 Hakodate 22:35 3840
    Morioka 09:51 Tokyo 10:31 2660
    Morioka 14:49 Tokyo 15:29 2660
    Morioka 19:42 Tokyo 20:22 2660
    Morioka 15:11 Hakodate 16:04 3550
    Morioka 23:03 Hakodate 23:56 3550
    Tokyo 09:44 Morioka 11:04 1330
    Tokyo 21:54 Morioka 22:34 2660
    Tokyo 11:34 Akita 12:04 2010
    0
    
    Expected output
    11000
    0
    11090