P17832 [CSP-S 2026] Subway / subway (Official Data)
Description
City B has $m$ subway stations, numbered $1$ to $m$. Each station has a concourse, and there is a passage between the entrance and the concourse, taking the same time in both directions. Walking from the entrance of station $v$ $(1 \le v \le m)$ to its concourse takes time $w_v$.
City B has $n$ subway lines, numbered $1$ to $n$. Line $i$ $(1 \le i \le n)$ passes through $k_i$ stations, numbered $a_{i,1},a_{i,2},\ldots,a_{i,k_i}$ in order. At station $a_{i,j}$ $(1 \le j \le k_i)$, walking from the concourse to the platform of line $i$ takes time $t_{i,j}$, the same in both directions. A line never passes through the same station twice, that is, $a_{i,1},a_{i,2},\ldots,a_{i,k_i}$ are pairwise distinct.
Because space is limited, each station is passed through by at most $d$ lines. In addition, the subway network of city B is connected, that is, any two stations can reach each other via some lines.
Little R has $q$ travel plans. For the $r$-th plan $(1 \le r \le q)$, Little R starts from the entrance of station $x_r$ and takes the subway to the entrance of station $y_r$, possibly making several in-station transfers along the way. Each in-station transfer requires walking from the platform of one line to the concourse, and then from the concourse to the platform of another line. The total walking time of a plan is the sum of the walking times of entering and leaving stations and of all transfers. Little R wants the total walking time of each plan to be as small as possible.
You need to help Little R find the minimum total walking time for each travel plan.
Input Format
Read the input from the file `subway.in`.
The first line of the input contains five non-negative integers $c,n,m,d,q$, denoting the test point number, the number of subway lines, the number of stations, the maximum number of lines passing through a station, and the number of travel plans, respectively. In particular, if $c=0$, the test point is the samples.
The second line contains $m$ positive integers $w_1,w_2,\ldots,w_m$, the times needed to walk from the entrance to the concourse of each station, in order.
The $(i+2)$-th line $(1 \le i \le n)$ contains $1+2k_i$ positive integers. The first one is $k_i$, the number of stations line $i$ passes through; the next $2k_i$ positive integers are $a_{i,1},t_{i,1},a_{i,2},t_{i,2},\ldots,a_{i,k_i},t_{i,k_i}$, where $a_{i,j}$ is the $j$-th station that line $i$ passes through, and $t_{i,j}$ is the time needed to walk from the concourse of that station to the platform of line $i$.
The $(r+n+2)$-th line $(1 \le r \le q)$ contains two positive integers $x_r,y_r$, the numbers of the starting and ending stations of the $r$-th travel plan.
Output Format
Write the output to the file `subway.out`.
Output $q$ lines, where the $r$-th line $(1 \le r \le q)$ contains one non-negative integer: the minimum total walking time of the $r$-th travel plan.
Explanation/Hint
### Sample 1 Explanation
City B has $5$ stations, and the times to walk from the entrances to the concourses are $4,1,9,2,5$, respectively.
City B has $2$ subway lines. Line $1$ passes through stations $1,2,4$ in order, and line $2$ passes through stations $2,3,5$ in order. The two lines intersect at station $2$.
For the first travel plan, Little R travels from station $1$ to station $3$: he can first walk from the entrance of station $1$ to the concourse, then to the platform of line $1$, take line $1$ to station $2$ and walk back to the concourse, then walk to the platform of line $2$, take line $2$ to station $3$ and walk back to the concourse, and finally walk to the entrance of station $3$. The total walking time is
$$
w_1+t_{1,1}+(t_{1,2}+t_{2,1})+t_{2,2}+w_3=4+5+(7+3)+4+9=32.
$$
For the second travel plan, Little R travels from station $1$ to station $2$: he can first walk from the entrance of station $1$ to the concourse, then to the platform of line $1$, take line $1$ to station $2$ and walk back to the concourse, and finally walk to the entrance of station $2$. The total walking time is
$$
w_1+t_{1,1}+t_{1,2}+w_2=4+5+7+1=17.
$$
### Sample 2 Explanation
City B has $7$ stations and $3$ subway lines. Line $1$ passes through stations $1,2,5$ in order, line $2$ passes through stations $2,3,7$ in order, and line $3$ passes through stations $3,5,4,6$ in order.
For the first travel plan, Little R travels from station $1$ to station $6$: he can first walk from the entrance of station $1$ to the concourse, then to the platform of line $1$, take line $1$ to station $2$ and walk back to the concourse, then walk to the platform of line $2$, take line $2$ to station $3$ and walk back to the concourse, then walk to the platform of line $3$, take line $3$ to station $6$ and walk back to the concourse, and finally walk to the entrance of station $6$. The total walking time is
$$
w_1+t_{1,1}+(t_{1,2}+t_{2,1})+(t_{2,2}+t_{3,1})+t_{3,4}+w_6=35.
$$
For the second travel plan, Little R travels from station $2$ to station $3$: he can first walk from the entrance of station $2$ to the concourse, then to the platform of line $2$, take line $2$ to station $3$ and walk back to the concourse, and finally walk to the entrance of station $3$. The total walking time is
$$
w_2+t_{2,1}+t_{2,2}+w_3=16.
$$
### Sample 3
See `subway/3.in` and `subway/3.ans` in the problem directory.
This sample satisfies the constraints of test points $4 \sim 6$.
### Sample 4
See `subway/4.in` and `subway/4.ans` in the problem directory.
This sample satisfies the constraints of test points $9 \sim 11$.
### Sample 5
See `subway/5.in` and `subway/5.ans` in the problem directory.
This sample satisfies the constraints of test points $12 \sim 15$.
### Sample 6
See `subway/6.in` and `subway/6.ans` in the problem directory.
This sample satisfies the constraints of test points $16 \sim 19$.
### Sample 7
See `subway/7.in` and `subway/7.ans` in the problem directory.
This sample satisfies the constraints of test points $22,23$.
### Constraints
For all testdata:
- $0 \le c \le 25$, $1 \le n \le 500$, $1 \le m \le \sum_{i=1}^{n} k_i \le 5 \times 10^5$, $1 \le d \le 20$, $1 \le q \le 10^5$;
- For all $v$ $(1 \le v \le m)$, $1 \le w_v \le 10^9$;
- For all $i$ $(1 \le i \le n)$, $1 \le k_i \le m$;
- For all $i,j$ $(1 \le i \le n$, $1 \le j \le k_i)$, $1 \le a_{i,j} \le m$, $1 \le t_{i,j} \le 10^9$;
- For all $i$ $(1 \le i \le n)$, $a_{i,1},a_{i,2},\ldots,a_{i,k_i}$ are pairwise distinct;
- For all $v$ $(1 \le v \le m)$, the number of lines passing through station $v$ does not exceed $d$;
- Any two stations can reach each other via some lines;
- For all $r$ $(1 \le r \le q)$, $1 \le x_r,y_r \le m$ and $x_r \ne y_r$.
::cute-table{tuack}
| Test Point | $n \le$ | $\sum_{i=1}^{n} k_i \le$ | $d \le$ | $q \le$ | Special Properties |
| :---: | :---: | :---: | :---: | :---: | :---: |
| $1 \sim 3$ | $3$ | $10$ | $20$ | $10$ | None |
| $4 \sim 6$ | $50$ | $300$ | ^ | $200$ | None |
| $7,8$ | $200$ | $400$ | ^ | ^ | A |
| $9 \sim 11$ | $500$ | $1000$ | ^| $10^5$ | ^ |
| $12 \sim 15$ | ^ | $2 \times 10^4$ | ^ | $2000$ | B |
| $16 \sim 19$ | ^ | $5 \times 10^5$ | ^ | $10^5$ | ^ |
| $20,21$ | $2$ | ^ | ^| ^ | None |
| $22,23$ | $500$ | ^ | $2$ | ^ | ^ |
| $24,25$ | ^ | ^ | $20$ | ^ | ^ |
Special property A: Each line passes through at most $2$ stations, that is, for all $i$ $(1 \le i \le n)$, $k_i \le 2$.
Special property B: For all stations passed through by all lines, the walking time from the concourse to the corresponding line's platform is the same, that is, there exists a positive integer $T$ such that for all $i,j$ $(1 \le i \le n$, $1 \le j \le k_i)$, $t_{i,j}=T$.
Translated by QwenWork