DINIC網路流+當前弧最佳化

來源:互聯網
上載者:User

標籤:uil   style   var   als   log   type   bre   cti   max   

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網路流+當前弧最佳化

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.