DINIC网络流+当前弧优化

Posted G . H . O . S . T . R . E . A

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了DINIC网络流+当前弧优化相关的知识,希望对你有一定的参考价值。

const
  inf=1000000000;
type
  rec=record
    s,e,w,next:longint;
  end;
var
  b,bb,d,q:array[-1..300001] of longint;
  a:array[-1..1000001] of rec;
  e:array[0..300001,1..3]of longint;
  n,m,i,j,k,l,st,ed,ww,top,ans,nn,dd,x:longint;
function min(aa,bb:longint):longint;
begin
  if aa<bb then exit(aa);exit(bb);
end;
procedure add(st,ed,ww:longint);
begin
  inc(top);
  a[top].s:=st;
  a[top].e:=ed;
  a[top].w:=ww;
  a[top].next:=b[st];
  b[st]:=top;
end;
function bfs:boolean;
var head,tail,x,u,i:longint;
  y:rec;
begin
  for i:=1 to n do d[i]:=-1;
  tail:=1; head:=0; d[st]:=0; q[1]:=st;
  while head<tail do
  begin
    inc(head); x:=q[head];
    u:=b[x];
    while u>0 do
    begin
      y:=a[u];
      if(d[y.e]=-1)and(y.w>0)then
      begin
        d[y.e]:=d[x]+1;
        inc(tail); q[tail]:=y.e;
      end;
      u:=y.next;
    end;
  end;
  if d[ed]=-1 then exit(false);
  exit(true);
end;
function addflow(p,maxflow:longint):longint;
var
  o,u:longint;
  y:rec;
begin
  if(p=ed)or(maxflow=0)then exit(maxflow);
  addflow:=0; u:=bb[p];
  while u>0 do
  begin
    y:=a[u];
    if(d[y.e]=d[p]+1)and(y.w>0)then
    begin
      o:=addflow(y.e,min(maxflow,y.w));
      if o>0 then
      begin
        dec(a[u].w,o); if a[u].w>0 then bb[p]:=u;
        inc(a[u xor 1].w,o);
        dec(maxflow,o); inc(addflow,o);
        if maxflow=0 then break;
      end;
    end;
    u:=y.next;
  end;
  if addflow=0 then d[p]:=-1;
end;
function network:longint;
var i:longint;
begin
  network:=0;
  while bfs do
  begin
    for i:=st to ed do bb[i]:=b[i];
    inc(network,addflow(st,inf));
  end;
end;
begin
  readln(n,m);
  top:=1; build;
  writeln(network);
end.

以上是关于DINIC网络流+当前弧优化的主要内容,如果未能解决你的问题,请参考以下文章

dinic及当前弧优化

最大流当前弧优化Dinic分层模板

P3376 (最大流 dinic)

网络流之当前弧优化浅谈

luogu3376 模板网络最大流 dinic

CODEVS1993 草地排水