#P7407. [JOI 2021 Final] 机器人 / Robot

[JOI 2021 Final] 机器人 / Robot

Description

Given an undirected graph with NN vertices numbered 1N1 \sim N, connected by MM edges numbered 1M1 \sim M. Edge ii connects vertices AiA_i and BiB_i. Each edge is painted with a color: the color of edge ii is CiC_i. It is guaranteed that Ci[1,M]C_i \in [1,M], but the values CiC_i are not necessarily distinct.

You are very smart. If I say a color LL and it satisfies the following conditions:

  • There exists an edge with color LL such that one of its endpoints is the vertex where you are currently located.
  • There do not exist two or more edges with color LL 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 11. I want you to move to vertex NN. I can tell you some colors, and you will move according to those colors. However, this graph may not allow you to go from 11 to NN, so I want to change the colors of some edges. Changing the color of edge ii to another number in [1,M][1,M] costs PiP_i. Find the minimum total cost needed to make it possible for you to successfully reach vertex NN.

Input Format

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

The next MM lines each contain four integers Ai,Bi,Ci,PiA_i,B_i,C_i,P_i, describing an edge.

Output Format

Output one integer, the minimum total cost. If it is impossible to reach, output 1-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
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 44 to 44, with cost 11.
  • Change the color of edge 66 to 22, with cost 22.

The total cost is 33, and then I can command you as follows:

  • I say “color 22!”. You move from vertex 11 to vertex 22.
  • I say “color 44!”. You move from vertex 22 to vertex 44.

Explanation for Sample 2

Unfortunately, even if you are this smart, you still cannot reach vertex NN.

Constraints

This problem uses bundled testdata.

  • Subtask 1 (34 pts): N1000N \le 1000, M2000M \le 2000.
  • Subtask 2 (24 pts): Pi=1P_i=1.
  • Subtask 3 (42 pts): no additional constraints.

For 100%100\% of the testdata, 2N1052 \le N \le 10^5, 1M2×1051 \le M \le 2 \times 10^5, 1Ai<BiN1 \le A_i<B_i \le N, 1CiM1 \le C_i \le M, 1Pi1091 \le P_i \le 10^9, 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