#P7500. 「HMOI R1」地铁客流
「HMOI R1」地铁客流
Description
The metro system of Tiankong City consists of lines and has a total of stations. Every station has lines stopping at it. On each line, two adjacent stations can be viewed as connected by an undirected edge. Line has stations, and these stations are all distinct.
These lines intersect at some stations, meaning that one station may have many lines stopping at it.
Now, there are passengers. Passenger wants to travel from station to station , and it is guaranteed that these two stations are different. If the two stations are not on the same line, the passenger will transfer several times. As a technical staff member of the Tiankong Metro, you need to compute the passenger flow contributed by these passengers.
Note that the method used to compute passenger flow here is different from real-world applications.
Passenger flow entry flow transfer flow. The entry flow is the number of passengers; the transfer flow is the number of transfers made by passengers. There may be multiple paths between the start and the destination; in that case, the metro passenger flow is independent of which path the passenger chooses. When calculating, we only consider paths with the minimum number of transfers.
Notes:
- Suppose the minimum number of transfers from to is . Then the passenger needs to take lines, contributing passenger flow.
- When the passenger cannot reach the destination from the start (i.e., there is no path between and ), they will take a bus instead, and the passenger flow is counted as .
Input Format
The first line contains three integers .
The next lines each describe one metro line:
The first integer in each line is , the number of stations on this line;
Then follow integers , the station indices that this line passes through in order.
The next lines each contain two integers , describing the start and end stations of one passenger’s trip.
Output Format
Output one integer in a single line, representing the passenger flow contributed by these passengers.
8 4 4
4 1 2 3 5
2 3 6
2 2 4
3 7 4 8
1 6
4 3
4 8
6 7
9
8 4 4
4 1 2 3 5
3 6 3 8
2 2 4
3 7 4 8
1 6
4 3
4 8
6 7
7
8 3 4
4 1 2 3 5
2 3 6
3 7 4 8
1 6
4 3
4 8
6 7
3
Hint
Sample explanation:
- By default, passengers will choose a path with the minimum number of transfers.
- The number on an edge indicates the index of the metro line it belongs to.
Sample 1 is shown in the figure:

Passenger will take Line from to , then take Line from to ;
Passenger will take Line from to , then take Line from to ;
Passenger will take Line from to ;
Passenger will take Line from to , take Line from to , then take Line from to , and finally take Line from to .
Thus, the four passengers contribute passenger flow of respectively, and the answer is .
Sample 2 is shown in the figure:

Compared with Sample 1,
Passenger can choose another route: take Line from to , then take Line from to , which also involves one transfer;
Passenger can choose another route with fewer transfers: take Line from to , then take Line from to , with only one transfer.
The total passenger flow is .
Sample 3 is shown in the figure:

Compared with Sample 1, the travel paths of passengers and are not connected, so they are not counted in the passenger flow.
The total passenger flow is .
Let the number of lines stopping at station be .
For all testdata:
- ;
- ;
- ;
- .
This problem uses bundled tests.
| No. | Constraints | Score |
|---|---|---|
| No further constraints |
Due to the large input size, do not use cin with stream synchronization enabled.
You can disable synchronization for cin by using std::ios::sync_with_stdio(false).
You may also use the following fast input template, which supports reading non-negative integers within the int range.
int readInt() {
int ret = 0; char o;
while (!isdigit(o = getchar()));
do ret = ret * 10 + (o ^ 48);
while (isdigit(o = getchar()));
return ret;
}
- Idea: Nanqiao Bus Station (Nánqiáo qìchēzhàn).
- Solution: Nanqiao Bus Station (Nánqiáo qìchēzhàn).
- Code: Nanqiao Bus Station (Nánqiáo qìchēzhàn).
- Data: Nanqiao Bus Station (Nánqiáo qìchēzhàn).
Translated by ChatGPT 5
京公网安备 11011102002149号