比赛 20120705 评测结果 AAAAAATAAA
题目名称 塔防游戏 最终得分 90
用户昵称 SnowDancer 运行时间 0.000 s
代码语言 Pascal 内存使用 0.00 MiB
提交时间 2012-07-05 10:57:46
显示代码纯文本
program defence;
var
  d:array[1..1000,1..1000] of longint;
  c:array[1..1000,1..1000] of boolean;
  dis:array[1..1000] of longint;
  ina:array[1..1000] of boolean;
  gap,height,now:array[0..1000] of longint;
  n,i,j,k,l,m,s,t,mini,maxflow,minh,minE:longint;
  find:boolean;
procedure search(v:longint);
  var
    i:longint;
  begin
    ina[v]:=true;
    for i:=1 to n do
      if dis[v]+d[v,i]=dis[i] then begin
         c[v,i]:=true;
         if not ina[i] then search(i);
      end;
  end;
procedure SAP(k:longint);
  begin
    if k=t then begin
      find:=true;inc(maxflow);exit;
    end;
    while now[k]<=n do begin
      if c[k,now[k]] and (height[k]=height[now[k]]+1) then begin
        SAP(now[k]);
        if height[s]=n then exit;
        if find then break;
      end;
      inc(now[k]);
    end;
    if find then begin
      c[k,now[k]]:=false;
      c[now[k],k]:=true;
    end else begin
      minh:=n-1; minE:=1;
      for i:=1 to n do
        if c[k,i] and (height[i]<minh) then begin
          minE:=i; minh:=height[i];
        end;
      dec(gap[height[k]]);
      if gap[height[k]]=0 then height[s]:=n;
      height[k]:=minh+1;  now[k]:=minE;
      inc(gap[minh+1]);
    end;
  end;
begin
  assign(input,'defence.in');reset(input);
  assign(output,'defence.out');rewrite(output);
  // input
  readln(n,m,s,t);
  filldword(d,sizeof(d)>>2,maxlongint>>2);
  for i:=1 to n do d[i,i]:=0;
  for l:=1 to m do begin
    readln(i,j,d[i,j]);
    d[j,i]:=d[i,j];
  end;
  //  Dijkstra
  for i:=1 to n do dis[i]:=maxlongint>>2;
  dis[s]:=0;  l:=0;
  repeat
    inc(l); mini:=maxlongint; k:=0;
    for i:=1 to n do
      if not ina[i] and (dis[i]<mini) then begin
        k:=i;mini:=dis[i];
      end;
    ina[k]:=true;
    for i:=1 to n do
      if not ina[i] and (dis[k]+d[k,i]<dis[i]) then
        dis[i]:=dis[k]+d[k,i];
  until l=n;
  //  Build Graph
  fillchar(ina,sizeof(ina),false);
  search(s);
  for i:=1 to n do c[i,i]:=false;
  //  SAP
  for i:=1 to n do now[i]:=1;
  gap[0]:=n;
  repeat
    find:=false;
    SAP(s);
  until height[s]=n;
  //  Print
  writeln(maxflow);
  close(input); close(output);
end.