| 시간 제한 | 메모리 제한 | 제출 | 정답 | 맞힌 사람 | 정답 비율 |
|---|---|---|---|---|---|
| 3 초 | 1024 MB | 65 | 8 | 8 | 30.769% |
In JOI city, there is a grid-shaped road network consisting of $H$ infinitely long east-west roads and $W$ infinitely long north-south roads. Intersection $(i, j)$ ($1 ≤ i ≤ H$, $1 ≤ j ≤ W$) is the intersection where the $i$-th northernmost east-west road and the $j$-th westernmost north-south road cross.
Currently, part of the roads is closed due to poor road conditions. Specifically, the status of the roads is as follows:
President K, the mayor of JOI city, decided to make a repair plan of the road network. A repair plan consists of zero or more repairs. A repair is done by choosing an integer $i$ satisfying $1 ≤ i ≤ H$ and doing the following:
For every integer $j$ satisfying $1 ≤ j ≤ W - 1$, make the segment in the $i$-th northernmost east-west road connecting intersection $(i, j)$ and intersection $(i, j + 1)$ passable (if it is closed).
The repair takes $C_i$ days. Note that $C_i$ is either $1$ or $2$.
Since no two repairs in a repair plan can be done in parallel, the period of a repair plan is equal to the sum of the time taken by repairs consisting the repair plan.
President K thinks that securing the route between city facilities is important and asks you $Q$ questions. The $k$-th questions ($1 ≤ k ≤ Q$) is as follows:
Is there a repair plan that makes $T_k$ intersections $(X_{k,1}, Y_{k,1}), (X_{k,2}, Y_{k,2}), \dots , (X_{k,T_k}, Y_{k,T_k})$ mutually reachable? If so, what is the minimum possible period of such a repair plan?
Write a program which, given the status of the road network, the days taken by repairing each east-west road and the details of the questions by President K, answers all the questions.
Read the following data from the standard input.
$H$ $W$ $Q$
$A_{1,1}A_{1,2} \cdots A_{1,W-1}$
$A_{2,1}A_{2,2} \cdots A_{2,W-1}$
$\vdots$
$A_{H,1}A_{H,2} \cdots A_{H,W-1}$
$B_{1,1}B_{1,2} \cdots B_{1,W}$
$B_{2,1}B_{2,2} \cdots B_{2,W}$
$\vdots$
$B_{H-1,1}B_{H-1,2} \cdots B_{H-1,W}$
$C_1$ $C_2$ $\cdots$ $C_H$
$Query_1$
$Query_2$
$\vdots$
$Query_Q$
Here, $Query_k$ ($1 ≤ k ≤ Q$) is as follows:
$T_k$
$X_{k,1}$ $Y_{k,1}$
$X_{k,2}$ $Y_{k,2}$
$\vdots$
$X_{k,T_k}$ $Y_{k,T_k}$
Write $Q$ lines to the standard output. In the $k$-th line ($1 ≤ k ≤ Q$), output the minimum possible period, in days, of a repair plan that makes $T_k$ intersections $(X_{k,1}, Y_{k,1}), (X_{k,2}, Y_{k,2}), \dots , (X_{k,T_k}, Y_{k,T_k})$ mutually reachable if such a repair plan exists. Otherwise, output -1.
| 번호 | 배점 | 제한 |
|---|---|---|
| 1 | 10 | $C_i = 1$ ($1 ≤ i ≤ H$), $Q ≤ 5$, $T_k = 2$ ($1 ≤ k ≤ Q$), $A_{i, j} = 0$ ($1 ≤ i ≤ H$, $1 ≤ j ≤ W - 1$). |
| 2 | 6 | $C_i = 1$ ($1 ≤ i ≤ H$), $Q ≤ 5$, $T_k = 2$ ($1 ≤ k ≤ Q$). |
| 3 | 15 | $C_i = 1$ ($1 ≤ i ≤ H$), $Q ≤ 5$. |
| 4 | 11 | $C_i = 1$ ($1 ≤ i ≤ H$), $T_k = 2$ ($1 ≤ k ≤ Q$). |
| 5 | 6 | $C_i = 1$ ($1 ≤ i ≤ H$). |
| 6 | 12 | $Q ≤ 5$. |
| 7 | 26 | $T_k = 2$ ($1 ≤ k ≤ Q$). |
| 8 | 14 | No additional constraints. |
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
The figure below shows the current status of the road network. The gray part is closed, and the blue part is passable.
-1 in the fourth line.This sample input satisfies the constraints of Subtasks 1, 2, 3, 4, 5, 6, 7, 8.
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
This sample input satisfies the constraints of Subtasks 2, 3, 4, 5, 6, 7, 8.
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
This sample input satisfies the constraints of Subtasks 3, 5, 6, 8.
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
This sample input satisfies the constraints of Subtasks 6, 7, 8.
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
This sample input satisfies the constraints of Subtasks 6, 8.