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.
Medium5Dynamic programmingShortest pathGreedyInterviewNo attempts yetTime limit5sMemory limit512 MBIt 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 N 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.
The first line contains the number of test cases, T. T test cases follow. Each test case has this form.
Limits.
For each test case, output one line containing Case #x: y1 y2 ... yQ, where x is the test case number starting from 1 and yk is the minimum time, in hours, to deliver a letter from city Uk to city Vk.
Print each yk 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.
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 1/3+1/4.
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: