If a person does not have an outbound vertex, he or she can jump to any vertex. The minimum number of jumps makes it necessary to walk all the edges.
Question: The smallest stream with the upper and lower bounds.
[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 550
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 in [Max], out [Max];
Int n, m, s, t, eid;
Struct node
{
Int to, next, c;
} E [1, 40400], ee [2, 40400];
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;
}
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];
}
}
Return maxflow;
}
Int main ()
{
Int m, n, t, count = 1, sum;
Int u, v, I, j, k, x, y, tp;
Scanf ("% d", & t );
While (t --)
{
Scanf ("% d", & n, & m );
Memset (p,-1, sizeof (p ));
Eid = 0;
Sum = 0;
Memset (in, 0, sizeof (in ));
Memset (out, 0, sizeof (out ));
For (I = 0; I <m; I ++)
{
Scanf ("% d", & u, & v, & tp );
If (tp = 1)
{
In [v] + = 1;
Sum + = 1;
Out [u] + = 1;
Addedge (u, v, inf-1 );
Addedge (v, u, 0 );
}
Else
{
Addedge (u, v, inf );
Addedge (v, u, 0 );
}
}
For (I = 1; I <= n; I ++)
{
Addedge (n + 2, I, in [I]);
Addedge (I, n + 2, 0 );
Addedge (I, n + 3, out [I]);
Addedge (n + 3, I, 0 );
Addedge (0, I, inf );
Addedge (I, 0, 0 );
If (out [I] = 0)
{
Addedge (I, n + 1, inf );
Addedge (n + 1, I, 0 );
}
}
Addedge (n + 1, 0, inf );
Addedge (0, n + 1, 0 );
Int l, r, mid;
L = 0; r = sum + 2;
While (l <= r)
{
Mid = (l + r)> 1;
For (I = 0; I <= eid; I ++) e [I] = ee [I];
E [p [n + 1]. c = mid;
E [p [0]. c = 0;
If (ISAP (n + 2, n + 3, n + 4, count) = sum)
R = mid-1;
Else
L = mid + 1;
}
Printf ("Case # % d: % d \ n", count ++, l );
}
}