• POJ1273 dinic Pascal模板


    之前写的其实不是dinic真不好意思……耽误大家了。现在重写了一个真正的dinic。 2013-05-13

    program dinic;
    
    //Algorithm:Dinic for Network
    //Author:HT
    //Date:2013/05/10
    
    Const
     inf=1000000;
    
    Type
     rec=record
       s,e,w,flow,next:longint;
     end;
    
    Var
     b,d,q:array[0..300] of longint;
     a:array[-1..450] of rec;
     v:array[0..300] of boolean;
     n,m,i,st,ed,ww,top,tar:longint;
    
    Function min(a,b:longint):longint;inline;begin if a<b then exit(a);exit(b); end;
    
    Procedure Add(st,ed,ww:longint);inline;
      begin
      inc(top);
      with a[top] do begin s:=st; e:=ed;w:=ww;next:=b[st]; end;
      b[st]:=top;
    end;
    
    function bfs:boolean;
    var
     head,tail,x,u:longint;
     y:rec;
      begin
      fillchar(v,sizeof(v),false);
      tail:=1;head:=0;d[st]:=1;
      v[st]:=true;
      q[1]:=st;
      while head<tail do
        begin
        inc(head);
        x:=q[head];
        u:=b[x];
        while u<>b[0] do
          begin
          y:=a[u];
          if (not v[y.e]) and (y.flow<y.w) then
            begin
            v[y.e]:=true;
            d[y.e]:=d[x]+1;
            inc(tail);
            q[tail]:=y.e;
          end;
          u:=y.next;
        end;
      end;
      exit(v[tar]);
    end;
    
    Function Addflow(p,maxflow:longint):longint;
    var
     u,o:longint;
     y:rec;
      begin
      if (p=tar) or (maxflow=0) then exit(maxflow);
      addflow:=0;
      u:=b[p];
      while u<>b[0] do
        begin
        y:=a[u];
        if (d[y.e]=d[p]+1) and (y.flow<y.w) then
          begin
          o:=Addflow(y.e,min(maxflow,y.w-y.flow));
          if o>0 then
            begin
            inc(a[u].flow,o);
            dec(a[u xor 1].flow,o);
            dec(maxflow,o);
            inc(addflow,o);
            if maxflow=0 then break;
          end;
        end;
        u:=y.next;
      end;
    
    end;
    
    
    function network:longint;
      begin
      network:=0;
      while bfs do
        begin
        inc(network,addflow(st,inf));
        //writeln('network=',network);
      end;
    end;
    
      begin
    
      while not eof do begin
      fillchar(b,sizeof(b),$ff);
      fillchar(a,20*(2*m+2),0);
      readln(m,n); top:=-1;
      for i:=1 to m do
        begin
        readln(st,ed,ww);
        Add(st,ed,ww);
        Add(ed,st,0);
      end;
      st:=1;tar:=n;
      writeln(Network);   end;
    
    end.
    
  • 相关阅读:
    八皇后之回溯算法
    手撕堆排序 优先级队列的实现方式
    扩展1000!(n!)的尾数零的个数
    #12. 整数转罗马数字 穷举法
    #11 盛最多水的容器
    Sql Server 复制数据库
    常见dos命令行
    JSP页面中,EL表达式获取根路径。
    maven springTest结合junit单元测试
    【Java异常】Exception in thread“main” java util ConcurrentModificationException的解决方案
  • 原文地址:https://www.cnblogs.com/htfy/p/2353147.html
Copyright © 2020-2023  润新知