题目收集
题目列表题单
返回列表

Hard Autograd for Algebraic Expressions

CodeforcesP0072· 3000ms· 256 MB· ZJU Summer 2026 Contest 7原题链接
图论最短路

创建于 2026/07/18 · 更新于 2026/07/18

题目描述

You are given a directed graph with nnn vertices (numbered 111 to nnn) and mmm 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 uuu to vertex vvv. Concatenating the labels of the edges along the path (in the order they are traversed) yields a string sss. We say the path forms a valid arithmetic expression if s∈Ss\in Ss∈S, where SSS is generated by the following rules:

  1. For every string xxx representing a positive integer, x∈Sx\in Sx∈S.
  2. If both xxx and yyy belong to SSS, x+yx\texttt{+}yx+y and x*yx\texttt{*}yx*y are in SSS.
  3. If x∈Sx\in Sx∈S, (x)∈S\texttt{(}x\texttt{)}\in S(x)∈S.
  4. There are no other strings in SSS.

For every ordered pair (u,v)(u, v)(u,v) such that there exists at least one path from uuu to vvv 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 10910^9109, cap it at 10910^9109 (i.e., report 10910^9109 for any value ≥109\geq 10^9≥109).

Note that in this problem, concatenating two integers directly is not allowed. That is, if there are two edges e1=(1,2,123)e_1=(1,2,\texttt{123})e1​=(1,2,123), e2=(2,3,234)e_2=(2,3,\texttt{234})e2​=(2,3,234), the path e1e2e_1e_2e1​e2​ does not form a valid arithmetic expression.

输入格式

The first line contains a single integer TTT, the number of test cases.

For each test case:

  • The first line contains two integers nnn and mmm (1≤n≤3001\leq n\leq 3001≤n≤300, 1≤m≤1061\leq m\leq 10^61≤m≤106), the number of vertices and the number of edges.
  • Each of the following mmm lines contains three integers u,v,wu, v, wu,v,w (1<=u,v<=n1 \lt =u,v \lt =n1<=u,v<=n, −3≤w≤109-3\leq w\leq 10^9−3≤w≤109), describing a directed edge from vertex uuu to vertex vvv. The label of the edge is interpreted as follows:
    • If w>0w \gt 0w>0: the edge is labeled with the positive integer www.
    • If w=0w=0w=0: the edge is labeled with '+'.
    • If w=−1w=-1w=−1: the edge is labeled with '*'.
    • If w=−2w=-2w=−2: the edge is labeled with '('.
    • If w=−3w=-3w=−3: the edge is labeled with ')'.

There may be multiple edges and loops (edges from a vertex to itself).

It is guaranteed that the sum of nnn over all test cases does not exceed 300300300, and the sum of mmm over all test cases does not exceed 10610^6106.

输出格式

For each test case, first print a single integer KKK — the number of ordered pairs (u,v)(u, v)(u,v) for which there exists at least one path from uuu to vvv forming a valid arithmetic expression.

Then output KKK lines. Each line must contain three integers uuu, vvv, and ccc (1≤u,v≤n1 \le u, v \le n1≤u,v≤n, 0≤c≤1090 \le c \le 10^90≤c≤109), meaning that the minimum value among all valid expressions on paths from uuu to vvv is ccc, capped at 10910^9109.

The KKK lines must be sorted in lexicographic order of the pair (u,v)(u, v)(u,v): first by uuu ascending, then by vvv ascending.

样例输入 #1

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
1 2 42
3
1 1 7
1 2 3
3 1 4
3
1 3 8
1 4 9
2 3 9