GPS Hack
Time limit1sMemory limit1024 MB
Count the s-to-t routes with total time exactly L, where the hacked GPS may deviate from a shortest path at most once, modulo 1e9+7.
- Level
Hard8 of 10
- Topics
- Shortest path, Dynamic programming, Graph
- Solved
- No attempts yet
Problem
Denis is a student and he failed his data security exam, so today he is retaking it. He woke up late and decided to take a taxi.
Denis lives in a city that can be represented as crossroads connected by bidirectional roads. The time needed to drive along each road is known. Denis lives near crossroad , and the university is near crossroad . As soon as the taxi reaches the university, Denis leaves it and rushes to his exam.
Unfortunately, the situation is more complicated. Hackers have hacked the GPS in Denis's taxi. When the GPS works properly, it leads you to the destination as fast as possible. At every crossroad it shows which road to take to finish the journey in the fastest way. If more than one road is possible, the GPS can show any of them. Hackers can make the GPS show any road they want, but at most once.
Denis does not remember exactly how he got to the university, but he remembers that the ride took time. The teacher knew about the GPS attack, so he asked Denis to count the number of different possible routes from his home to the university as part of his retake.
Help Denis count the routes from crossroad to crossroad that take time and are possible given the GPS hack. Since the number can be very large, print it modulo .
Input
The first line contains two integers and , the number of crossroads and roads in the city (, ).
The second line contains three integers , , and , the crossroad where Denis lives, the crossroad near the university, and the ride time (, , ).
The next lines each contain three integers , , (, , ), the two crossroads connected by the road and the time needed to drive along it.
All roads are bidirectional, and each pair of crossroads is connected by at most one road. No road connects a crossroad to itself. It is guaranteed that a route from to exists.
Output
Print the number of routes Denis could have taken from to with total time , modulo .