#P6302. [NOI2019] 回家路线 加强版

    ID: 7080 远端评测题 1000ms 256MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>动态规划 DP2019NOIO2优化斜率优化

[NOI2019] 回家路线 加强版

Background

This problem is an enhanced version of NOI 2019 Route Home. Except for the Constraints, it is the same as the original problem.

Problem Description

In the railway system of Cat Kingdom, there are nn stations, numbered from 1n1 - n. The kitten plans to start from station 11 and take trains to return to station nn, where its home is. It checked the trains it can take. There are mm trains in total, numbered from 1m1 - m. The kitten will arrive at station 11 at time 00. For train ii, it departs from station xix_i at time pip_i and arrives directly at station yiy_i at time qiq_i. The kitten can only board train ii at time pip_i, and can only get off train ii at time qiq_i. The kitten can reach station nn by transferring multiple times. A transfer means: for two trains, suppose they are train uu and train vv. If yu=xvy_u = x_v and qupvq_u \leq p_v, then after taking train uu, the kitten can wait at station yuy_u for pvqup_v - q_u time units, and board train vv at time pvp_v.

The kitten only wants to get home and reduce the trouble on the way, so it uses an irritation value to measure this.

  • When the kitten waits at a station, its irritation value increases. For one wait of t(t0)t (t \geq 0) time units, the irritation value increases by At2+Bt+CAt^2 + Bt + C, where A,B,CA, B, C are given constants. Note: the time spent staying at station 11 starting from time 00 before boarding the first train is also counted as one wait.

  • If the kitten finally arrives at station nn at time zz, the irritation value will further increase by zz.

Formally, suppose the kitten takes kk trains in total, and the train numbers it takes in order can be written as a sequence s1,s2,,sks_1, s_2, \cdots , s_k. This plan is called a feasible route home if and only if it satisfies the following two conditions:

  • xs1=1,ysk=nx_{s1} = 1, y_{sk} = n

  • For all j(1j<k)j (1 \leq j < k), ysj=xsj+1y_{sj} = x_{s_{j+1}} and qsjpsj+1q_{sj}\leq p_{s_{j+1}}

For this route home, the irritation value the kitten gets is:

$$q_{s_k}+(A\times p_{s_1}^2+B\times p_{s_1}+C)+\sum_{j=1}^{k-1}(A(p_{s_{j+1}}-q_{s_j})^2+B(p_{s_{j+1}}-q_{s_j})+C)$$

The kitten wants the irritation value to be as small as possible. Please help it find the minimum irritation value among all feasible routes home. The problem guarantees that there is at least one feasible route home.

Input Format

The first line contains five integers n,m,A,B,Cn, m, A, B, C. The meanings of the variables are given in the description.

The next mm lines describe the trains. In line ii, there are four integers xi,yi,pi,qix_i, y_i, p_i, q_i, representing the departure station, arrival station, departure time, and arrival time of train ii, respectively.

Output Format

Output only one line with one integer, the required answer.

3 4 1 5 10
1 2 3 4
1 2 5 7
1 2 6 8
2 3 9 10
94
4 3 1 2 3
1 2 2 3
2 3 5 7
3 4 7 9
34

Hint

For all testdata, it is guaranteed that 2n1052\le n\le 10^5, 1m1061\le m\le 10^6, 0A100\le A\le 10, 0B,C1070\le B, C\le 10^7, 1xi,yin1\le x_i, y_i\le n, xiyix_i\neq y_i, and 0pi<qi4×1040\le p_i<q_i\le 4\times 10^4.

Translated by ChatGPT 5