zoukankan      html  css  js  c++  java
  • poj1273 sap算法的模型

    简单基础网络流,用sap算法,不过这道题有多重边,被阴了,WA一次,还有就是这题有多组数据(居然没看到...)

    program poj1273;
    var
      n,m,i,j,i1,j1:integer;
      ans,min,p:longint;
      flag:boolean;
      g:array[1..200,1..200] of longint;
      fir,now,t,num,liu:array[0..200] of longint;(fir[i]是表示增广路i的前一个节点,now[i]是当前边的两个端点为i,now[i],t[i]是顶点数为i的节点的个数,num[i]是第i个节点的编号,liu[i]是当前节点1到i的流量
    procedure init;
    var
      x,y,z:longint;
    begin
      readln(m,n);
      for i:=1 to m do
      begin
        readln(x,y,z);
        inc(g[x,y],z);
      end;
    end;
    begin
      while not eof do
      begin
        fillchar(g,sizeof(g),0);
        fillchar(fir,sizeof(fir),0);
        fillchar(now,sizeof(now),0);
        fillchar(t,sizeof(t),0);
        fillchar(num,sizeof(num),0);
        fillchar(liu,sizeof(liu),0);
        init;
        t[0]:=n;
        for i:=1 to n do
          now[i]:=1;
        ans:=0;
        i:=1;
        p:=maxlongint;
        while num[i]<n do
        begin
          flag:=false;
          liu[i]:=p;
          for j:=now[i] to n do
            if (g[i,j]>0) and (num[j]+1=num[i]) then
            begin
              flag:=true;
              if g[i,j]<p then p:=g[i,j];
              fir[j]:=i;
              now[i]:=j;
              i:=j;
              if i=n then
              begin
                inc(ans,p);
                while i<>1 do
                begin
                  i1:=i;
                  i:=fir[i];
                  dec(g[i,i1],p);
                  inc(g[i1,i],p);
                end;
                p:=maxlongint;
              end;
              break;
            end;
          if flag then continue;
          min:=n-1;//没有允许弧了,需要重标号
          for j:=1 to n do
            if (g[i,j]>0) and (num[j]<min) then
            begin
              j1:=j;
              min:=num[j];
            end;
          now[i]:=j1;
          dec(t[num[i]]);//GAP优化
          if t[num[i]]=0 then break;
          num[i]:=min+1;
          inc(t[num[i]]);
          if i<>1 then
          begin
            i:=fir[i];
            p:=liu[i];
          end;
        end;
        writeln(ans);
      end;
    end.

  • 相关阅读:
    MAC使用小技巧(二)
    Swift # GET&POST请求 网络缓存的简单处理
    iOS:Block写递归
    Swift # 项目框架
    uva 11665 Chinese Ink (几何+并查集)
    uva 11859 Division Game
    uva 11916 Emoogle Grid (BSGS)
    uva 11754 Code Feat (中国剩余定理)
    hdu 4347 The Closest M Points(KD树)
    2013多校训练赛第四场 总结
  • 原文地址:https://www.cnblogs.com/stmq/p/2567311.html
Copyright © 2011-2022 走看看