Binary Matrix Generation Algorithm

For two input arrays [R1, ..., Rn] and [C1, ..., Cn]. We want to create a binary matrix A (size-nxn) such that the sum of the elements in column i from A is Ci and the sum of the elements in row j of group A is equal to Rj.

I tried to fill using the greedy algorithm: fill 1 from left to right and decrease Ci and do it for each line. But that did not work. (Also, I tried sorting rows and columns in descending order, but still didn't work)

+4
source share
1 answer

This can be solved using Maximum Flows. A similar (more complicated version) problem is available on LightOJ , and my code is for link

Here is the solution.

. no_rows, - no_cols.

no_rows + no_cols. no_rows ( "partite" ). l1, l2, ..., lno_row.

no_cols ( "partite" ). r1, r2, ... , rno_cols.

li rj 1 <= i <= no_rows 1 <= j <= no_cols, , 1.

(S) (T). , , .

, , .

. , li rj, , (i, j) 1, 0.

. , , (S, l) (r, T) .


: Dinic ++ ideone


2: , li, Ri ( R - , ). , Ri T, Ci ( C - , , )

+6
source

Source: https://habr.com/ru/post/1652415/


All Articles