Hoo's Afraid of the Big Bad Wolf?
InterviewTime limit1sMemory limit128 MB
Find the directed path from X to Y with the largest product of edge safety probabilities, printed rounded to six decimals.
- Level
Medium4 of 10
- Topics
- Shortest path, Graph
- Solved
- No attempts yet
Problem
Little Red Riding Hood is walking to her grandmother's house. She reads the Big Bad Wolf's blog, which lists the paths that he and his friends guard. The wolf keeps the details to himself, so the blog only publishes the probability that a path has no wolf on it. If Little Red Riding Hood takes a path a wolf is guarding, she is eaten. Every path through the forest runs in one direction, so she cannot walk back along a path she has taken.
Find the largest probability that Little Red Riding Hood reaches her grandmother's house.
The picture below shows the first example.

Input
The first line contains the number of test cases.
Each test case begins with a line containing an integer (), the number of intersections. The next line contains two integers and () separated by a single space, the starting intersection and the ending intersection . A route from to always exists. The next line contains an integer (), the number of one directional paths. Each of the next lines contains three values separated by spaces: the start intersection , the end intersection , and the probability () that the path from to is safe, meaning no wolf guards it. Two intersections can be joined by more than one path. Every probability is written with at most three digits after the decimal point.
Output
For each test case print one line in the form Case x: p, where is the case number starting from 1 and is the probability that Little Red Riding Hood reaches her grandmother's house along the safest route.
Round to exactly six digits after the decimal point and keep the trailing zeros. Do not use exponential notation. In every test the answer is farther than from a rounding boundary, so the rounded value is unambiguous.
If and are the same intersection, Little Red Riding Hood is already at the house, so the answer is .