#P3314. [SDOI2014] 电路板

[SDOI2014] 电路板

Description

Given a circuit diagram and a circuit board of specified size, Alice\text{Alice} and Bob\text{Bob} need to realize the circuit on the board.

You can regard the circuit board as an n×mn \times m 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 kk 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 44 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, Alice\text{Alice} wants to find a feasible scheme that minimizes the total length of all wires. Bob\text{Bob} wants to know how many schemes achieve this minimal total length.

Input Format

The first line contains three integers n,m,kn, m, k, denoting the size of the circuit board and the number of pairs of cells that must be connected by wires.

Then follow nn lines, each containing mm integers, each being 00 or 11. A 00 means the cell is usable; a 11 means it is an obstacle and cannot be used.

Then follow kk lines, each containing 44 integers x1,y1,x2,y2x1,y1,x2,y2, giving one pair of cells to be connected by a wire. The row and column indices of cells are 00-based, so 0x1,x2<n0 \le x1,x2 < n and 0y1,y2<m0 \le y1,y2 < m.

This problem has multiple testcases (at most 3030). 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 2561984925619849.

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 44 paths between (1,2)(1,2) and (2,1)(2,1), and it is easy to see that the minimal total path length is 1616. In any feasible scheme, any path between (1,2)(1,2) and (2,1)(2,1) can correspond to any of the required 44 paths. If we consider distinct shapes, there are 44 different schemes. Taking into account permutations, 4!=244! = 24, the total number of schemes is 9696.
  • For the second testcase: due to 33 obstacle cells, there is only one feasible path.

Constraints:

  • For 20%20\% of the testdata: n,m4n, m \le 4.
  • For 40%40\% of the testdata: n,m8n, m \le 8.
  • For 100%100\% of the testdata: n,m9n, m \le 9, k10k \le 10.

In addition:

  • There exists 10%10\% of the testdata with k=1k=1.
  • There exists 30%30\% of the testdata with k3k \le 3.

Translated by ChatGPT 5