Following Flow
Time limit1sMemory limit8 MB
Compute the expected travel time from vertex 0 to vertex N when each step follows a uniformly random outgoing edge with its own crossing time.
- Level
Medium7 of 10
- Topics
- Probability, Matrix, Graph
- Solved
- No attempts yet
Problem
The world has vertices, numbered through . Edges run between the vertices. Every edge is one way, and each edge takes its own amount of time to cross. Donghyun is standing at vertex , and vertex is his home.
Until he reaches home, Donghyun wanders wherever the mood takes him. Wandering means that he picks one of the edges leaving his current vertex, each with the same probability, and follows it to the next vertex. When several edges join the same pair of vertices, each one counts as a separate edge. An edge that returns to its own vertex is also possible. Once he arrives at vertex , he stops there.
Compute the expected time it takes him to get from vertex to his home.
For example, suppose the world looks like the picture below.

At vertex Donghyun spends time to move to vertex with probability , or spends time to move to vertex and reach home. At vertex he spends time to return to vertex with probability , or spends time to reach home. The answer in this case is
Input
The first line has , the number of vertices, and , the number of edges, separated by a space. Note that the real number of vertices is .
Each of the next lines has , , separated by spaces, meaning that an edge runs from vertex to vertex and takes time to cross. The edges are given so that every vertex has a path to vertex . Every vertex from to therefore has at least one outgoing edge.
Output
Print on one line the expected time to get from vertex to home, vertex , rounded to exactly six digits after the decimal point. Print all six digits even when the value is an integer. The answer never exceeds , and no input makes the rounding at the sixth decimal digit ambiguous.