Genius Walk
Time limit2sMemory limit1024 MB
Find the earliest time Sam can reach city t from s when each road has periodic rain and he cannot stop midway along a road.
- Level
Medium7 of 10
- Topics
- Shortest path, Graph, Math
- Solved
- No attempts yet
Problem
Sam found cities in a new region, connected by two-way roads. Sam can only move along the roads. He needs to get from city to city without being caught in temporal rain. According to the weather forecast, rain falls over the -th road during the time intervals for all integers ( and are positive). To travel along the -th road, Sam needs units of time, and there must be no rain over that road during the entire trip. Sam can take shelter from the rain in cities, so he can be in a city at any time. Sam can leave a city onto a road at the moment the rain stops, and enter a city from a road at the moment the rain starts.
At time , Sam is in city . Find the earliest moment at which he can be in city .
Input
The first line contains four integers , , , and : the number of cities, the number of roads, the starting city, and the destination city (; ; ). Each of the next lines describes a road with five integers , , , , and (; ). Road connects cities and .
Output
If Sam cannot get from city to city , print . Otherwise, print the minimum moment at which he can be in city .