#P7407. [JOI 2021 Final] 机器人 / Robot
[JOI 2021 Final] 机器人 / Robot
Description
Given an undirected graph with vertices numbered , connected by edges numbered . Edge connects vertices and . Each edge is painted with a color: the color of edge is . It is guaranteed that , but the values are not necessarily distinct.
You are very smart. If I say a color and it satisfies the following conditions:
- There exists an edge with color such that one of its endpoints is the vertex where you are currently located.
- There do not exist two or more edges with color such that one of their endpoints is the vertex where you are currently located.
then you will move along that edge to the other endpoint. Otherwise, you will stay where you are.
You are currently at vertex . I want you to move to vertex . I can tell you some colors, and you will move according to those colors. However, this graph may not allow you to go from to , so I want to change the colors of some edges. Changing the color of edge to another number in costs . Find the minimum total cost needed to make it possible for you to successfully reach vertex .
Input Format
The first line contains two integers , representing the number of vertices and the number of edges in the undirected graph.
The next lines each contain four integers , describing an edge.
Output Format
Output one integer, the minimum total cost. If it is impossible to reach, output .
4 6
1 4 4 4
3 4 1 3
1 3 4 4
2 4 3 1
2 3 3 2
1 2 4 2
3
5 2
1 4 1 2
3 5 1 4
-1
5 7
2 3 7 1
1 4 5 1
4 5 3 1
3 4 7 1
2 4 3 1
3 5 6 1
1 2 5 1
1
Hint
Explanation for Sample 1
I can do the following operations:
- Change the color of edge to , with cost .
- Change the color of edge to , with cost .
The total cost is , and then I can command you as follows:
- I say “color !”. You move from vertex to vertex .
- I say “color !”. You move from vertex to vertex .
Explanation for Sample 2
Unfortunately, even if you are this smart, you still cannot reach vertex .
Constraints
This problem uses bundled testdata.
- Subtask 1 (34 pts): , .
- Subtask 2 (24 pts): .
- Subtask 3 (42 pts): no additional constraints.
For of the testdata, , , , , , and the graph has no multiple edges.
Notes
Translated from The 20th Japanese Olympiad in Informatics Final Round D ロボット English translation Robot.
Translated by ChatGPT 5
京公网安备 11011102002149号