Some sides of the net must be full streams to find the minimum feasible stream.
Question: The Upper and Lower Bound minimum streams. For more information, see Zhou Yuan's "A Simple Method for Solving Network Flow Problems in networks with the upper and lower bound traffic". There is also a non-binary method, but there is no strict proof, so I still use binary.
[Cpp]
# Include <stdio. h>
# Include <stdlib. h>
# Include <string. h>
# Include <string>
# Include <queue>
# Include <algorithm>
# Include <vector>
# Include <stack>
# Include <list>
# Include <iostream>
# Include <map>
Using namespace std;
# Define inf 0x3f3f3f
# Deprecision Max 110
Int max (int a, int B)
{
Return a> B? A: B;
}
Int min (int a, int B)
{
Return a <B? A: B;
}
Int dis [Max], gap [Max], pre [Max], cur [Max], p [Max];
// Int d [4] [2] = {0, 1, 0, 0,-1,-1 };
Int in [Max], out [Max], id [Max * Max];
Int n, m, s, t, eid;
Int c [Max * Max];
Struct node
{
Int to, next, c;
} E [2 * Max], ee [2 * Max];
Void addedge (int u, int v, int c)
{
Ee [eid]. to = v;
Ee [eid]. c = c;
Ee [eid]. next = p [u];
P [u] = eid ++;
}
Int ISAP (int st, int ed, int n, int count) // start point, end point, number of vertices
{
Memset (dis, 0, sizeof (dis ));
Memset (gap, 0, sizeof (gap); gap [0] = n;
Memcpy (cur, p, sizeof (p); // memcpy!
Int I, flag, v, u = pre [st] = st, maxflow = 0, aug = inf; // puts ("akk ");
While (dis [st] <n)
{
For (flag = 0, I = cur [u]; I! =-1; I = e [I]. next) // cur [u]
If (e [I]. c & dis [u] = dis [e [I]. to] + 1)
{
Flag = 1;
Break;
}
If (flag)
{
If (aug> e [I]. c)
Aug = e [I]. c;
V = e [I].;
Pre [v] = u;
Cur [u] = I;
U = v;
If (u = ed)
{
For (u = pre [u]; 1; u = pre [u]) // notice!
{
E [cur [u]. c-= aug;
E [cur [u] ^ 1]. c + = aug;
If (u = st) break;
// Puts ("akkk ");
}
Maxflow + = aug;
Aug = inf;
}
}
Else
{
Int minx = n;
For (I = p [u]; I! =-1; I = e [I]. next)
If (e [I]. c & dis [e [I]. to] <minx)
{
Minx = dis [e [I]. to];
Cur [u] = I;
}
If (-- gap [dis [u] = 0)
Break;
Dis [u] = minx + 1;
Gap [dis [u] ++;
U = pre [u];
}
}
// Printf ("Case % d: \ n % d \ n", count, maxflow );
// Printf ("% d \ n", maxflow );
Return maxflow;
}
Int main ()
{
Int m, n, t, count = 1, sum;
Int u, v, I, j, k, x, y, tp;
While (scanf ("% d", & n, & m )! = EOF)
{
Memset (p,-1, sizeof (p ));
Eid = 0;
Sum = 0;
Memset (in, 0, sizeof (in ));
Memset (out, 0, sizeof (out ));
Memset (id,-1, sizeof (id ));
For (I = 0; I <m; I ++)
{
Scanf ("% d", & u, & v, & c [I], & tp );
If (tp = 1)
{
In [v] + = c [I];
Sum + = c [I];
Out [u] + = c [I];
}
Else
{
Id [I] = eid;
Addedge (u, v, c [I]);
Addedge (v, u, 0 );
}
}
For (I = 1; I <= n; I ++)
{
Addedge (0, I, in [I]);
Addedge (I, 0, 0 );
Addedge (I, n + 1, out [I]);
Addedge (n + 1, I, 0 );
}
Addedge (n, 1, inf );
Addedge (1, n, 0 );
Int l, r, mid;
L = 0; r = inf;
// Printf ("% d \ n", inf); puts ("akkkk ");
While (l <= r)
{
Mid = (l + r)> 1;
For (I = 0; I <= eid; I ++) e [I] = ee [I];
E [p [n]. c = mid;
E [p [1]. c = 0;
If (ISAP (0, n + 1, n + 2, count ++) = sum)
R = mid-1;
Else
L = mid + 1;
}
// ISAP (0, n * m + 1, n * m + 2, count ++ );
If (l> = inf)
Puts ("Impossible ");
Else
{
For (I = 0; I <= eid; I ++) e [I] = ee [I];
E [p [n]. c = l;
E [p [1]. c = 0;
ISAP (0, n + 1, n + 2, count ++ );
Printf ("% d \ n", l );
For (I = 0; I <m; I ++)
{
If (I = 0)
{
If (id [I] =-1)
Printf ("% d", c [I]);
Else
Printf ("% d", e [id [I] ^ 1]. c );
}
Else
{
If (id [I] =-1)
Printf ("% d", c [I]);
Else
Printf ("% d", e [id [I] ^ 1]. c );
}
}
Puts ("");
}
}