Choosing a Room
InterviewTime limit2sMemory limit1024 MB
Given a graph with random edge weights, find all rooms from which some walk to school (vertex 1) has expected total time at most T.
- Level
Medium6 of 10
- Topics
- Shortest path, Graph
- Solved
- No attempts yet
Problem
Hyeonseo, a freshman, has a lot on their mind again this semester. The biggest worry is that many morning classes make it hard to reach the lecture hall on time. After some thought, Hyeonseo decided to rent a room near school.
The area around the school is a graph with vertices and edges. The school is at vertex 1, and every other vertex has one room that Hyeonseo could rent. An edge is a road between the school and a room, or between two rooms. Roads can be used in both directions.
Each road has a traffic value. Crossing a road with traffic takes minutes in either direction. Traffic is not fixed because of various events. Specifically, the traffic on road is with probability 1/2 and with probability 1/2. Each road's traffic is chosen independently, and it can change every time Hyeonseo walks along the road. or may be negative, in which case time runs backward when crossing that road.
For every vertex other than vertex 1, let be the set of all possible walks from vertex to vertex 1.
- A walk from to starts at , follows connected roads only, and ends at .
- A walk may reuse the same road any number of times. Reaching vertex 1 does not require stopping there.
For each , define by
= (the expected time in minutes to reach vertex 1 from vertex by following walk )
Here is the set of all real numbers.
Hyeonseo only considers vertices for which some satisfies . Find all rooms that satisfy this condition.
Input
The first line contains the number of vertices and the number of edges ().
Each of the next lines contains two vertices and joined by edge , followed by integers and , separated by spaces. (; ; ) No pair of vertices is joined twice. That is, for all , if then .
Line contains the integer ().
Output
The first line contains the number of rooms that satisfy the condition. If no room satisfies it, print 0.
If the first line is not 0, the second line lists the vertex numbers of the rooms that satisfy the condition in ascending order, separated by spaces.
Hint
In the first example, for vertex 2, the walk 2 → 3 → 1 gives an expected time of 4 minutes. For vertex 3, the walk 3 → 1 gives an expected time of 2.5 minutes. As the third example shows, some rooms may be unreachable from the school.