这题上次做的时候居然是WA!我感到很莫名其妙。 只是一道最简单的最短路而已。对于每个顶点,与其相邻的第一个顶点权值为0,其余相邻节点的权值为1。求出从起点到终点的最短路即可! program tram; const inf=10000000; maxn=110; len=2000000; type pointer=^node; node=record v,data:longint; next:pointer; end; var edge:array[0..maxn] of pointer; dist:array[0..maxn] of longint; vis:array[0..maxn] of boolean; que:array[0..len] of longint; n,s,t,head,tail:longint; procedure prework; var i:longint; begin for i:=1 to maxn do edge[i]:=nil; end; procedure init; var i,j,k:longint; p:pointer; begin readln(n,s,t); for i:=1 to n do begin read(k); new(p); if k>0 then begin read(p^.v); p^.data:=0; p^.next:=edge[i]; edge[i]:=p; end; for j:=2 to k do begin new(p); read(p^.v); p^.data:=1; p^.next:=edge[i]; edge[i]:=p; end; end; end; procedure prespfa(s:longint); begin head:=0; tail:=1; fillchar(vis,sizeof(vis),0); filldword(dist,sizeof(dist) shr 2,inf); dist[s]:=0; vis[s]:=true; que[1]:=s; end; procedure push(var x:longint); begin inc(x); if x>len then x:=0; end; procedure spfa(s:longint); var x:longint; p:pointer; begin prespfa(s); while head<>tail do begin push(head); x:=que[head]; vis[x]:=false; p:=edge[x]; while p<>nil do begin if dist[p^.v]>dist[x]+p^.data then begin dist[p^.v]:=dist[x]+p^.data; if not vis[p^.v] then begin vis[p^.v]:=true; push(tail); que[tail]:=p^.v; end; end; p:=p^.next; end; end; end; procedure outit; begin if dist[t]<>inf then writeln(dist[t]) else writeln(-1); end; begin assign(input,'a.in'); reset(input); assign(output,'a.out'); rewrite(output); prework; init; spfa(s); outit; close(input); close(output); end.