#P10209. [JOI 2024 Final] 路网服务 2 / Road Service 2
[JOI 2024 Final] 路网服务 2 / Road Service 2
Problem Description
JOI City has a grid-like road network consisting of infinitely long east–west roads and north–south roads. The intersection of the -th east–west road counted from the north and the -th north–south road counted from the west is called intersection .
Now, due to long-term disrepair, some road segments are blocked. The blocking status is as follows:
- If , then on the -th east–west road counted from the north, the segment between intersections and is blocked. If , then it is passable.
- If , then on the -th north–south road counted from the west, the segment between intersections and is blocked. If , then it is passable.
- All other parts of the roads, namely the parts outside the intersections, are blocked.
The city mayor, Director K, decided to make a road repair plan. A repair plan consists of at least repairs. In one repair, choose an integer , and perform the following operation:
For all integers satisfying , if on the -th east–west road counted from the north the segment between intersections and is blocked, make it passable. This process takes days in total, where is or .
Multiple repairs in the repair plan cannot be carried out at the same time. Therefore, the number of days required to execute the repair plan is the sum of the days required by all repairs included in the plan.
To allow important facilities in the city to be mutually reachable, Director K asks you questions. The -th question is as follows:
Is there a repair plan that makes the intersections $(X_{k, 1}, Y_{k, 1}),(X_{k, 2}, Y_{k, 2}), \ldots ,(X_{k, T_{k}}, Y_{k, T_{k}})$ mutually reachable using only passable roads. If such a plan exists, what is the minimum number of days needed to execute such a repair plan.
Given the blocking status of the road network, the days required to repair each road, and the contents of Director K's questions, write a program to answer all of Director K's questions.
Input Format
The first line contains three integers .
The next lines each contain a string of length , representing .
The next lines each contain a string of length , representing .
The next line contains space-separated integers .
Then there are queries, each given in the following form:
The first line contains an integer .
The next lines each contain two integers .
Output Format
Output lines. In the -th line , if there exists a repair plan that makes the intersections $(X_{k, 1}, Y_{k, 1}),(X_{k, 2}, Y_{k, 2}), \ldots ,(X_{k, T_{k}}, Y_{k, T_{k}})$ mutually reachable, output the minimum number of days needed to execute such a repair plan. Otherwise, output .
4 3 4
00
00
00
00
100
001
000
1 1 1 1
2
1 1
3 3
2
3 1
1 2
2
2 3
3 3
2
4 2
3 2
1
3
0
-1
4 4 4
100
110
011
010
0010
1001
0101
1 1 1 1
2
1 2
3 1
2
1 4
4 1
2
3 2
1 2
2
4 3
1 1
1
3
2
2
7 3 3
10
00
00
10
00
01
00
110
101
011
001
110
100
1 1 1 1 1 1 1
3
7 2
3 1
3 2
3
3 1
6 3
2 3
7
2 2
1 3
7 3
5 2
1 2
7 2
3 1
3
2
4
4 3 3
00
00
10
00
110
011
001
1 2 2 2
2
1 1
3 1
2
4 3
2 1
2
4 1
1 3
1
2
5
7 3 2
01
00
00
00
00
10
01
100
110
011
001
101
001
1 1 2 1 1 2 2
3
7 2
1 3
5 1
5
1 1
2 2
3 1
2 3
4 2
4
1
Hint
For all input data, it holds that:
- .
- .
- .
- .
- is or .
- is or .
- is or .
- .
- .
- $1 \leq X_{k, l} \leq H\ (1 \leq k \leq Q, 1 \leq l \leq T_{k})$.
- $1 \leq Y_{k, l} \leq W\ (1 \leq k \leq Q, 1 \leq l \leq T_{k})$.
- $(X_{k, 1}, Y_{k, 1}),(X_{k, 2}, Y_{k, 2}), \ldots,(X_{k, T_{k}}, Y_{k, T_{k}})$ are all distinct .
The detailed additional constraints and scores for subtasks are shown in the table below.
| Subtask | Additional Constraints | Score |
|---|---|---|
| 1 | $C_{i}=1\ (1 \leq i \leq H), Q \leq 5, T_{k}=2\ (1 \leq k \leq Q), A_{i, j}=0\ (1 \leq i \leq H, 1 \leq j \leq W-1)$ | |
| 2 | $C_{i}=1\ (1 \leq i \leq H), Q \leq 5, T_{k}=2\ (1 \leq k \leq Q)$ | |
| 3 | ||
| 4 | $C_{i}=1\ (1 \leq i \leq H), T_{k}=2\ (1 \leq k \leq Q)$ | |
| 5 | ||
| 6 | ||
| 7 | ||
| 8 | No additional constraints |
Translated by ChatGPT 5