#P11898. 移动迷宫
移动迷宫
Background
Huasheng saw a small ad on a utility pole saying that the great detective Sherlock Holmes was recruiting assistants to help him catch a vicious murderer: “I don’t know”.
For some reason, Huasheng came to Holmes’s residence for an interview. But Holmes lives in a… moving maze?
Problem Description
This maze has rooms and bidirectional roads. The -th road connects rooms and , with length .
Holmes’s maze changes over time: every time you pass through a road (arrive at a room), all roads will expand or shrink. If a road’s original length is , then after the change its length becomes . (If you do not know how to take a fraction modulo, see P2613; also note that negative values modulo may be involved.)
Huasheng is in room . According to Huasheng’s calculations, Holmes lives in room .
Please help Huasheng reach room as fast as possible to find Holmes.
Negative modulo: if , then the result of equals the result of .
is a prime number.
Input Format
The first line contains two positive integers .
The next lines each contain three positive integers , describing an edge.
Output Format
Output one positive integer, the shortest distance to reach room .
5 7
1 2 3
2 3 8
3 5 1000
2 4 100
4 5 6
1 4 78888
1 3 114514
151073832
6 8
1 3 100000000
1 5 200000000
2 5 300000000
2 6 400000000
3 4 500000000
5 6 600000000
4 5 700000000
3 6 303063652
403063652
Hint
Sample #1 Explanation
Along the path , the path length is .
For of the testdata, .
For another of the testdata, all edge weights are equal.
For of the testdata, . The graph is guaranteed to be connected, , , and it is guaranteed that at any moment there will never be an edge with weight .
All input numbers are integers.
Postscript:
After Huasheng arrived at the room where Holmes lived, he saw Holmes staring at the screen without blinking. “What are you looking at?”
Holmes: “I don’t know.”
Translated by ChatGPT 5