Решение
{$A+,B-,D+,E+,F-,G-,I-,L+,N+,O-,P-,Q+,R+,S+,T-,V+,X+,Y+}
{$M 65520,0,655360}
unit helpmrc_u;
interface
const
NMax = 100;
type
tFlow = array[1..NMax, 1..NMax] of integer;
tC = array[1..NMax, 1..NMax] of integer;
procedure FindFlow(var Flow : tFlow; const C : Tc; const s, t, N : integer);
implementation
type
tHigh = array[1..NMax] of integer;
var
h, e : THigh;
st : array[1..NMax] of integer;
inSt : array[1..NMax] of boolean;
hSt : integer;
procedure Error(const s : string);
begin
writeln('Ошибка '+s);
halt;
end;
function Minimum(a, b : integer) : integer;
begin
minimum := a;
if b < a then
minimum := b;
end;
procedure Push(k : integer);
begin
inc(hSt);
st[hSt] := k;
inSt[k] := true;
end;
procedure FindFlow(var Flow : tFlow; const C : TC; const s, t, N : integer);
var
i, cur, min : integer;
fl : boolean;
begin
fillChar(h, sizeOF(h), 0);
h[s] := N;
fillChar(flow, sizeOf(flow), 0);
fillChar(e, sizeOf(e), 0);
fillChar(inSt, sizeOf(inSt), false);
inSt[s] := true; inSt[t] := true;
hSt := 0;
for i := 1 to N do
if c[s, i] > 0 then begin
flow[s, i] := c[s, i];
inc(e[i], c[s, i]);
if not inSt[i] then
Push(i);
end;
while hSt <> 0 do begin
cur := st[hSt]; dec(hSt); inSt[cur] := false;
min := 3*N+1;
fl := true;
for i := 1 to n do
if c[cur, i]- flow[cur, i] > 0 then
if h[cur] = h[i]+1 then begin
fl := false;
min := minimum(e[cur], c[cur, i]-flow[cur, i]);
dec(e[cur], min);
inc(e[i], min);
inc(flow[cur, i], min);
if not inSt[i] and (e[i] > 0) then
Push(i);
end
else if h[i] < min then
min := h[i];
for i := 1 to n do
if flow[i, cur] > 0 then
if h[cur] = h[i]+1 then begin
fl := false;
min := minimum(e[cur], flow[i, cur]);
dec(e[cur], min);
inc(e[i], min);
dec(flow[i, cur], min);
if not inSt[i] and (e[i] > 0) then
push(i);
end
else if h[i] < min then
min := h[i];
if fl then
h[cur] := min + 1;
if e[cur] > 0 then
Push(cur);
end;
for i := 1 to n do
if (i<>s) and (i<>t) and (e[i] > 0) then
Error('Не поток, сливаем');
for cur := 1 to n do
for i := 1 to n do
if flow[cur, i] < 0 then
Error('Не поток, отрицателен');
end;
end.