Pleasant Path
Time limit3sMemory limit1024 MB
In a DAG with places ordered toward home, find the path from 1 to n maximizing the average edge weight.
- Level
Medium7 of 10
- Topics
- Binary search, Dynamic programming, Graph, Greedy
- Solved
- No attempts yet
Problem
When I walk home, I do not always take the shortest route. I take a route that a) keeps bringing me closer to home and b) is the "pleasantest", meaning the average pleasantness factor of the road segments I pass is as high as possible. Write a program that computes the maximum such average.
The map of my city can be described with places numbered from to . Place is my starting point and place is my home, and the places are sorted by distance so that a place with a higher number is always closer to home than one with a lower number.
There are also different "road segments", each going from one place to another place and having a pleasantness factor , which might come from unusual trees, a cute cat in a window, or something else pleasant. Since I always want to walk in the direction of home, the description includes only road segments with .
Someone with a bit of mathematical interest (if there is such a person in this company) might call this a directed, weighted acyclic graph.

The map in the second sample. The pleasantest path is .
Input
The first line contains the two integers and ( , ). Each of the following lines describes one road segment and contains three integers , , (, ), meaning the road segment goes from place to place and has pleasantness factor .
No two road segments connect the same pair of places, and it is guaranteed that place is reachable from place .
Output
Print one number: the highest achievable average of the pleasantness factors along a path from place 1 to place . The answer is considered correct if it has a relative or absolute error of at most .