JZOJ 4.8 2433——最短路上的统计【最短路】

Description

一个无向图上,没有自环,所有边的权值均为1,对于一个点对(a,b),我们要把所有a与b之间所有最短路上的点的总个数输出。

Input

第一行n,m,表示n个点,m条边
接下来m行,每行两个数a,b,表示a,b之间有条边
在下来一个数p,表示问题的个数
接下来p行,每行两个数a,b,表示询问a,b

Output

对于每个询问,输出一个数c,表示a,b之间最短路上点的总个数

Sample Input

5 6
1 2
1 3
2 3
2 4
3 5
4 5
3
2 5
5 1
2 4

Sample Output

4
3
2


先用Floyd求出两点之间的最短路
在先前推出总个数


代码如下:

var
  a:array[0..200,0..200] of longint;
  n,m,z,x,y,i,j,k,sum:longint;
begin
  readln(n,m);
  for i:=1 to n do
    for j:=1 to n do
      a[i,j]:=10000000;
  for i:=1 to m do
    begin
      readln(x,y);
      a[x,y]:=1;
      a[y,x]:=1;
    end;
  for k:=1 to n do
    for i:=1 to n do
      for j:=1 to n do
        if a[i,j]>a[i,k]+a[k,j] then a[i,j]:=a[i,k]+a[k,j];
  readln(z);
  for i:=1 to z do
    begin
      sum:=2;
      readln(x,y);
      for j:=1 to n do if a[x,j]+a[j,y]=a[x,y] then if (x<>j)and(j<>y) then inc(sum);
      writeln(sum);
    end;
end.
posted @ 2017-04-08 15:13  BEYang_Z  阅读(188)  评论(0编辑  收藏  举报