#P3325. [SDOI2015] 模拟电路

[SDOI2015] 模拟电路

Description

A well-known chip company asks you to help install components that stabilize voltage on some of their latest products. Each chip is designed as an N×NN \times N grid of slots; a slot can hold a single component. Your task is to insert as many new components as possible.

Modern processors are complex. To keep the voltage stable, you must obey several restrictions: some slots are unusable; some slots are already occupied by other components and thus cannot be used for new ones; memory buses attach to the horizontal and vertical boundaries of the chip, and their load voltages must be safe.

Specifically, the company provides NN sets of constraints, one per row. For row ii (and its constraint set ii), you are given a nonnegative integer TiT_i and TiT_i column indices ri,jr_{i,j} (1jTi1 \le j \le T_i). The requirement is: the number of components in row ii must not exceed the sum of the numbers of components in the specified TiT_i columns (i.e., at most “the number of components in column ri,1r_{i,1} + the number of components in column ri,2r_{i,2} + \cdots”).

To avoid slot overheating, you are given a real number sis_i (1iN1 \le i \le N) with 0si10 \le s_i \le 1, and you must ensure that the number of components in row ii does not exceed sis_i times the total number of components. Similarly, you are given a real number tit_i (1iN1 \le i \le N) with 0ti10 \le t_i \le 1, and you must ensure that the number of components in column ii does not exceed tit_i times the total number of components.

Note that slots already occupied by components are counted when computing the number of components in any row or column. They are also counted in the total number of components on the chip. The chip is described by an NN-row, NN-column character matrix, where . denotes an open slot, / denotes an unusable slot, and C denotes a slot already occupied by a component.

Input Format

  • The first line contains a positive integer NN (1N401 \le N \le 40), the size of the chip.
  • Then follow NN lines, each containing NN characters describing the slots, each character being one of ., /, or C, as defined above.
  • Then follow NN lines describing the constraint sets. On the ii-th of these lines, first a nonnegative integer TiT_i is given, followed by TiT_i distinct integers (each between 11 and NN) specifying the related columns.
  • The next line contains NN real numbers, in order, corresponding to sis_i. Each number has at most three digits after the decimal point.
  • The next line contains NN real numbers, in order, corresponding to tit_i. Each number has at most three digits after the decimal point.

Output Format

If there exists a valid placement strategy, output the maximum number of additional components that can be installed on the chip. Otherwise, output impossible.

5
CC/..
././/
..C.C
/.C..
/./C/
1 1
1 2
1 3
1 4
1 5
0.3 0.3 0.3 0.3 0.3
0.3 0.3 0.3 0.3 0.3
7

Hint

Translated by ChatGPT 5