#P4453. [国家集训队] 飞行计划

[国家集训队] 飞行计划

Description

Shenben Airlines has a flight from AA to BB and needs to plan the most economical route. To simplify, we assume the ground is a plane at height 00, with NN waypoints and MM bidirectional airways. Each airway connects two waypoints and has two parameters HH and WW, meaning that traversing this airway at altitude hh costs (Hh)2+W(H - h)^2 + W. At each waypoint you may climb or descend; each unit of climb costs CC, while descending is free. Waypoint 00 is AA, and waypoint N1N - 1 is BB.

Input Format

The first line contains 3 positive integers NN, MM, and CC, as described above. The next MM lines each contain 4 integers u,v,H,Wu, v, H, W, indicating there is an airway between uu and vv with parameters HH and WW.

Output Format

Output a single line with one integer, the minimum cost from AA to BB.

3 2 5
0 1 10 10
1 2 20 10
114

Hint

Constraints:

  • For 10% of the testdata: N,M5N, M \le 5, H200H \le 200.
  • Additionally, for 20% of the testdata: N100N \le 100, M500M \le 500, H100H \le 100.
  • For all testdata: N2000N \le 2000, M10000M \le 10000, C10C \le 10, 0u,v<N0 \le u, v < N, 0H1050 \le H \le 10^5, 0W2×1050 \le W \le 2 \times 10^5. The input guarantees the answer does not exceed a 32-bit signed integer.

Translated by ChatGPT 5