P3245: 最快路线

这道题其实还是不难的,只是自己搞混了=-=//晕,做了好久啊,其实就是个spfa,关键是存储路径搞昏了。输出格式要求太严了,航模不能有空格啊,所以因为格式WA了三次,哭啊/(ㄒoㄒ)/~~。贴上代码吧=-=。

  1 const maxn=100000;
  2 type
  3   link=^node;
  4   node=record
  5     t,v,l:longint;
  6     f:link;
  7   end;
  8 var n,m,s,t,v,l,i,j,toto,sp:longint;
  9 dis:double;
 10 adj:array[0..500] of link;
 11 bl:array[0..500,0..500] of boolean;
 12 f,speed:array[0..1000000] of longint;
 13 go:array[0..500,0..500,1..2] of longint;
 14 d:array[0..500,0..500] of double;
 15 procedure insert(s,t,v,l:longint);
 16 var p:link;
 17 begin
 18   new(p);
 19   p^.f:=adj[s];
 20   p^.t:=t;
 21   p^.v:=v;
 22   p^.l:=l;
 23   adj[s]:=p;
 24 end;
 25 procedure spfa;
 26 var p:link;
 27 now,l,r,v:longint;
 28 begin
 29   for l:=0 to 500 do
 30     for r:=0 to n do d[l,r]:=maxn;
 31   fillchar(bl,sizeof(bl),true);
 32   l:=1; r:=1; d[70,1]:=0; f[1]:=1; bl[70,1]:=false; go[70,1,1]:=1; speed[1]:=70;
 33   while l<=r do
 34     begin
 35       now:=f[l];
 36       v:=speed[l];
 37       p:=adj[now];
 38       while p<>nil do
 39         begin
 40           if p^.v<>0 then
 41             begin
 42               if d[v,now]+(p^.l/p^.v)<d[p^.v,p^.t] then
 43                 begin
 44                   d[p^.v,p^.t]:=d[v,now]+(p^.l/p^.v);
 45                   go[p^.v,p^.t,1]:=now;
 46                   go[p^.v,p^.t,2]:=v;
 47                   if bl[p^.v,p^.t] then
 48                     begin
 49                       bl[p^.v,p^.t]:=false;
 50                       inc(r);
 51                       f[r]:=p^.t;
 52                       speed[r]:=p^.v;
 53                     end;
 54                 end;
 55             end
 56           else begin
 57             if d[v,now]+(p^.l/v)<d[v,p^.t] then
 58               begin
 59                 d[v,p^.t]:=d[v,now]+(p^.l/v);
 60                 go[v,p^.t,1]:=now;
 61                 go[v,p^.t,2]:=v;
 62                 if bl[v,p^.t] then
 63                   begin
 64                     bl[v,p^.t]:=false;
 65                     inc(r);
 66                     f[r]:=p^.t;
 67                     speed[r]:=v;
 68                   end;
 69               end;
 70           end;
 71           p:=p^.f;
 72         end;
 73       inc(l);
 74       bl[v,now]:=true;
 75     end;
 76 end;
 77 procedure print(x,v:longint);
 78 begin
 79   if (x<>1) then print(go[v,x,1],go[v,x,2]);
 80   if x<>toto then write(x-1,' ')
 81     else write(x-1);
 82 end;
 83 begin
 84   readln(n,m,toto);
 85   inc(toto);
 86   for i:=1 to m do
 87     begin
 88       readln(s,t,v,l);
 89       insert(s+1,t+1,v,l);
 90     end;
 91   spfa;
 92   dis:=maxn;
 93   for i:=0 to 500 do
 94     if d[i,toto]<dis then
 95       begin
 96         dis:=d[i,toto];
 97         sp:=i;
 98       end;
 99   print(toto,sp);
100 end.

(转载请注明出处:http://www.cnblogs.com/Kalenda/)

 

posted @ 2015-09-18 19:13  LovelyMonster丶  阅读(193)  评论(0编辑  收藏  举报