比赛 20120705 评测结果 AAWAAWAAAA
题目名称 塔防游戏 最终得分 80
用户昵称 fuhao 运行时间 0.000 s
代码语言 Pascal 内存使用 0.00 MiB
提交时间 2012-07-05 10:28:33
显示代码纯文本
const maxn=1000; maxm=500000;
var
 dis1,dis,dis2,hnum,h:array[0..maxn] of longint;
 w,a,c:array[0..maxn,0..maxn] of longint;
 f:array[0..maxm,1..3] of longint;
 u:array[0..maxn] of boolean;
 s,t,ans,i,max,n,m:longint; found:boolean;
 procedure insert(x,y:longint);
 begin inc(a[x,0]); a[x,a[x,0]]:=y; c[x,y]:=1; end;
 procedure sap(k:longint);
 var i,temp,hmin:longint;
 begin
  if k=t then
   begin
    found:=true;
    ans:=ans+max;
    exit;
   end;
  temp:=max; hmin:=n-1;
  for i:=1 to a[k,0] do
   if c[k,a[k,i]]>0 then
    begin
     if h[a[k,i]]+1=h[k] then
      begin
       if max>c[k,a[k,i]] then max:=c[k,a[k,i]];
       sap(a[k,i]);
       if h[0]>=n then exit;
       if found then break;
       max:=temp;
      end;
     if hmin>h[a[k,i]] then hmin:=h[a[k,i]];
    end;
  if found then
   begin
    dec(c[k,a[k,i]],max);
    inc(c[a[k,i],k],max);
   end else
   begin
    dec(hnum[h[k]]);
    if hnum[h[k]]=0 then h[0]:=n;
    h[k]:=hmin+1;
    inc(hnum[h[k]]);
   end;
 end;
 procedure dijkstra(s,t:longint);
 var i,j,k:longint;
 begin
  fillchar(u,sizeof(u),#0);
  fillchar(dis,sizeof(dis),$7f div 2);
  dis[s]:=0;
  for i:=1 to n do
   begin
    k:=0;
    for j:=1 to n do
     if (not u[j]) and (dis[j]<dis[k]) then k:=j;
    if k=0 then break;
    u[k]:=true;
    for j:=1 to n do
     if dis[j]>dis[k]+w[k,j] then
      dis[j]:=dis[k]+w[k,j];
   end;
 end;
begin
 assign(input,'defence.in'); reset(input);
 assign(output,'defence.out'); rewrite(output);
 readln(n,m,s,t);
 fillchar(w,sizeof(w),$7f div 2);
 for i:=1 to m do
  begin
   readln(f[i,1],f[i,2],f[i,3]);
   if w[f[i,1],f[i,2]]>f[i,3] then
    w[f[i,1],f[i,2]]:=f[i,3];
   if w[f[i,2],f[i,1]]>f[i,3] then
    w[f[i,2],f[i,1]]:=f[i,3];
  end;
 dijkstra(s,t);
 dis1:=dis;
 dijkstra(t,s);
 dis2:=dis;
 for i:=1 to m do
  if (dis1[f[i,1]]+dis2[f[i,2]]+f[i,3]=dis1[t])
  or (dis1[f[i,2]]+dis2[f[i,1]]+f[i,3]=dis1[t]) then
   begin
     insert(f[i,1],f[i,2]);
     insert(f[i,2],f[i,1]);
   end;
 hnum[0]:=n; h[s]:=0;
 while h[s]<=n-1 do
  begin
   found:=false;
   max:=maxlongint;
   sap(s);
  end;
 writeln(ans);
 close(input); close(output);
end.