This page is still under construction.

Parts of this page are still being built. What you see may change.

Checksum

Memory limit1024 MB

Summary
Given a partially corrupted N x N boolean matrix, its row and column XOR checksums, and recovery costs, find the minimum cost to reconstruct the matrix.
Level

Medium5 of 10

Topics
Graph, Union-find, Greedy, Implementation
Solved
No attempts yet

Problem

Grace and Edsger are constructing an N×NN \times N boolean matrix AA. The element in the ii-th row and jj-th column is written Ai,jA_{i,j}. They decide to note down the checksum (defined as the bitwise XOR of the given list of elements) along each row and each column. The checksum of the ii-th row is written RiR_i, and the checksum of the jj-th column is written CjC_j.

For example, if N=2N = 2 and A=[1011]A = \begin{bmatrix} 1 & 0 \\ 1 & 1 \end{bmatrix}, then R=[10]R = \begin{bmatrix} 1 & 0 \end{bmatrix} and C=[01]C = \begin{bmatrix} 0 & 1 \end{bmatrix}.

Once they finish the matrix, Edsger stores it on his computer. Due to a virus, some elements of matrix AA are replaced with −1-1 on Edsger's computer. Luckily, Edsger still remembers the checksum values. He wants to restore the matrix and asks Grace for help. After some investigation, it turns out that recovering the original value of Ai,jA_{i,j} from the disk takes Grace Bi,jB_{i,j} hours. Given the final matrix AA, the cost matrix BB, and the checksums along each row (RR) and column (CC), can you help Grace decide the minimum total number of hours needed to restore the original matrix AA?

Input

The first line of the input gives the number of test cases, TT. TT test cases follow.

The first line of each test case contains a single integer NN.

The next NN lines each contain NN integers representing the matrix AA. The jj-th element on the ii-th line is Ai,jA_{i,j}.

The next NN lines each contain NN integers representing the matrix BB. The jj-th element on the ii-th line is Bi,jB_{i,j}.

The next line contains NN integers representing the checksums of the rows. The ii-th element is RiR_i.

The next line contains NN integers representing the checksums of the columns. The jj-th element is CjC_j.

Output

For each test case, output one line containing Case #x: y, where xx is the test case number (starting from 1) and yy is the minimum number of hours to restore matrix AA.

Constraints

  • 1≤T≤1001 \le T \le 100.
  • −1≤Ai,j≤1-1 \le A_{i,j} \le 1, for all ii, jj.
  • 1≤Bi,j≤10001 \le B_{i,j} \le 1000 for ii, jj where Ai,j=−1A_{i,j} = -1, and Bi,j=0B_{i,j} = 0 otherwise.
  • 0≤Ri≤10 \le R_i \le 1, for all ii.
  • 0≤Cj≤10 \le C_j \le 1, for all jj.
  • There is at least one way to replace the −1-1 entries in AA with 00 or 11 so that RR and CC are satisfied.

Hint

In Sample Case #1, A1,2A_{1,2} can be restored using the checksum of either the 1st row or the 2nd column. So Grace can restore the matrix without spending any time recovering data.

In Sample Case #2, Grace spends one hour to recover A1,1A_{1,1}. After that, she can use the checksums of the 1st row and the 1st column to restore A1,2A_{1,2} and A2,1A_{2,1} respectively. Then she can use the checksum of the 2nd row to restore A2,2A_{2,2}. So Grace can restore the matrix by spending one hour.

In Sample Case #3, Grace can spend one hour to recover A1,1A_{1,1} and another hour to recover A2,2A_{2,2}. After that, she can use the checksums to restore the rest of the matrix. So Grace can restore the matrix by spending two hours in total.

Examples1

  1. Example 1

    Input
    3
    3
    1 -1 0
    0 1 0
    1 1 1
    0 1 0
    0 0 0
    0 0 0
    1 1 1
    0 0 1
    2
    -1 -1
    -1 -1
    1 10
    100 1000
    1 0
    0 1
    3
    -1 -1 -1
    -1 -1 -1
    0 0 0
    1 1 3
    5 1 4
    0 0 0
    0 0 0
    0 0 0
    
    Expected output
    Case #1: 0
    Case #2: 1
    Case #3: 2