比赛 20120705 评测结果 AAEEEETEAA
题目名称 塔防游戏 最终得分 40
用户昵称 zhangchi 运行时间 0.000 s
代码语言 Pascal 内存使用 0.00 MiB
提交时间 2012-07-05 10:37:31
显示代码纯文本
type
  node=record
         v,w,next,num:longint;
       end;
  edge=record
         l,r,w,num:longint;
       end;
  nodeb=record
          v,next,num:longint;
        end;
var
  n,m,s,t,tot,i,j,p,totb,head,tail,max,k,short,ans:longint;
  f,g:array[1..1000] of longint;
  q:array[1..1000] of boolean;
  dis:array[1..1000] of longint;
  d:array[1..1000] of longint;
  dl:array[1..10000000] of longint;
  a:array[1..1000000] of node;
  b:array[1..1000000] of nodeb;
  e:array[1..1000000] of edge;
  count:array[1..1000000] of longint;
  procedure insert(x,y,z,u:longint);
  begin
    inc(tot);
    a[tot].next:=f[x];
    f[x]:=tot;
    a[tot].v:=y;
    a[tot].w:=z;
    a[tot].num:=u;
  end;
  procedure add;
  var
    i:longint;
  begin
    tot:=0;
    fillchar(f,sizeof(f),0);
    for i:=1 to m do
      begin
        insert(e[i].l,e[i].r,e[i].w,e[i].num);
        insert(e[i].r,e[i].l,e[i].w,e[i].num);
      end;
  end;
  procedure SPFA;
  begin
    fillchar(dis,sizeof(dis),$7F div 2);
    fillchar(q,sizeof(q),false);
    head:=0; tail:=1; totb:=0;
    fillchar(g,sizeof(g),0);
    d[1]:=s;
    q[s]:=true;
    dis[s]:=0;
    repeat
      head:=head mod n+1;
      q[d[head]]:=false;
      p:=f[d[head]];
      while p<>0 do
        begin
          if dis[d[head]]+a[p].w<dis[a[p].v] then
            begin
              dis[a[p].v]:=dis[d[head]]+a[p].w;
              inc(totb);
              g[a[p].v]:=0;
              b[totb].next:=g[a[p].v];
              g[a[p].v]:=totb;
              b[totb].v:=d[head];
              b[totb].num:=a[p].num;
              if q[a[p].v]=false then
                begin
                  q[a[p].v]:=true;
                  tail:=tail mod n+1;
                  d[tail]:=a[p].v;
                end;
            end
            else if dis[d[head]]+a[p].w=dis[a[p].v] then
              begin
                inc(totb);
                b[totb].next:=g[a[p].v];
                g[a[p].v]:=totb;
                b[totb].v:=d[head];
                b[totb].num:=a[p].num;
              end;
          p:=a[p].next;
        end;
    until head=tail;
  end;
  procedure BFS;
  begin
    fillchar(count,sizeof(count),0);
    max:=0; k:=0;
    head:=0; tail:=1;
    dl[1]:=t;
    repeat
      inc(head);
      p:=g[dl[head]];
      while p<>0 do
        begin
          inc(tail);
          dl[tail]:=b[p].v;
          inc(count[b[p].num]);
          if count[b[p].num]>max then
            begin
              max:=count[b[p].num];
              k:=b[p].num;
            end;
          p:=b[p].next;
        end;
    until head>=tail;
  end;
begin
  assign(input,'defence.in'); reset(input);
  assign(output,'defence.out'); rewrite(output);
  readln(n,m,s,t);
  for i:=1 to m do
    begin
      readln(e[i].l,e[i].r,e[i].w);
      e[i].num:=i;
    end;
  add;
  SPFA;
  short:=dis[t];
  while dis[t]=short do
    begin
      BFS;
      inc(e[k].w);
      inc(ans);
      add;
      SPFA;
    end;
  writeln(ans);
  close(input); close(output);
end.