#P51174. [NOI 2019] Route to Home
[NOI 2019] Route to Home
Description
Duplicated of #6520. 「ICPC PacNW 2017 Div.1」David 的旅程
There are railway stations in the Kingdom of Cat, numbered from to . Little cat is going home (at station ) from station . There are trains in the kingdom, numbered from to . For the -th train, it will depart station at time , and arrive at station station directly at time . At time , little cat is at station .
Little cat can go home by multiple transfers. One transfer is that for a pair of trains and , if and , then after taking train , little cat can wait at station for time units, and take train at time .
Little cat wants to minimize its anxiety, which is calculated as followed:
- If little cat waits at some stations for time units, its anxiety will be increased by , where are given constants.
- Note that before little cat taking the first train at time , it has been waiting for time units at station .
- If little cat arrives at station at time , its anxiety will be increased by .
Formally, if little cat takes trains totally, whose numbers are , little cat can get home if and only if:
- and hold for
and its anxiety will be
$$q_{s_k} + (A \cdot p_{s_1}^2 + B \cdot p_{s_1} + C) + \sum_{j = 1}^{k - 1}\left(A(p_{s_{j + 1}} - q_{s_j})^2 + B(p_{s_{j + 1}} - q_{s_j}) + C\right) $$Your task is to figure out the minimal anxiety.
Input
The first line contains integers .
Each of the next lines contains integers .
It is guaranteed that there is at least one way for little cat to go home.
Output
Print a single integer — the minimal anxiety.
Sample
3 4 1 5 10
1 2 3 4
1 2 5 7
1 2 6 8
2 3 9 10
94
There are ways to go home.
- Take train in turn, and the anxiety is $10 + 1 \times 3^2 + 5 \times 3 + 10 + 1 \times (9-4)^2$
- Take train in turn, and the anxiety is $10 + 1 \times 5^2 + 5 \times 5 + 10 + 1 \times (9-7)^2$
- Take train in turn, and the anxiety is $10 + 1 \times 6^2 + 5 \times 6 + 10 + 1 \times (9-8)^2$
So the minimal anxiety is .
4 3 1 2 3
1 2 2 3
2 3 5 7
3 4 7 9
34
Limits And Hints
For all of the tests, , , , , , , .