Problem p00808 - Generation 1

Orig Description

Problem F: Young, Poor and Busy
Ken and Keiko are young, poor and busy. Short explanation: they are students, and ridden with part-time jobs. To make things worse, Ken lives in Hakodate and Keiko in Tokyo. They want to meet, but since they have neither time nor money, they have to go back to their respective jobs immediately after, and must be careful about transportation costs. Help them find the most economical meeting point.
Ken starts from Hakodate, Keiko from Tokyo. They know schedules and fares for all trains, and can choose to meet anywhere including their hometowns, but they cannot leave before 8am and must be back by 6pm in their respective towns. Train changes take no time (one can leave the same minute he/she arrives), but they want to meet for at least 30 minutes in the same city. 
There can be up to 100 cities and 2000 direct connections, so you should devise an algorithm clever enough for the task. 
Input
The input is a sequence of data sets.
The first line of a data set contains a single integer, the number of connections in the timetable. It is not greater than 2000.
Connections are given one on a line, in the following format.
     Start_city HH:MM Arrival_city HH:MM price
Start_city and Arrival_city are composed of up to 16 alphabetical characters, with only the first one in upper case. Departure and arrival times are given in hours and minutes (two digits each, separated by ":") from 00:00 to 23:59. Arrival time is strictly after departure time. The price for one connection is an integer between 1 and 10000, inclusive. Fields are separated by spaces.
The end of the input is marked by a line containing a zero. 
Output
The output should contain one integer for each data set, the lowest cost possible. This is the total fare of all connections they use.
If there is no solution to a data set, you should output a zero.
The solution to each data set should be given in a separate line. 
Sample 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
Output for the Sample Input
11000
0
11090

Extracted Specification

An integer M (1 ≤ M ≤ 2000), representing the number of connections.
M lines follow, each containing:
- A string Start_city of up to 16 alphabetical characters (first character uppercase, rest lowercase).
- A time HH:MM representing the departure time (00:00 ≤ HH:MM ≤ 23:59).
- A string Arrival_city of up to 16 alphabetical characters (first character uppercase, rest lowercase).
- A time HH:MM representing the arrival time (00:00 ≤ HH:MM ≤ 23:59, strictly after the departure time).
- An integer price (1 ≤ price ≤ 10000), representing the cost of the connection.
The end of the input is marked by a line containing a zero.

### Example 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
0
```

### Function Signature:
Write a function f(connections) that takes in the input.
```python
def f(connections: List[Tuple[str, str, str, str, int]]):
    '''
    connections: a list of tuples, each containing:
        - a string representing the start city
        - a string representing the departure time in HH:MM format
        - a string representing the arrival city
        - a string representing the arrival time in HH:MM format
        - an integer representing the price
    '''
```

Test Case Generator

Sure, I can help you write a test case generator for the problem "Young, Poor and Busy". This generator will create random test cases based on the specifications provided. Below is a Python script that generates such test cases:

```python
import random
import string

def random_city():
    # Generate a random city name with up to 16 alphabetical characters
    return ''.join(random.choices(string.ascii_uppercase + string.ascii_lowercase, k=random.randint(1, 16)))

def random_time():
    # Generate a random time in HH:MM format
    hour = random.randint(0, 23)
    minute = random.randint(0, 59)
    return f"{hour:02d}:{minute:02d}"

def is_valid_time(start, end):
    # Ensure the end time is strictly after start time
    start_hour, start_minute = map(int, start.split(':'))
    end_hour, end_minute = map(int, end.split(':'))
    return (end_hour > start_hour) or (end_hour == start_hour and end_minute > start_minute)

def generate_connection():
    start_city = random_city()
    start_time = random_time()
    arrival_city = random_city()
    while True:
        arrival_time = random_time()
        if is_valid_time(start_time, arrival_time):
            break
    price = random.randint(1, 10000)
    return f"{start_city} {start_time} {arrival_city} {arrival_time} {price}"

def tcgen():
    num_datasets = random.randint(1, 10)
    datasets = []

    for _ in range(num_datasets):
        num_connections = random.randint(1, 2000)
        connections = [generate_connection() for _ in range(num_connections)]
        datasets.append(f"{num_connections}\n" + "\n".join(connections))
    
    test_case = "\n".join(datasets) + "\n0"
    return test_case

# Example usage:
print(tcgen())
```

### Explanation:
1. **random_city()**: Generates a random city name of up to 16 characters.
2. **random_time()**: Generates a random time in HH:MM format.
3. **is_valid_time(start, end)**: Checks if the end time is strictly after the start time.
4. **generate_connection()**: Generates a random connection with valid times and price.
5. **tcgen()**: Generates multiple datasets with a random number of connections for each dataset.

### Sample Output:
The script will generate a test case with a sequence of datasets, each containing a number of connections in the specified format. The last line will be a zero to indicate the end of input.

This generator creates realistic and valid test cases for the problem, ensuring you can test your solution against a variety of scenarios.

Extract Arguments

def extract_arguments(fh):
    data_sets = []
    while True:
        num_connections = int(fh.readline().strip())
        if num_connections == 0:
            break
        connections = []
        for _ in range(num_connections):
            connection = fh.readline().strip().split()
            start_city = connection[0]
            departure_time = connection[1]
            arrival_city = connection[2]
            arrival_time = connection[3]
            price = int(connection[4])
            connections.append((start_city, departure_time, arrival_city, arrival_time, price))
        data_sets.append(connections)
    return data_sets

# if __name__ == "__main__":
#     input_path = sys.argv[1]
#     with open(input_path, 'r') as fh: 
#         data_sets = extract_arguments(fh)
#         for data_set in data_sets:
#             f(data_set)