- 题解
- 2015-05-30 17:38:03 @
本题一开始没理解题意,以为只要记最短距离就可以,后来才知道,最短路不一定只有一个,所以最后做一个判断(if d[x,i]+d[i,y]=d[x,y],inc(ans))
(注:这是我第一次用弗洛伊德,代码比较丑陋,希望别介意!):
var
n,m,x,y,p,i,j,k,ans:longint;
d:array[0..100,1..100,1..100]of longint;
map:array[1..100,1..100]of longint;
begin
read(n,m);
for i:=1 to n do
for j:=1 to n do
begin
if i=j
then map[i,j]:=0
else map[i,j]:=100000000;
end;
for i:=1 to m do
begin
read(x,y);
map[x,y]:=1;
map[y,x]:=1;
end;
for i:=1 to n do
for j:=1 to n do
d[0,i,j]:=map[i,j];
for k:=1 to n do
begin
for i:=1 to n do
for j:=1 to n do
begin
d[k,i,j]:=d[k-1,i,j];
if d[k,i,j]>d[k-1,i,k]+d[k-1,k,j]
then
d[k,i,j]:=d[k-1,i,k]+d[k-1,k,j];
end;
end;
read(p);
for i:=1 to p do
begin
ans:=0;
read(x,y);
for j:=1 to n do
if d[n,x,j]+d[n,j,y]=d[n,x,y]
then inc(ans);
writeln(ans);
end;
end.