#abc232g. G - Modulo Shortest Path
G - Modulo Shortest Path
Score : points
问题描述
我们有一个有向图,包含 个顶点,分别称为顶点 , 顶点 , , 顶点 。
对于每一对整数 和 ,满足 且 ,存在一条从顶点 到顶点 的有向边,其权重为 。(此处, 表示 除以 后的余数。)
除此之外没有其他边。
请输出从顶点 到顶点 的最短距离,即从顶点 到顶点 的路径中所有边的最小可能总权重。
以上为通义千问 qwen-max 翻译,仅供参考。
Problem Statement
We have a directed graph with vertices, called Vertex , Vertex , , Vertex .
For each pair of integers such that and , there is a directed edge from Vertex to Vertex of weight . (Here, denotes the remainder when is divided by .)
There is no edge other than the above.
Print the shortest distance from Vertex to Vertex , that is, the minimum possible total weight of the edges in a path from Vertex to Vertex .
Constraints
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
Output
Print the minimum possible total weight of the edges in a path from Vertex to Vertex .
Sample Input 1
4 12
10 11 6 0
8 7 4 1
Sample Output 1
3
Below, denotes the directed edge from Vertex to Vertex .
Let us consider the path .
- Edge weighs ,
- Edge weighs ,
- Edge weighs .
Thus, the total weight of the edges in this path is .
This is the minimum possible sum for a path from Vertex to Vertex .
Sample Input 2
10 1000
785 934 671 520 794 168 586 667 411 332
363 763 40 425 524 311 139 875 548 198
Sample Output 2
462
update @ 2024/3/10 10:07:39