Sightseeing Cows
Time limit1sMemory limit128 MB
Find a directed cycle maximizing the ratio of total fun at its first-visit landmarks to total traversal time, and print that maximum ratio.
- Level
Hard8 of 10
- Topics
- Graph, Binary search, Shortest path, Greedy
- Solved
- No attempts yet
Problem
Farmer John is rewarding his cows for their hard work with a day trip to the big city, and the cows must decide how to spend their free time.
They have a detailed map of the city showing () major landmarks (numbered through ) and () one-way cow paths that connect them. Farmer John drives the cows to a starting landmark of their choice; from there they walk along the paths through a series of landmarks and eventually return to that same starting landmark, where Farmer John picks them up. Because space in the city is scarce, every cow path is narrow and may be walked in one fixed direction only.
The cows may stay in the city as long as they like, but they bore easily: visiting a new landmark is fun, yet walking between landmarks costs time. Every landmark has a known fun value ().
Cow path runs from landmark to landmark (in the direction ) and takes time () to traverse.
To make the most of the day off, the cows want to maximize the average fun value per unit time of their trip. A landmark is only fun the first time it is visited; passing through it again grants no additional fun. Farmer John also insists that the trip visit at least two landmarks so the cows get some exercise.
Help the cows find the maximum average fun per unit time they can achieve.
Input
- Line 1: two integers and .
- Lines 2 to : line contains the single integer .
- Lines to : line describes cow path with three integers , , and .
Output
- One line with the maximum achievable average fun per unit time, truncated (rounded down) to exactly two decimal places. If the cows cannot make any valid round trip, print
0.00.
Hint
In the sample, the trip collects fun over time , for an average of . The trip averages only , and any trip that passes through landmark averages less than .