POJ 2396 Budget upstream and downstream network streams

Source: Internet
Author: User

This is a basic upstream/downstream network stream.
The upstream and downstream network stream algorithms are based entirely on the paper "a simple method for solving the network with the upper and lower bounds of traffic" and a model for the application of the largest stream in the informatics competition.
Then I will talk about it in more detail.
The specific solution is no longer mentioned. The network flow algorithm is always troublesome and the code volume is large. After reading it for a long time, I can see what the algorithm means.
At the beginning, I was confused why I added a pair of source sink and another pair. Later, I understood that for this question, it was a network with a source sink at the beginning,
It is the first source sink we add, and we want to convert it into a network without source sink, and add an infinite capacity edge to the source, which satisfies the defined requirements, later, we added a pair of source sink, which is the additional source sink mentioned in the paper.
Cool code
When the answer is output. At the beginning, I felt a lot of pain. Later I found that the edge was added in order, and I instantly felt that the world was beautiful.
In my template, the cap stored in the edge indicates how much traffic is available on the edge, and flow indicates the traffic that is used now.
 
[Cpp]
# Include <iostream>
# Include <algorithm>
# Include <cstring>
# Include <string>
# Include <cstdio>
# Include <cmath>
# Include <queue>
# Include <map>
# Include <set>
# Define maxn555
# Define MAXM 555555
# Define INF 1000000007
Using namespace std;
Struct node
{
Int ver; // vertex
Int cap; // capacity
Int flow; // current flow in this arc
Int next, rev;
} Edge [MAXM];
Int dist [MAXN], numbs [MAXN], src, des, n;
Int head [MAXN], e;
Void add (int x, int y, int c)
{// The total number of edges recorded by e
Edge [e]. ver = y;
Edge [e]. cap = c;
Edge [e]. flow = 0;
Edge [e]. rev = e + 1; // subscript position of the reverse edge in the edge
Edge [e]. next = head [x]; // records the subscript position of the last edge starting from x in the edge.
Head [x] = e ++; // position of the edge starting from x
// Reverse side
Edge [e]. ver = x;
Edge [e]. cap = 0; // The Initial Feasible traffic of the reverse edge is 0.
Edge [e]. flow = 0;
Edge [e]. rev = e-1;
Edge [e]. next = head [y];
Head [y] = e ++;
}
Void rev_BFS ()
{
Int Q [MAXN], qhead = 0, qtail = 0;
For (int I = 1; I <= n; ++ I)
{
Dist [I] = MAXN;
Numbs [I] = 0;
}
Q [qtail ++] = des;
Dist [des] = 0;
Numbs [0] = 1;
While (qhead! = Qtail)
{
Int v = Q [qhead ++];
For (int I = head [v]; I! =-1; I = edge [I]. next)
{
If (edge [edge [I]. rev]. cap = 0 | dist [edge [I]. ver] <MAXN) continue;
Dist [edge [I]. ver] = dist [v] + 1;
++ Numbs [dist [edge [I]. ver];
Q [qtail ++] = edge [I]. ver;
}
}
}
Void init ()
{
E = 0;
Memset (head,-1, sizeof (head ));
}
Int maxflow ()
{
Int u, totalflow = 0;
Int Curhead [MAXN], revpath [MAXN];
For (int I = 1; I <= n; ++ I) Curhead [I] = head [I];
U = src;
While (dist [src] <n)
{
If (u = des) // find an augmenting path
{
Int augflow = INF;
For (int I = src; I! = Des; I = edge [Curhead [I]. ver)
Augflow = min (augflow, edge [Curhead [I]. cap );
For (int I = src; I! = Des; I = edge [Curhead [I]. ver)
{
Edge [Curhead [I]. cap-= augflow;
Edge [edge [Curhead [I]. rev]. cap + = augflow;
Edge [Curhead [I]. flow + = augflow;
Edge [edge [Curhead [I]. rev]. flow-= augflow;
}
Totalflow + = augflow;
U = src;
}
Int I;
For (I = Curhead [u]; I! =-1; I = edge [I]. next)
If (edge [I]. cap> 0 & dist [u] = dist [edge [I]. ver] + 1) break;
If (I! =-1) // find an admissible arc, then Advance
{
Curhead [u] = I;
Revpath [edge [I]. ver] = edge [I]. rev;
U = edge [I]. ver;
}
Else // no admissible arc, then relabel this vertex
{
If (0 = (-- numbs [dist [u]) break; // GAP cut, Important!
Curhead [u] = head [u];
Int mindist = n;
For (int j = head [u]; j! =-1; j = edge [j]. next)
If (edge [j]. cap> 0) mindist = min (mindist, dist [edge [j]. ver]);
Dist [u] = mindist + 1;
++ Numbs [dist [u];
If (u! = Src)
U = edge [revpath [u]. ver; // Backtrack
}
}
Return totalflow;
}
Int low [MAXN] [MAXN], up [MAXN] [MAXN];
Int xj [MAXN];
Int col, row, s, t;
Bool build ()
{
For (int I = 1; I <= row; I ++)
For (int j = 1; j <= col; j ++)
{
If (low [I] [j]> up [I] [j]) return false;
Else
{
Xj [I]-= low [I] [j];
Xj [j + row] + = low [I] [j];
Add (I, j + row, up [I] [j]-low [I] [j]);
}
}
Return true;
}
Void solve ()
{
Src = t + 1;
Des = t + 2;
N = des;
For (int I = 1; I <= t; I ++)
If (xj [I]> 0) add (src, I, xj [I]);
Else if (xj [I] <0) add (I, des,-xj [I]);
Add (t, s, INF );
Rev_BFS ();
Maxflow ();
For (int I = head [src]; I! =-1; I = edge [I]. next)
If (edge [I]. cap> 0)
{
Printf ("IMPOSSIBLE \ n ");
Return;
}
For (int I = 1; I <= row; I ++)
For (int j = 1; j <= col; j ++)
{
Printf ("% d", edge [(I-1) * col + j-1) * 2]. flow + low [I] [j]);
If (j <col) putchar ('');
Else putchar ('\ n ');
}
Printf ("\ n ");
}
Int main ()
{
Int T, u, v, w;
Char op [5];
Scanf ("% d", & T );
While (T --)
{
Init ();
Scanf ("% d", & row, & col );
Memset (xj, 0, sizeof (xj ));
For (int I = 0; I <row + 5; I ++)
For (int j = 0; j <col + 5; j ++)
Low [I] [j] = 0, up [I] [j] = INF;
S = row + col + 1;
T = row + col + 2;
For (int I = 1; I <= row; I ++)
{
Scanf ("% d", & u );
Xj [s]-= u;
Xj [I] + = u;
}
For (int I = row + 1; I <= row + col; I ++)
{
Scanf ("% d", & u );
Xj [t] + = u;
Xj [I]-= u;
}
Int q, lc, rc, lr, rr;
Scanf ("% d", & q );
While (q --)
{
Scanf ("% d % s % d", & u, & v, op, & w );
Lr = rr = u;
Lc = rc = v;
If (u = 0) lr = 1, rr = row;
If (v = 0) lc = 1, rc = col;
For (int I = lr; I <= rr; I ++)
For (int j = lc; j <= rc; j ++)
{
If (op [0] = ') low [I] [j] = max (low [I] [j], w ), up [I] [j] = min (up [I] [j], w );
Else if (op [0] = '<') up [I] [j] = min (w-1, up [I] [j]);
Else if (op [0] = '>') low [I] [j] = max (low [I] [j], w + 1 );
}
}
If (build () solve ();
Else printf ("IMPOSSIBLE \ n ");
}
Return 0;
}

Related Article

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.