#BS0000424. Cuộc gọi trên đường đi (Call During the Journey)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Cuộc gọi trên đường đi (Call During the Journey)
Call During the Journey
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
Your city has intersections numbered through and bidirectional streets. Every street joins two distinct intersections, no pair is joined by more than one street, and every intersection is reachable from every other one.
Street connects and . Traversing it takes minutes by bus or minutes on foot. Buses may depart at any integer minute; boarding and leaving a bus are allowed only at intersections. Waiting at intersections is allowed.
You are at home, intersection , at time . You must arrive at intersection by the event deadline . A scheduled call occupies the interval from to . During the call you may walk, wait, or stay home, but may not ride a bus. A ride ending exactly at or beginning exactly at is permitted.
Find the latest integer departure time from home that meets the deadline and respects the call restriction.
Input
The first line contains integers , the numbers of intersections and streets.
The second line contains integers (in minutes), the event deadline, call start, and call end.
Each of the next lines contains four integers : the two endpoints, bus travel time, and walking time for street .
Output
Print one integer: the latest possible departure time, measured from time 0. Print -1 if the event cannot be reached by the deadline.
Subtasks
- Subtask 1 (20 points): ; ; ; ; ; ; for every . The undirected graph is connected, with no parallel edges.
- Subtask 2 (30 points): ; ; ; ; ; ; for every . The undirected graph is connected, with no parallel edges.
- Subtask 3 (50 points): ; ; ; ; ; ; for every . The undirected graph is connected, with no parallel edges.
Examples
Input 1
2 1
10 4 8
1 2 3 7
Output 1
3
Explanation 1
A bus can depart at 1 and arrive at 4. Walking takes 7 minutes, so departure at 3 reaches the event at 10. The first bus after the call arrives at 11. The latest departure is 3.
Input 2
3 2
12 4 8
1 2 2 6
2 3 4 9
Output 2
2
Explanation 2
Leave at 2, reach intersection 2 at 4 by bus, wait until 8, and take the 4-minute bus to intersection 3 at 12. A later departure cannot reach intersection 2 by 4 by bus or by 8 on foot, so the deadline cannot be met.
Input 3
2 1
5 1 4
1 2 3 6
Output 3
-1
Explanation 3
Walking needs 6 minutes, exceeding the deadline even from time 0. A 3-minute bus cannot finish by time 1, and the first bus after the call arrives at 7. No feasible trip exists.