#graph503. 矩阵问题

矩阵问题

Matrix Problem

Problem Description

There is an n×mn\times m matrix; you need to fill in a number xi,jx_{i,j} at each position (i,j)(i, j).

There are several constraints. For each position (i,j)(i, j), it must satisfy li,j≤xi,j≤ri,jl_{i,j}\leq x_{i,j}\leq r_{i,j}.

Further, for all i,j(1≤i≤n,1≤j≤m−1)i, j(1\leq i\leq n, 1\leq j\leq m-1), it must satisfy xi,j+xi,j+1≤ai,jx_{i,j} + x_{i, j+1} \leq a_{i,j}. For all i,j(1≤i≤n−1,1≤j≤m)i, j(1\leq i\leq n-1, 1\leq j\leq m), it must satisfy xi,j+xi+1,j≥bi,jx_{i,j} + x_{i + 1, j} \geq b_{i,j}.

Input Format

The first line contains two integers n,m(2≤n,m≤50)n, m(2\leq n, m\leq 50).

The next nn lines each contain mm numbers li,j(1≤li,j≤109)l_{i,j} (1\leq l_{i, j}\leq 10^9).

The next nn lines each contain mm numbers ri,j(1≤ri,j≤109)r_{i,j} (1\leq r_{i, j}\leq 10^9).

The next nn lines each contain m−1m-1 numbers ai,j(0≤ai,j≤2×109)a_{i,j} (0\leq a_{i, j}\leq 2\times 10^9).

The next n−1n-1 lines each contain mm numbers bi,j(0≤bi,j≤2×109)b_{i,j} (0\leq b_{i, j}\leq 2\times 10^9).

The input guarantees that a solution always exists.

Output Format

Output nn lines of mm numbers each, representing one valid solution. Any valid solution is accepted.

Sample Input

2 2
0 1
2 3
2 3
4 5
4
8
4 5

Sample Output

1 2
3 4

Constraints

2≤n,m≤502\leq n, m\leq 50; all bounds are within the ranges given in the input; a solution always exists.

Hint

  • Every constraint is of the form "xu−xv≤cx_u - x_v \leq c" or "xu−xv≥cx_u - x_v \geq c", which is a classic system of difference constraints. Build a graph and run the Bellman-Ford / SPFA shortest-path algorithm to find one feasible assignment.