#P3783. [SDOI2017] 天才黑客

    ID: 2725 远端评测题 2000~3000ms 500MiB 尝试: 0 已通过: 0 难度: 9 上传者: 标签>字符串2017线段树各省省选山东O2优化虚树字典树,Trie 树

[SDOI2017] 天才黑客

Description

The intranet has nn nodes (numbered from 11 to nn) and mm directed cables. The central control system is at node 11. Each cable connects an ordered pair of nodes, and starting from node 11 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 len\mathrm{len}) 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 len\mathrm{len} 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 kk nodes (numbered from 11 to kk), rooted at node 11. Each edge carries a character. A string SS is in the dictionary if and only if there exists a node uu such that the characters along the path from the root to uu concatenated in order form SS.

Now Xiao Q simultaneously starts (n1)(n-1) applications at node 11. 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 ii (i=2,3,,ni=2,3,\dots ,n), the minimal time for the program destined for node ii to finish its task.

Input Format

The first line contains a positive integer TT, the number of test cases.

For each test case, the first line contains three integers n,m,kn, m, k, the number of intranet nodes, the number of cables, and the number of dictionary tree nodes, respectively.

The next mm lines each contain four integers ai,bi,ci,dia_i, b_i, c_i, d_i, meaning that there is a directed cable from node aia_i to node bib_i whose traversal time is cic_i, and whose passcode equals the string formed by the characters along the path from the dictionary root to node did_i (which can be empty). Note that the intranet may have self‑loops and multiple edges.

The next (k1)(k-1) lines each contain three integers ui,vi,wiu_i, v_i, w_i, indicating there is an edge uiviu_i \rightarrow v_i in the dictionary tree whose label is a character wiw_i. It is guaranteed that the given edges form a rooted tree with root 11, and the labels on the outgoing edges of any node are pairwise distinct.

Output Format

For each test case, output (n1)(n-1) lines. The ii‑th line is the minimal time for the program to reach node (i+1)(i+1).

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 LCP(S,T)\mathrm{LCP}(S,T) be the length of the longest common prefix of strings SS and TT. For example, $\mathrm{LCP}(\texttt{\textcolor{red}{starry}killer},\texttt{\textcolor{red}{starry}dust})=6$. Let ϵ\epsilon be the empty string.

One feasible path from 11 to 33 is 1231 \rightarrow 2 \rightarrow 3. 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 nn \le mm \le kk \le Remarks
151 \sim 5 50005\,000 2000020\,000 -
6146 \sim 14 5000050\,000 nk200000nk \le 200\,000
152015 \sim 20 -

For 100%100\% of the testdata, it is guaranteed that:

  • T10T \leq 10.
  • 2n500002 \leq n \leq 50000, 1m500001 \leq m \leq 50000, 1k200001 \leq k \leq 20000.
  • The number of test cases with n>5000n > 5000 or m>5000m > 5000 does not exceed 22.
  • 1ai,bin1 \leq a_i, b_i \leq n, 0ci200000 \leq c_i \leq 20000, 1dik1 \leq d_i \leq k.
  • 1ui,vik1 \leq u_i, v_i \leq k, 1wi200001 \leq w_i \leq 20000.

Translated by ChatGPT 5