Poj 2396 budget
Link: http://poj.org/problem? Id = 2396
Question:Given a matrix of M * n, given the sum of each column in each row, and some of the value constraints, we can ask whether a feasible matrix can be formed.
Ideas:Add a Source Vertex to join the edge of each row. The upper and lower bounds of each edge are the sum of the rows. Add a vertex and each column is connected to the edge of the vertex, the upper and lower bounds of the edge are the sum of the column. Then, each row is connected to each column, and the upper and lower bounds of the edge are (0, INF) at the beginning, and then updated through some restrictions. Now the problem is to find a feasible stream with an upstream and downstream sink. You only need to add a super source point, a super sink point, and link the sink point of the source image to the source point to find a feasible stream.
Details: the composition of this question is troublesome.
/* ID: [email protected] prog: Lang: c ++ */# include <map> # include <set> # include <queue> # include <stack> # include <cmath> # include <cstdio> # include <vector> # include <string> # include <fstream> # include <cstring> # include <ctype. h> # include <iostream> # include <algorithm> using namespace STD; # define Inf (1 <30) # define linf (1ll <60) # define PI ACOs (-1.0) # define MEM (a, B) memset (a, B, sizeof (A) # define rep (I, A, n) for (in T I = A; I <n; I ++) # define per (I, A, n) for (INT I = n-1; I> = A; I --) # define EPS 1e-6 # define debug puts ("================ ") # define Pb push_back # define mkp make_pair # define all (x ). begin (), (x ). end () # define Fi first # define se second # define SZ (x) (INT) (x ). size () # define posin (x, y) (0 <= (x) & (x) <n & 0 <= (y) & (y) <m) typedef long ll; typedef unsigned long ull; const int maxn = 2 000; const int maxm = 2000000; struct node {int V; // vertex int cap; // capacity int flow; // current flow in this arc int NXT ;} E [maxm * 2]; int G [maxn], CNT; int St, Ed, n, n, m, S, T; int ID [maxm]; void add (int u, int V, int C, int K) {e [++ CNT]. V = V; E [CNT]. CAP = C; E [CNT]. flow = 0; E [CNT]. NXT = G [u]; G [u] = CNT; Id [k] = CNT; E [++ CNT]. V = u; E [CNT]. CAP = 0; E [CNT]. flow = 0; E [CNT]. NXT = G [v]; G [V] = CNT; Id [0] = 0;} int sum1, sum2; int low [maxm], up [maxm], TOT [maxn]; bool flag; bool set_edge (int I, Int J, char * STR, int c) {int x = (I-1) * n + J; if (STR [0] = ') {If (low [x] <= C & C <= up [x]) low [x] = up [x] = C; else return false;} else if (STR [0] = '<') {If (low [x] <C) up [x] = min (up [X], C-1); else return false;} else if (STR [0] = '> ') {If (up [x]> C) low [x] = max (low [X], c + 1); else return false;} return true;} int sum; void Init () {memset (G, 0, sizeof (INT) * (m + n + 10); memset (TOT, 0, sizeof (INT) * (m + n + 10); CNT = 1; st = m + n + 2, ED = m + n + 3; S = 0, T = m + n + 1; int U, V, C; sum1 = sum2 = 0; For (INT I = 1; I <= m; I ++) {scanf ("% d", & C ); TOT [s]-= C; Tot [I] + = C; sum1 + = C;} For (INT I = 1; I <= N; I ++) {scanf ("% d", & C); Tot [T] + = C; Tot [I + M] -= C; sum2 + = C;} flag = true; If (sum1! = Sum2) Flag = false; int K; scanf ("% d", & K); memset (low, 0, sizeof (INT) * (M * n + 10); fill (Up, Up + sizeof (INT) * (M * n + 10), INF); char STR [10]; while (k --) {scanf ("% d % S % d", & U, & V, STR, & C); If (FLAG) {int rst, red, CST, CED; If (u = 0) rst = 1, Red = m; else rst = Red = u; If (V = 0) CST = 1, CED = N; else CST = CED = V; For (INT I = RST; I <= red; I ++) {for (Int J = CST; j <= CED; J ++) {If (! Set_edge (I, j, STR, c) {flag = false; goto FK ;}}}fk :;}} if (! Flag) return; add (t, s, INF, 0); For (INT I = 1; I <= m; I ++) {for (Int J = 1; j <= N; j ++) {int x = (I-1) * n + J; Tot [I]-= low [x]; TOT [J + M] + = low [X]; add (I, j + m, up [x]-low [X], x) ;}} sum = 0; for (INT I = 0; I <= m + n + 1; I ++) {If (TOT [I]> 0) add (St, I, TOT [I], 0), sum + = tot [I]; If (TOT [I] <0) add (I, Ed,-tot [I], 0);} n = ed + 3;} int Dist [maxn], numbs [maxn], Q [maxn]; void rev _ BFS () {int font = 0, rear = 1; for (INT I = 0; I <= N; I ++) {// n indicates the total number of DIST [I] = maxn; numbs [I] = 0;} Q [font] = Ed; Dist [ed] = 0; numbs [0] = 1; while (font! = Rear) {int u = Q [font ++]; for (INT I = G [u]; I; I = E [I]. NXT) {If (E [I ^ 1]. CAP = 0 | Dist [E [I]. v] <maxn) continue; Dist [E [I]. v] = DIST [u] + 1; ++ numbs [Dist [E [I]. v]; Q [rear ++] = E [I]. V ;}}int maxflow () {rev_bfs (); int U, totalflow = 0; int curg [maxn], revpath [maxn]; for (INT I = 0; I <= N; ++ I) curg [I] = G [I]; u = sT; while (Dist [st] <n) {If (u = ed) {// find an Augmenting Path int augf Low = inf; For (INT I = sT; I! = Ed; I = E [curg [I]. v) augflow = min (augflow, E [curg [I]. CAP); For (INT I = sT; I! = Ed; I = E [curg [I]. v) {e [curg [I]. cap-= augflow; E [curg [I] ^ 1]. cap + = augflow; E [curg [I]. flow + = augflow; E [curg [I] ^ 1]. flow-= augflow;} totalflow + = augflow; u = sT;} int I; for (I = curg [u]; I; I = E [I]. NXT) if (E [I]. cap> 0 & Dist [u] = DIST [E [I]. v] + 1) break; if (I) {// find an admissible arc, then advance curg [u] = I; revpath [E [I]. v] = I ^ 1; u = E [I]. v;} else {// no admissible arc, Then relabel This vertex if (0 = (-- numbs [Dist [u]) break; // gap cut, important! Curg [u] = G [u]; int mindist = N; For (Int J = G [u]; j = E [J]. NXT) if (E [J]. cap> 0) mindist = min (mindist, DIST [E [J]. v]); Dist [u] = mindist + 1; ++ numbs [Dist [u]; If (u! = ST) u = E [revpath [u]. v; // backtrack} return totalflow;} int main () {int t; scanf ("% d", & T); While (t --) {scanf ("% d", & M, & N); Init (); If (! Flag) puts ("impossible"); else {int dd = maxflow (); If (DD! = Sum) {puts ("impossible"); continue;} For (INT I = 1; I <= m; I ++) {for (Int J = 1; j <= N; j ++) {int x = (I-1) * n + J; printf ("% d % C ", low [x] + E [ID [x]. flow, j = n? '\ N': '') ;}}if (t) puts (" ");} return 0 ;}
Poj 2396 budget (available stream with upstream and downstream sources)