#P3783. [SDOI2017] 天才黑客
[SDOI2017] 天才黑客
Description
The intranet has nodes (numbered from to ) and directed cables. The central control system is at node . Each cable connects an ordered pair of nodes, and starting from node you can reach any other node by following some cables. Any node can run any application. An application carries a communication passcode. A program can traverse a cable if and only if its current passcode equals the cable’s passcode. Traversing any cable takes some amount of time.
Each application can modify its communication passcode at any node. The time to change the passcode itself can be neglected. However, to minimize the amount of modification, you must first call a subroutine to compute the longest common prefix (denote its length by ) between the program’s current passcode and the cable’s passcode. Because obtaining any character of the cable’s passcode is time‑consuming, calling this subroutine once costs time units.
In addition, Xiao Q found a dictionary in the central control system. Every cable’s passcode is some string from this dictionary. Specifically, this dictionary is a rooted tree with nodes (numbered from to ), rooted at node . Each edge carries a character. A string is in the dictionary if and only if there exists a node such that the characters along the path from the root to concatenated in order form .
Now Xiao Q simultaneously starts applications at node . These programs run concurrently without interference. Each program’s initial passcode is empty. He wants to send these programs to the other nodes in the shortest possible time. You need to help Xiao Q compute, for each (), the minimal time for the program destined for node to finish its task.
Input Format
The first line contains a positive integer , the number of test cases.
For each test case, the first line contains three integers , the number of intranet nodes, the number of cables, and the number of dictionary tree nodes, respectively.
The next lines each contain four integers , meaning that there is a directed cable from node to node whose traversal time is , and whose passcode equals the string formed by the characters along the path from the dictionary root to node (which can be empty). Note that the intranet may have self‑loops and multiple edges.
The next lines each contain three integers , indicating there is an edge in the dictionary tree whose label is a character . It is guaranteed that the given edges form a rooted tree with root , and the labels on the outgoing edges of any node are pairwise distinct.
Output Format
For each test case, output lines. The ‑th line is the minimal time for the program to reach node .
1
4 4 6
1 2 2 5
2 3 2 5
2 4 1 6
4 2 1 6
1 2 1
2 3 1
3 4 1
4 5 2
1 6 2
2
7
3
Hint
Sample explanation: The following figure shows the intranet structure in the sample. Strings are marked in red.

Let be the length of the longest common prefix of strings and . For example, $\mathrm{LCP}(\texttt{\textcolor{red}{starry}killer},\texttt{\textcolor{red}{starry}dust})=6$. Let be the empty string.
One feasible path from to is . The required time is $(2 + \mathrm{LCP}(\epsilon , \texttt{1112})) + (2 + \mathrm{LCP}(\texttt{1112} , \texttt{1112})) = 8$.
However, this path is not optimal. The optimal path is $1 \rightarrow 2 \rightarrow 4 \rightarrow 2 \rightarrow 3$.
| Test point index | Remarks | |||
|---|---|---|---|---|
| - | ||||
| - | ||||
For of the testdata, it is guaranteed that:
- .
- , , .
- The number of test cases with or does not exceed .
- , , .
- , .
Translated by ChatGPT 5
京公网安备 11011102002149号