#P6606. [Code+#7] 最小路径串
[Code+#7] 最小路径串
Description
In an undirected graph with vertices and edges, every vertex is labeled by a -digit string starting from 0, i.e., 000000, 000001, 000002, , up to the -digit string corresponding to . It is guaranteed that , so the -digit labels will not overflow.
For every vertex except 000000, you need to find a path starting from 000000 that does not pass through any repeated vertex, such that the string formed by concatenating the digit strings of all vertices on the path in order is lexicographically smallest. To compare the lexicographical order of two different strings: if one string is a prefix of the other, then the shorter string is lexicographically smaller; otherwise, scan from left to right to find the first position where they differ, and the string with the smaller digit at that position is lexicographically smaller.
Since outputting the path is too troublesome, you do not need to output the whole path. You only need to treat the concatenation of all vertex digit strings on the path as an integer, and output this number modulo .
Input Format
The first line contains two integers and .
The second line contains a digit string of length , which describes each edge in order. Each edge is represented by digits: the first digits and the last digits are the labels of the two vertices connected by this edge.
Note that the input may contain self-loops or multiple edges.
Output Format
Output lines. For each vertex other than 000000, output the result of taking the lexicographically smallest path from 000000 to that vertex, treating the concatenated string as an integer and taking it modulo . If a vertex is not reachable from 000000, output on the corresponding line instead.
5 5
000000000003000001000003000001000002000002000000000002000003
2000001
2
517560944
-1
Hint
Sample Explanation
- From
000000to000001, the required path corresponds to the string000000000002000001. - From
000000to000002, the required path corresponds to the string000000000002. - From
000000to000003, the required path corresponds to the string000000000002000001000003, and after taking modulo it is . - From
000000to000004, there is no path.
Subtasks
Subtask ( points)
- .
Subtask ( points)
- .
Subtask ( points)
- .
Translated by ChatGPT 5
京公网安备 11011102002149号