Under construction

Still being built. Things may change

Infimum of Paths

Time limit8sMemory limit256 MB

Summary
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 lex(p)lex(p) to denote the lexical weight of a path pp, where the path pp can be regarded as a sequence of consecutive edges. The lexical weight is defined by the recurrence relation

lex(\[])=0,lex(\[e1,e2,…,en])=w(e1)+lex(\[e2,e3,…,en])10,lex(\[]) = 0, lex(\[e_1, e_2, \ldots, e_n]) = \frac{w(e_1) + lex(\[e_2, e_3, \ldots, e_n])}{10}\text{,}

where w(e1)w(e_1) is the weight of edge e1e_1, an integer between 00 and 99, inclusive.

Given a directed graph, find the infimum of the lexical weights of all paths from node 00 to node 11. 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 TT (1≤T≤1001 \le T \le 100). Then TT test cases follow.

Each test case starts with two integers nn (2≤n≤20002 \le n \le 2000, ∑n≤20000\sum{n} \le 20000) and mm (1≤m≤40001 \le m \le 4000, ∑m≤40000\sum{m} \le 40000), where nn is the number of nodes and mm is the number of edges.

The next mm lines each contain three integers uu, vv, ww (0≤u,v<n0 \le u, v < n, 0≤w≤90 \le w \le 9), meaning there is an edge from uu to vv with weight ww.

It is guaranteed that at least one path from node 00 to node 11 exists in each test case.

Output

For each test case, output one line in the format "Case #x: y", where xx is the test case number starting from 11. The value yy is the answer modulo (109+7)(10^9 + 7). Specifically, if the answer can be written as an irreducible fraction AB\frac{A}{B}, then yy is (A⋅B−1) mod (109+7)(A \cdot B^{-1}) \bmod (10^9 + 7).

Hint

In the first test case, the path that achieves the infimum is 0→2→4→10 \to 2 \to 4 \to 1, so the answer is 0.3130.313.

Examples1

  1. Example 1

    Input
    2
    5 5
    0 2 3
    2 3 4
    2 4 1
    3 1 2
    4 1 3
    5 6
    0 1 9
    2 0 6
    3 0 1
    0 3 3
    4 0 3
    4 2 7
    
    Expected output
    Case #1: 241000002
    Case #2: 40404041