luogu#P4673. [BalticOI 2005] Bus Trip (Day2)

    ID: 3663 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 6 上传者: 标签>动态规划 DP2005拓扑排序BalticOI(波罗的海)

[BalticOI 2005] Bus Trip (Day2)

Problem Description

There are NN towns and MM one-way direct bus routes between the towns (with no intermediate stops). The towns are numbered from 11 to NN. A traveler is in town 11 at time 00 and wants to reach town PP. He will arrive at town PP at time TT by taking buses. If he arrives earlier, he must wait.

For each bus route ii, we know its starting town sis_i and destination town tit_i. We also know its departure and arrival times, but only approximately: we know the bus leaves the starting town sis_i within the time interval [ai,bi][a_i, b_i], and arrives at the destination town tit_i within the time interval [ci,di][c_i, d_i] (endpoints included).

The traveler dislikes waiting, so he wants to find a travel plan that minimizes the maximum waiting time, while also guaranteeing that he will never miss any bus in the plan (that is, each time he transfers, the latest possible arrival time of the bus he gets off is not later than the earliest possible departure time of the next bus he needs to take).

When computing waiting time, we must assume the earliest possible arrival time and the latest possible departure time.

Write a program to help the traveler find a suitable plan.

Input Format

The first line contains integers N,M,P,TN, M, P, T, as described above.

The next MM lines describe the bus routes. Each line contains integers si,ti,ai,bi,ci,dis_i, t_i, a_i, b_i, c_i, d_i, where sis_i and tit_i are the starting town and destination of route ii, and ai,bi,ci,dia_i, b_i, c_i, d_i describe the departure and arrival times.

Output Format

Output one line containing the maximum possible total waiting time for the best possible travel plan. If it is impossible to guarantee arrival at town PP at time TT, this line should contain -1.

3 6 2 100
1 3 10 20 30 40
3 2 32 35 95 95
1 1 1 1 7 8
1 3 8 8 9 9
2 2 98 98 99 99
1 2 0 0 99 101
32

Hint

Constraints

For 100%100\% of the testdata, 1PN5×1041 \leq P \leq N \leq 5\times 10^4, 1M1051 \leq M \leq 10^5, 0T1090 \leq T \leq 10^9, 1si,tiN1 \leq s_i, t_i \leq N, 0aibi<cidi1090 \leq a_i \leq b_i < c_i \leq d_i \leq 10^9.

Notes

Translated from BalticOI 2005 Day2 B Bus Trip

Translated by ChatGPT 5