#P1813. 拯救小 tim

拯救小 tim

Description

Little Tim was in an amusement park and finally escaped one day! But he was accidentally spotted by the staff again… So your task is to safely escort Little Tim home. However, the complex traffic in City A gives you a big challenge.

City A has nn intersections and mm one-way roads. Each road is only open during a certain time interval. For safety, you must choose an escort route that minimizes Tim’s time on the road, that is, minimize the arrival time minus the time when he leaves the amusement park.

Input Format

The first line contains 44 numbers, n,m,s,tn,m,s,t. The base is at intersection ss, and the dock is at intersection tt.

Each of the next mm lines contains 55 numbers x,y,b,e,cx,y,b,e,c, describing a directed road from intersection xx to intersection yy, which is open from time bb to time ee, and takes cc time to traverse (you must ensure that the road remains open during the entire traversal; otherwise Tim will be caught). There may be multiple roads between two intersections. The start time is 00 (of course, you don’t have to depart immediately; you may wait at the base).

If no plan exists that allows Tim to reach the dock, output Impossible.

Output Format

Output one line: the minimal time Tim spends on the road.

4 5 1 4 
1 2 0 1 1 
1 2 0 1 2 
1 3 1 3 2 
2 4 3 4 1 
3 4 3 4 1 
3

Hint

[Sample Explanation #1]

The optimal plan is to stay at node 11 until time 11, then go to node 33, then go to node 44. The arrival time is 44. Tim’s time on the road is 41=34-1=3.

Constraints

For all testdata:

  • 2n1002\leqslant n\leqslant 100, 0m10000\leqslant m\leqslant 1000, 1s,tn1\leqslant s,t\leqslant n, sts\not= t.
  • 1x,yn1\leqslant x,y\leqslant n, 0b,e,c100000\leqslant b,e,c\leqslant 10000, b<eb< e.

Translated by ChatGPT 5