#P6880. [JOI 2020 Final] 奥运公交 / Olympic Bus

[JOI 2020 Final] 奥运公交 / Olympic Bus

Description

Given a directed graph with NN vertices and MM edges, the vertices are numbered from 11 to NN. Each edge goes from UiU_i to ViV_i, and the cost to traverse this edge is CiC_i. The graph may contain multiple edges.

At the very beginning, you may reverse at most one edge, permanently changing it from UiViU_i \to V_i to ViUiV_i \to U_i, and paying an additional cost of DiD_i.

You need to travel from vertex 11 to vertex NN, and then from vertex NN back to vertex 11. You want to know: by reversing at most one edge, what is the minimum possible total cost?

Input Format

The first line contains two integers N,MN, M, representing the number of vertices and the number of edges.

The next MM lines each contain four integers Ui,Vi,Ci,DiU_i, V_i, C_i, D_i, describing an edge.

Output Format

Output one integer: the minimum total cost. If there is no solution, output 1-1.

4 5
1 2 4 4
1 3 2 1
4 3 1 2
4 1 6 1
2 4 2 5
10
4 10
1 2 4 4
1 2 4 4
1 3 2 1
1 3 2 1
4 3 1 2
4 3 1 2
4 1 6 1
4 1 6 1
2 4 2 5
2 4 2 5
10
4 4
1 2 0 4
1 3 0 1
4 3 0 2
4 1 0 1
2
4 5
1 2 4 4
1 3 2 4
4 3 1 5
4 1 6 1
2 4 2 5
12
4 5
2 1 4 4
1 3 2 1
4 3 1 2
4 3 6 1
2 4 2 5
-1

Hint

Explanation for Sample 1

The optimal solution is to reverse the second edge. The total cost is:

  • The reversing cost is 11.
  • The shortest path from vertex 11 to vertex NN and then back is 124311 \to 2 \to 4 \to 3 \to 1, with cost 4+2+1+2=94+2+1+2=9.

Explanation for Sample 4

It is not necessary to reverse any edge.

Explanation for Sample 5

There are two edges from vertex 44 to vertex 33.

Constraints

This problem uses bundled testdata.

  • Subtask 1 (5 pts): M1000M \le 1000.
  • Subtask 2 (11 pts): MM is even, and U2i=U2i1U_{2i}=U_{2i-1}, V2i=V2i1V_{2i}=V_{2i-1}, C2i=C2i1C_{2i}=C_{2i-1}.
  • Subtask 3 (21 pts): Ci=0C_i=0.
  • Subtask 4 (63 pts): No additional constraints.

For 100%100\% of the testdata:

  • 2N2002 \le N \le 200.
  • 1M5×1041 \le M \le 5 \times 10^4.
  • 1UiN1 \le U_i \le N.
  • 1ViN1 \le V_i \le N.
  • UiViU_i \ne V_i.
  • 0Ci1060 \le C_i \le 10^6.
  • 0Di1090 \le D_i \le 10^9.

Notes

Translated from The 19th Japanese Olympiad in Informatics, Final Round, D Olympic Bus.

Input Format

Output Format

Hint

Translated by ChatGPT 5