#P3314. [SDOI2014] 电路板
[SDOI2014] 电路板
Description
Given a circuit diagram and a circuit board of specified size, and need to realize the circuit on the board.
You can regard the circuit board as an grid.
The user’s circuit consists of several components, each of which may occupy one or more cells. We classify the cells occupied by components into two types:
- One type merely occupies the cell; afterward the cell will no longer be used, and no wire will run from that occupied position. Such cells are considered obstacles on the circuit board.
- The other type are interfaces of components. Although occupied by a component, wires may still be connected from these cells to other components to form the circuit.
For some wires in the circuit that connect two components, we specify them as pairs of cells on the board. Each pair must be connected by exactly one wire.
The same cell may belong to multiple pairs (for example, in some parallel circuits).
Any two wires must not intersect (but they may connect to the same cell that contains a component), and each edge of every cell on the board can be used by at most one wire. Therefore, each component interface can have at most wires attached. However, a cell not occupied by a component may be traversed by multiple wires.
Specifically, to ensure that wires do not intersect: one wire can enter a cell from the top edge and leave from the left edge, while another wire can enter from the bottom edge and leave from the right edge. Note that wires themselves are undirected; that is, the relation described by a pair of cells is an undirected edge. So the same configuration can also be described as entering from the left edge and leaving from the top edge, and entering from the right edge and leaving from the bottom edge. There are several similar valid pairings.
Now, wants to find a feasible scheme that minimizes the total length of all wires. wants to know how many schemes achieve this minimal total length.
Input Format
The first line contains three integers , denoting the size of the circuit board and the number of pairs of cells that must be connected by wires.
Then follow lines, each containing integers, each being or . A means the cell is usable; a means it is an obstacle and cannot be used.
Then follow lines, each containing integers , giving one pair of cells to be connected by a wire. The row and column indices of cells are -based, so and .
This problem has multiple testcases (at most ). The input ends with a line 0 0 0.
Output Format
For each testcase, output two integers: the minimal total wire length, and the number of schemes that achieve this minimal length.
Output the number of schemes modulo .
If there is no solution, output -1 0.
4 4 4
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
1 2 2 1
2 1 1 2
1 2 2 1
2 1 1 2
4 4 2
0 0 1 1
0 0 0 0
1 0 0 0
0 0 0 0
1 0 2 2
0 0 3 0
0 0 0
16 96
12 1
Hint
Sample explanation:
- For the first testcase: there are paths between and , and it is easy to see that the minimal total path length is . In any feasible scheme, any path between and can correspond to any of the required paths. If we consider distinct shapes, there are different schemes. Taking into account permutations, , the total number of schemes is .
- For the second testcase: due to obstacle cells, there is only one feasible path.
Constraints:
- For of the testdata: .
- For of the testdata: .
- For of the testdata: , .
In addition:
- There exists of the testdata with .
- There exists of the testdata with .
Translated by ChatGPT 5
京公网安备 11011102002149号