Pony Express (Small)
InterviewTime limit5sMemory limit512 MB
Cities lie on a line with a horse in each; find the minimum time from city 1 to city N, switching horses at intermediate cities, subject to each horse's endurance limit.
- Level
Medium5 of 10
- Topics
- Dynamic programming, Shortest path, Greedy
- Solved
- No attempts yet
Problem
It is the year 1860, and the Pony Express is the fastest mail delivery system joining the East and West coasts of the United States. The system covers different cities. Each city has exactly one horse. Every horse travels at its own constant speed and has a maximum total distance it can cover before it is too tired to continue.
The rider starts out on the starting city's horse. Every time the rider reaches a city, the rider may keep the current horse or switch to that city's horse. Switching takes no time. Horses never get a chance to rest, so once part of a horse's maximum total distance is used up, it is used up forever. The mail is delivered when the rider reaches the destination city.
The routes between cities were settled by complicated negotiations between the company owners, lawmakers, union delegates, and cousin Pete. That is why the distances between cities do not follow common sense. They do not have to satisfy the triangle inequality, and the distance from city A to city B may differ from the distance from city B to city A.
You are a time traveling entrepreneur and you brought a fast computer back from the future. One computer is not enough to start an e-mail service and put the Pony Express out of business, but you can use it to plan optimal routes for the Pony Express. You are given all data about the routes between cities and the horses in each city, together with a list of pairs of starting and ending cities. Compute the minimum time needed for each delivery. The deliveries are independent. Using a city or a horse on one route does not make it unavailable on another route.
Input
The first line contains the number of test cases, . test cases follow. Each test case has this form.
- One line with two integers and . is the number of cities with horses and is the number of pairs of stops to investigate. Cities are numbered from to .
- lines, each with two integers and . is the maximum total distance in kilometers that the horse in city can go, and is the constant speed in kilometers per hour at which that horse travels.
- lines, each with integers. The -th integer on the -th of these lines, , is if there is no direct route from city to city , and the length of that route in kilometers otherwise.
- lines, each with two integers and , the starting city and the destination city of the -th pair to investigate.
Limits.
- for all
- for all
- for all ,
- for all . There is no direct route from a city to itself.
- for all ,
- for all
- The delivery from city to city can be completed with the given horses, for all .
- or for all different , . No ordered pair of cities to investigate repeats within a test case.
- for all , with . The cities stand in a single line, and every route goes from one city to the next city in the line.
- . The only delivery to compute runs from the first city in the line to the last one.
Output
For each test case, output one line containing Case #x: y1 y2 ... yQ, where is the test case number starting from and is the minimum time, in hours, to deliver a letter from city to city .
Print each rounded to six digits after the decimal point, always with all six digits. No input is given for which that rounding is ambiguous at the sixth digit.
Hint
In the first test case of the example there are two options: use the horse in city 1 for the whole trip, or change horses in city 2. Both horses have enough endurance, so both options work. The horse in city 2 is faster, so changing is better, for a total time of .
In the second test case of the example there are two intermediate cities where you can change horses. If you change horses in city 2, the new horse is blazingly fast but does not have enough endurance, so you are forced to change again in city 3. If you keep your horse, you can decide in city 3 whether to change. The three options and their total times are:
- Change horses in both city 2 and city 3.
- Change horses just in city 3.
- Never change horses.