题目大意:
给出一个 N 个顶点 M 条边的无向无权图,顶点编号为 1-N。问从顶点 1 开始,到其他每个点的最短路有几条。
题解:
更新边长的时候如果大于号就覆盖,有相同最短路径就相加。
代码:
1 const 2 mo=100003; 3 type 4 arr=record 5 y,next:longint; 6 end; 7 var 8 n,m,nm:longint; 9 a:array [0..4000001] of arr; 10 ls,d,ans,list,v:array [0..1000001] of longint; 11 procedure add(x,y:longint); 12 begin 13 inc(nm); 14 a[nm].y:=y; a[nm].next:=ls[x]; ls[x]:=nm; 15 end; 16 17 procedure init; 18 var 19 i,x,y:longint; 20 begin 21 readln(n,m); 22 for i:=1 to m do 23 begin 24 readln(x,y); 25 add(x,y); add(y,x); 26 end; 27 end; 28 29 procedure spfa; 30 var 31 head,tail,i,x:longint; 32 begin 33 fillchar(d,sizeof(d),63); 34 head:=0; tail:=1; 35 list[1]:=1; d[1]:=0; ans[1]:=1; 36 repeat 37 inc(head); 38 x:=list[head]; 39 i:=ls[x]; 40 while i<>0 do 41 with a[i] do 42 begin 43 if d[x]+1<d[y] then 44 begin 45 d[y]:=d[x]+1; 46 ans[y]:=ans[x]; 47 if v[y]=0 then 48 begin 49 inc(tail); 50 list[tail]:=y; 51 v[y]:=1; 52 end; 53 end else 54 if d[y]=d[x]+1 then 55 ans[y]:=(ans[y]+ans[x]) mod mo; 56 i:=next; 57 end; 58 v[x]:=0; 59 until head>tail; 60 end; 61 62 procedure print; 63 var 64 i:longint; 65 begin 66 for i:=1 to n do 67 writeln(ans[i]); 68 end; 69 70 begin 71 init; 72 spfa; 73 print; 74 end.