创建于 2026/07/18 · 更新于 2026/07/18
You are given a directed graph with vertices (numbered to ) and edges. Each edge is labeled with either a positive integer, a plus sign ('+'), a multiplication sign ('*'), a left parenthesis ('('), or a right parenthesis (')').
Consider a path from vertex to vertex . Concatenating the labels of the edges along the path (in the order they are traversed) yields a string . We say the path forms a valid arithmetic expression if , where is generated by the following rules:
For every ordered pair such that there exists at least one path from to forming a valid arithmetic expression, compute the minimum possible value (evaluated according to the standard arithmetic rules) of such an expression. If the minimum value exceeds , cap it at (i.e., report for any value ).
Note that in this problem, concatenating two integers directly is not allowed. That is, if there are two edges , , the path does not form a valid arithmetic expression.
The first line contains a single integer , the number of test cases.
For each test case:
There may be multiple edges and loops (edges from a vertex to itself).
It is guaranteed that the sum of over all test cases does not exceed , and the sum of over all test cases does not exceed .
For each test case, first print a single integer — the number of ordered pairs for which there exists at least one path from to forming a valid arithmetic expression.
Then output lines. Each line must contain three integers , , and (, ), meaning that the minimum value among all valid expressions on paths from to is , capped at .
The lines must be sorted in lexicographic order of the pair : first by ascending, then by ascending.
3
2 1
1 2 42
3 3
1 2 3
2 3 0
3 1 4
4 4
1 2 -2
2 3 9
3 4 -3
1 3 8
1
1 2 42
3
1 1 7
1 2 3
3 1 4
3
1 3 8
1 4 9
2 3 9