#P51434. [JOI 2021 Final] Robot
[JOI 2021 Final] Robot
Description
There are crossings in the IOI town, numbered from to . There are roads, numbered from to .
Each road connects two different crossings in both directions. The road () connects the crossing and the crossing . No two different roads connect the same pair of crossings. Each of the roads has a color, which is described as an integer between and , inclusive. Currently, the color of the road is . More than one road may have the same color.
The JOI Co., Ltd. developed a robot moving around the crossings of the IOI town. Whenever you tell a color to the robot, the robot will find the road with that color, and then the robot will pass through it and moves to the adjacent crossing. However, if there are more than one roads with the told color connected to the current crossing of the robot, it cannot decide which road it should pass through, and will halt.
The robot is currently in the crossing . Your task is to move the robot to the crossing by telling colors to it. However, it is not always true that the robot can be moved to the crossing . You may change the colors of some of the roads in advance so that the robot can be moved to the crossing . It costs yen to change the color of the road () to any color between and , inclusive.
Write a program which, given the information of the crossings and the roads, calculates the minimum total cost. However, if it is impossible to move the robot to the crossing N even if you change the colors of the roads, output -1
instead.
Input
Read the following data from the standard input. Given values are all integers.
Output
Write one line to the standard output. The output should contain the minimum total cost. However, if it is impossible to move the robot to the crossing even if you change the colors of the roads, output -1
instead.
Sample 1
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
You can change the color of the road from color to color at the cost of yen. You can change the color of the road from color to color at the cost of yen. The total cost is yen.
After that, you tell the color to the robot, then it moves from the crossing to the crossing . And, you tell the color to the robot, then it moves to the crossing .
It is impossible to pay less than yen so that the robot can be moved to the crossing . Hence output .
5 2
1 4 1 2
3 5 1 4
-1
It is impossible to move the robot to the crossing even if you change the colors of the roads. Hence output -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
This sample input satisfies the constraints of the subtask .
Constraints
- .
- .
- ().
- ().
- ().
- ().
Subtasks
- ( points) , .
- ( points) ().
- ( points) No additional constraints.