Train Delays

Time limit5sMemory limit128 MB

Summary
Given a train timetable with hourly departures and probabilistic delays, compute the minimum expected total travel time from start to destination as an exact fraction.
Level

Hard8 of 10

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

Problem

You are planning a train journey with transfers and want to spend as little time traveling as possible, but trains can be delayed.

For every train you know its scheduled departure time, its travel time when it runs on time, the probability that it is delayed, and how long such a delay can be. Whether a given train is delayed is independent of every other train, and you cannot know in advance whether a train will be delayed; you only find out when the delay actually happens.

Trains always leave exactly on schedule; only the arrival can be late. Changing trains takes no time, so you may board a train that departs at the very same minute another train drops you off. If a delay occurs along the way, you may revise the rest of your plan to account for it. You may choose the moment you board your very first train freely, so you never have to wait for it.

Given the schedule, compute the minimum possible expected total travel time from the start city to the destination city.

Input

The first line contains the number of test cases, which is at most 100.

Each test case begins with a line containing the name of the start city and the name of the destination city; these two names are always different. The next line contains the number of trains nn (1≤n≤10001 \le n \le 1000). Each of the following nn lines describes one train with six fields:

  • the departure city and the arrival city (always two different names)
  • the departure minute mm (0≤m≤590 \le m \le 59): the train runs once every hour and always leaves at minute mm
  • the travel time tt (1≤t≤3001 \le t \le 300), in minutes, when the train is not delayed
  • the delay probability pp (0≤p≤1000 \le p \le 100), as a percentage
  • the maximum delay dd (1≤d≤1201 \le d \le 120), in minutes

When a train is delayed, the delay is an integer number of minutes drawn uniformly from [1,d][1, d]. Every city name consists of uppercase and lowercase letters only and has length at most 20.

Output

For each test case, print the minimum expected total travel time as an irreducible fraction p/q, where q≥1q \ge 1 and gcd⁡(p,q)=1\gcd(p, q) = 1; if the value is an integer aa, print it as a/1. If the destination city cannot be reached, print IMPOSSIBLE instead.

Every input quantity is rational, so the expected travel time is always a rational number and can be given exactly as such a fraction.

Examples6

  1. Example 1

    Input
    3
    Seoul Daejeon
    3
    Seoul Daejeon 15 68 10 5
    Seoul Daejeon 46 55 50 60
    Daejeon Busan 14 226 10 120
    Seoul Daejeon
    1
    Seoul Busan 10 22 5 10
    Seoul Daejeon
    9
    Seoul Gwangmyeong 15 10 0 1
    Seoul Gwangmyeong 45 10 0 1
    Seoul Cheonan 23 140 10 15
    Gwangmyeong Busan 44 51 60 70
    Busan Incheon 55 147 38 40
    Incheon Daejeon 24 15 30 15
    Incheon Daejeon 54 15 10 35
    Cheonan Anyang 45 140 5 10
    Anyang Incheon 46 96 10 20
    
    Expected output
    683/10
    IMPOSSIBLE
    2135373/7000
  2. Example 2

    Input
    1
    Alpha Bravo
    1
    Alpha Bravo 0 10 0 1
    
    Expected output
    10/1
  3. Example 3

    Input
    1
    Alpha Bravo
    1
    Alpha Bravo 0 10 100 4
    
    Expected output
    25/2
  4. Example 4

    Input
    1
    Home Work
    2
    Home Work 0 20 50 10
    Home Work 0 18 100 8
    
    Expected output
    45/2
  5. Example 5

    Input
    1
    Start Goal
    2
    Start Middle 0 5 0 1
    Other Goal 0 5 0 1
    
    Expected output
    IMPOSSIBLE
  6. Example 6

    Input
    1
    A C
    2
    A B 10 20 0 1
    B C 45 30 0 1
    
    Expected output
    65/1