Poj2396 budget (maximum stream with lower bound]

Source: Internet
Author: User

Budget
Time limit:3000 Ms   Memory limit:65536 K
Total submissions:5962   Accepted:2266   Special Judge

Description

We are supposed to make a budget proposal for this multi-site competition. the budget proposal is a matrix where the rows represent different kinds of expenses and the columns represent different sites. we had a meeting about this, some time ago where we discussed the sums over different kinds of expenses and sums over different sites. there was also some talk about special constraints: someone mentioned that computer center wowould need at least 2000 K rials for food and someone from Sharif authorities argued they wouldn't use more than 30000 K rials for T-shirts. anyway, we are sure there was more; we will go and try to find some notes from that meeting.

And, by the way, no one really reads budget proposals anyway, so we'll just have to make sure that it sums up properly and meets all constraints.

Input

The first line of the input contains an integer N, giving the number of test cases. the next line is empty, then, test cases follow: the first line of each test case contains two integers, M and N, giving the number of rows and columns (M <= 200, n <= 20 ). the second line contains M integers, giving the row sums of the matrix. the third line contains N integers, giving the column sums of the matrix. the fourth line contains an integer c (C <1000) giving the number of constraints. the next C lines contain the constraints. there is an empty line after each test case.

Each constraint consists of two integers R and Q, specifying some entry (or entries) in the matrix (the upper left corner is 1 1 and 0 is interpreted as "all", I. e. 4 0 means all entries on the fourth row and 0 0 means the entire matrix), one element from the set {<, =, >}and one integer V, with the obvious interpretation. for instance, the constraint 1 2> 5 means that the cell in the 1st row and 2nd column must have an entry strictly greater than 5, and the constraint 4 0 = 3 means that all elements in the fourth row shoshould be equal to 3.

Output

For each case output a matrix of non-negative integers meeting the above constraints or the string "impossible" if no legal solution exists. Put One empty lineBetween matrices.

Sample Input

22 3 8 10 5 6 7 4 0 2 > 2 2 1 = 3 2 3 > 2 2 3 < 5 2 2 4 5 6 7 1 1 1 > 10

Sample output

2 3 3 3 3 4 IMPOSSIBLE 

Source

Tehran 2003 preliminary is really crazy. It took three days out of service before, after, and after a while. The main time was stuck in a hand error and a letter was wrong... there is an N * m square matrix. The numbers in the square matrix are unknown, but we know the following constraints:
1> sum of numbers in each row
2> sum of numbers in each column
3> the number and size of some cells are limited. For example, it is required that the number in Column 2nd of row 3rd must be greater than 5 (or less than 3, or equal to 10)
Whether the solution is to fill the square matrix with positive numbers when all constraints are met. If yes, output the filled square matrix; otherwise, output impossible.
Question: This question can be converted into the maximum flow problem with the upper and lower bounds of capacity, and the rows of the square matrix can be transformed from 1 ...... N number, column N + 1 ...... N + M number, add Source Vertex S = 0 and sink vertex t = n + m + 1.
1> connect the Source Vertex to each row node. The capacity and lower bound of the edge formed by the connection are set to the sum of all numbers in the row.
2> connect each column node to a vertex. The capacity and lower bound of the edge formed by the connection are set to the sum of all numbers in the column.
3> from each row node to each column node, the capacity is infinite.
4> If the number of the V column in the U row must be greater than W, the lower bound of the edge <u, v + n> traffic is W + 1
5> If the number of the V column in the U row must be less than W, the size of the edge <U, V + n> is changed to the W-1
6> If the number of the V column in the U row must be W, the lower bound and capacity of the edge <u, v + n> traffic are both W
The feasible stream (also the largest stream) is the solution to the problem.
Trick:
1) W may be a negative number, resulting in a negative traffic lower bound. Should be processed to 0
2) data may be in conflict. For example, we mentioned () = 1, and then () = 10.

