#P11168. 「CMOI R1」First Town of This Journey/Grid Covering
「CMOI R1」First Town of This Journey/Grid Covering
Background
$\small\color{white}/10^{\text{th}}\text{Problem by AtC}.$
In this problem, the line connecting two points is considered to be the line segment with these two points as endpoints.
Problem Description
has a grid with rows and columns. You need to choose as few lattice points as possible so that, for any two distinct lattice points, the line segment connecting them passes through at least one chosen point (here we consider that a segment passes through its two endpoints).
thinks this problem is too easy, so he additionally gives , meaning that the lattice point at row , column must be chosen or must not be chosen:
- When , this lattice point cannot be chosen.
- When , this lattice point must be chosen.
You only need to output the minimum number of lattice points to choose. and will give the grid and your answer to so that he can construct an actual selection.
Input Format
The first line contains two integers .
The second line contains three integers .
Output Format
Output one integer in one line, the minimum number of lattice points that need to be chosen.
3 3
2 2 1
5
2 5
1 3 0
7
4 5
3 2 0
16
Hint
All testdata satisfy , , , .
- $\text{Subtask 1}: 1 \le n, m \le 10, \text{5 points.}$
The grid has rows and columns, and the lattice point at row , column must be chosen.
An optimal solution is as follows (black points are chosen lattice points):

Note that the following solutions are not valid:
- The figure shows two ways where the segment between two distinct lattice points does not pass through any black point.
- The lattice point at row , column is not chosen, but the input requires this point to be chosen.

Also, we consider that having an endpoint on a black point counts as passing through a black point. That is, the following segment passes through a black lattice point.

Translated by ChatGPT 5