比赛 20120710 评测结果 AAAAAAAAAA
题目名称 三元限制最短路 最终得分 100
用户昵称 zhangchi 运行时间 1.106 s
代码语言 Pascal 内存使用 69.81 MiB
提交时间 2012-07-10 08:59:44
显示代码纯文本
type
  node=record
         v,next:longint;
       end;
var
  n,m,k,i,j,temp,head,tail,qhead,p,x,y,z,tot:longint;
  a:array[1..3000,0..3000] of integer;
  b:array[1..3000,1..3000] of longint;
  c:array[1..100000] of node;
  dis:array[1..3000,1..3000] of integer;
  d:array[1..3000] of longint;
  q:array[1..3000] of boolean;
  bool:array[1..3000] of boolean;
  queue:array[1..100000] of integer;
  procedure print(x,y:longint);
  var
    i:longint;
  begin
    if x=1 then
      begin
        write(1);
        exit;
      end;
    for i:=1 to n do
      if dis[i,x]=y then
        begin
          print(i,y-1);
          break;
        end;
    write(' ',x);
  end;
  procedure SPFA;
  begin
    head:=0; tail:=1;
    fillchar(dis,sizeof(dis),$7F div 2);
    for i:=1 to n do
      dis[i,1]:=0;
    d[1]:=1;
    repeat
      head:=head mod n+1;
      temp:=d[head];
      q[temp]:=false;
      for j:=1 to n do
        begin
          qhead:=0;
          p:=b[j,temp];
          while p<>0 do
            begin
              bool[c[p].v]:=true;
              inc(qhead);
              queue[qhead]:=c[p].v;
              p:=c[p].next;
            end;
          for i:=1 to a[temp,0] do
            if (dis[j,temp]+1<dis[temp,a[temp,i]])and(bool[a[temp,i]]=false) then
              begin
                dis[temp,a[temp,i]]:=dis[j,temp]+1;
                if q[a[temp,i]]=false then
                  begin
                    q[a[temp,i]]:=true;
                    tail:=tail mod n+1;
                    d[tail]:=a[temp,i];
                  end;
              end;
          for i:=1 to qhead do
            bool[queue[i]]:=false;
        end;
    until head=tail;
  end;
begin
  assign(input,'patha.in'); reset(input);
  assign(output,'patha.out'); rewrite(output);
  readln(n,m,k);
  for i:=1 to m do
    begin
      readln(x,y);
      inc(a[x,0]); a[x,a[x,0]]:=y;
      inc(a[y,0]); a[y,a[y,0]]:=x;
    end;
  for i:=1 to k do
    begin
      readln(x,y,z);
      inc(tot);
      c[tot].next:=b[x,y];
      b[x,y]:=tot;
      c[tot].v:=z;
    end;
  SPFA;
  j:=1;
  for i:=1 to n do
    if dis[i,n]<dis[j,n] then j:=i;
  writeln(dis[j,n]);
  print(n,dis[j,n]);
  writeln;
  close(input); close(output);
end.