# Include <stdio. h> # include <string. h> # define INF 0x3fffffff # define maxn 250int m, n, sink, ssource, ssink; // m rows, N columnsint G [maxn] [maxn], g0 [maxn] [maxn], flow [maxn] [maxn]; int low [maxn] [maxn], high [maxn] [maxn]; int in [maxn], out [maxn], layer [maxn], que [maxn]; bool vis [maxn]; int min (int A, int B) {return A> B? B: A;} int max (int A, int B) {return a <B? B: A;} bool countlayer () {memset (layer, 0, sizeof (layer); int I, now, id = 0, front = 0; layer [ssource] = 1; que [ID ++] = ssource; while (front <ID) {now = que [Front ++]; for (I = 0; I <= ssink; ++ I) if (G [now] [I]> 0 &&! Layer [I]) {layer [I] = layer [now] + 1; if (I = ssink) return true; else que [ID ++] = I ;}} return false;} int dinic () {int maxflow = 0, mincut, POs, I, now, U, V, id = 0; while (countlayer ()) {memset (VIS, 0, sizeof (VIS); vis [ssource] = 1; que [ID ++] = ssource; while (ID) {now = que [ID-1]; If (now = ssink) {mincut = inf; for (I = 1; I <ID; ++ I) {u = que [I-1]; V = que [I]; If (mincut> G [u] [v]) {Mincut = G [u] [v]; Pos = u ;}} maxflow ++ = mincut; for (I = 1; I <ID; ++ I) {u = que [I-1]; V = que [I]; G [u] [v]-= mincut; G [v] [u] + = mincut; flow [u] [v] + = mincut; flow [v] [u]-= mincut;} while (que [ID-1]! = POS) vis [que [-- id] = 0;} else {for (I = 0; I <= ssink; ++ I) {If (G [now] [I]> 0 & layer [now] + 1 = layer [I] &! Vis [I]) {vis [I] = 1; que [ID ++] = I; break ;}} if (I> ssink) -- id ;}}} return maxflow;} void solve () {int I, j, sum = 0; for (I = 0; I <= sink; ++ I) for (j = 0; j <= sink; ++ J) {G [I] [J] = high [I] [J]-low [I] [J]; out [I] + = low [I] [J]; In [J] + = low [I] [J]; sum + = low [I] [J];} for (I = 0; I <= sink; ++ I) {G [ssource] [I] = in [I]; G [I] [ssink] = out [I];} // memcpy (G0, G, sizeof (g); G [sink] [0] = inf; If (Sum! = Dinic () {printf ("impossible \ n"); return;} G [sink] [0] = G [0] [sink] = 0; for (I = 1; I <= m; ++ I) {// printf ("% d ", g0 [I] [1 + M]-G [I] [1 + M] + low [I] [1 + M]); printf ("% d ", flow [I] [1 + M] + low [I] [1 + M]); For (j = 2; j <= N; ++ J) printf ("% d", flow [I] [J + M] + low [I] [J + M]); printf ("\ n ");}} int main () {// freopen ("poj2396.txt", "r", stdin); // freopen ("ans1.txt", "W", stdout); int T, C, x, Y, Z, I, J; char ch; scanf ("% d", & T); While (t --) {memset (G, 0, sizeof (g )); memset (low, 0, sizeof (low); memset (high, 0, sizeof (high); memset (Out, 0, sizeof (out); memset (in, 0, sizeof (in); memset (flow, 0, sizeof (flow); scanf ("% d", & M, & N ); sink = m + n + 1; ssource = sink + 1; ssink = ssource + 1; for (I = 1; I <= m; ++ I) {scanf ("% d", & Z); low [0] [I] = high [0] [I] = z;} for (I = 1; I <= N; ++ I ){ Scanf ("% d", & Z); low [M + I] [sink] = high [M + I] [sink] = z;} for (I = 1; I <= m; ++ I) {for (j = 1; j <= N; ++ J) {high [I] [J + M] = inf ;}} scanf ("% d", & C); While (c --) {scanf ("% d % C % d", & X, & Y, & Ch, & Z); If (! X & Y) {// The Y-th element of all rows if (CH = ') {for (I = 1; I <= m; ++ I) low [I] [M + Y] = high [I] [M + Y] = z;} else if (CH = '<') {for (I = 1; I <= m; ++ I) High [I] [M + Y] = min (Z-1, high [I] [M + Y]);} else {for (I = 1; I <= m; ++ I) Low [I] [M + Y] = max (Z + 1, low [I] [M + Y]) ;}} else if (X &&! Y) {If (CH = ') {for (I = 1; I <= N; ++ I) low [x] [M + I] = high [x] [M + I] = z;} else if (CH = '<') {for (I = 1; I <= N; ++ I) High [x] [M + I] = min (high [x] [M + I], Z-1 );} else {for (I = 1; I <= N; ++ I) Low [x] [M + I] = max (low [x] [M + I], Z + 1) ;}} else if (! X &&! Y) {for (I = 1; I <= m; ++ I) for (j = 1; j <= N; ++ J) {If (CH = ') low [I] [M + J] = high [I] [M + J] = z; else if (CH = '<') High [I] [M + J] = min (high [I] [M + J], Z-1 ); else low [I] [M + J] = max (low [I] [M + J], Z + 1 );}} else {If (CH = ') low [x] [M + Y] = high [x] [M + Y] = z; else if (CH = '<') High [x] [M + Y] = min (high [x] [M + y], Z-1 ); else low [x] [M + Y] = max (low [x] [M + y], Z + 1) ;}} solve (); printf ("\ n");} return 0 ;}


Poj2396 budget (maximum stream with lower bound]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.