Infimum of Paths
Time limit8sMemory limit256 MB
Given a directed graph with digit-weighted edges, find the infimum of the decimal values of all paths from node 0 to node 1, modulo 1e9+7.
- Level
Medium7 of 10
- Topics
- Graph, Greedy, Shortest path
- Solved
- No attempts yet
Problem
On a directed graph, we use to denote the lexical weight of a path , where the path can be regarded as a sequence of consecutive edges. The lexical weight is defined by the recurrence relation
where is the weight of edge , an integer between and , inclusive.
Given a directed graph, find the infimum of the lexical weights of all paths from node to node . The infimum of a set of rational numbers is the greatest rational number that, if it exists, is less than or equal to every element of the set.
Input
The first line contains the number of test cases (). Then test cases follow.
Each test case starts with two integers (, ) and (, ), where is the number of nodes and is the number of edges.
The next lines each contain three integers , , (, ), meaning there is an edge from to with weight .
It is guaranteed that at least one path from node to node exists in each test case.
Output
For each test case, output one line in the format "Case #x: y", where is the test case number starting from . The value is the answer modulo . Specifically, if the answer can be written as an irreducible fraction , then is .
Hint
In the first test case, the path that achieves the infimum is , so the answer is .