#P6880. [JOI 2020 Final] 奥运公交 / Olympic Bus
[JOI 2020 Final] 奥运公交 / Olympic Bus
Description
Given a directed graph with vertices and edges, the vertices are numbered from to . Each edge goes from to , and the cost to traverse this edge is . The graph may contain multiple edges.
At the very beginning, you may reverse at most one edge, permanently changing it from to , and paying an additional cost of .
You need to travel from vertex to vertex , and then from vertex back to vertex . 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 , representing the number of vertices and the number of edges.
The next lines each contain four integers , describing an edge.
Output Format
Output one integer: the minimum total cost. If there is no solution, output .
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 .
- The shortest path from vertex to vertex and then back is , with cost .
Explanation for Sample 4
It is not necessary to reverse any edge.
Explanation for Sample 5
There are two edges from vertex to vertex .
Constraints
This problem uses bundled testdata.
- Subtask 1 (5 pts): .
- Subtask 2 (11 pts): is even, and , , .
- Subtask 3 (21 pts): .
- Subtask 4 (63 pts): No additional constraints.
For of the testdata:
- .
- .
- .
- .
- .
- .
- .
Notes
Translated from The 19th Japanese Olympiad in Informatics, Final Round, D Olympic Bus.
Input Format
Output Format
Hint
Translated by ChatGPT 5
京公网安备 11011102002149号