POJ2396 Budget [maximum stream with lower bound], poj21_budget

Source: Internet
Author: User

POJ2396 Budget [maximum stream with lower bound], poj21_budget

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 ;}





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.