[Cpp]
Use FLOYD to find the minimum distance between any two points, use Dinic to find the maximum stream, and use the bipartite method to find the maximum distance and the minimum value.
# Include <iostream>
# Include <cstring>
# Include <cstdio>
Using namespace std;
# Deprecision MAX 300
# Define INF 1000000
Int dis [MAX] [MAX];
Int map [MAX] [MAX];
Bool sign [MAX] [MAX];
Bool used [MAX];
Int K, C, n, M;
Int min (int a, int B)
{
Return a <B? A: B;
}
Void Build_Graph (int min_max)
{
Int I, j;
Memset (map, 0, sizeof (map ));
For (I = K + 1; I <= n; I ++) // create an edge from the source point to each cow with a capacity of 1
Map [0] [I] = 1;
For (I = 1; I <= K; I ++) // edge is built from the milk collector to the sink point, with the capacity of M
Map [I] [n + 1] = M;
For (I = K + 1; I <= n; I ++) // edge is built from each dairy cow to the milk collector, with a capacity of 1
{
For (j = 1; j <= K; j ++)
{
If (dis [I] [j] <= min_max)
Map [I] [j] = 1;
}
}
}
Bool BFS () // build a layered network
{
Memset (used, 0, sizeof (used ));
Memset (sign, 0, sizeof (sign ));
Int queue [100 * MAX] = {0 };
Queue [0] = 0;
Used [0] = 1;
Int t = 1, f = 0;
While (f <t)
{
For (int I = 0; I <= n + 1; I ++)
{
If (! Used [I] & map [queue [f] [I])
{
Queue [t ++] = I;
Used [I] = 1;
Sign [queue [f] [I] = 1;
}
}
F ++;
}
If (used [n + 1])
Return true;
Else
Return false;
}
Int DFS (int v, int sum) // DFS augmented
{
Int I, s, t;
If (v = n + 1)
Return sum;
S = sum;
For (I = 0; I <= n + 1; I ++)
{
If (sign [v] [I])
{
T = DFS (I, min (map [v] [I], sum ));
Map [v] [I]-= t;
Map [I] [v] + = t;
Sum-= t;
}
}
Return s-sum;
}
Int main ()
{
Int I, j, k, L, R, mid, ans;
Cin> K> C> M;
N = K + C;
For (I = 1; I <= n; I ++)
{
For (j = 1; j <= n; j ++)
{
Cin> dis [I] [j];
If (dis [I] [j] = 0)
Dis [I] [j] = INF;
}
}
For (k = 1; k <= n; k ++) // floyd
For (I = 1; I <= n; I ++)
For (j = 1; j <= n; j ++)
{
Dis [I] [j] = min (dis [I] [j], dis [I] [k] + dis [k] [j]);
}
L = 0, R = 10000;
While (L <R) // Binary Search
{
Mid = (L + R)/2;
Ans = 0;
Build_Graph (mid );
While (BFS () ans + = DFS (0, INF); // calculate the maximum stream of dinic
If (ans> = C) R = mid;
Else L = mid + 1;
}
Cout <R <endl;
Return 0;