- 最短路上的统计
- 2016-11-27 19:41:13 @
#include<bits/stdc++.h>
using namespace std;
int m,n,a,b,p;int c;int dis[101][101];
void floyd(){
int i,j,k;
for (int k=1;i<=n;i++){
for (int i=1;j<=n;j++){
for (int j=1;k<=n;k++){
if (dis[i][k]+dis[k][j]<dis[i][j]){
dis[j][i]=dis[i][j]=dis[i][k]+dis[k][j];
}
}
}
}
}
int main(){
scanf("%d%d",&n,&m);
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
if(i!=j) {dis[i][j] = 9999;}
else dis[i][j] = 0;
for (int i=1;i<=m;i++){
scanf("%d%d",&a,&b);
dis[a][b]=1;
dis[b][a]=1;
}
floyd();
scanf("%d",&p);
for (int i=1;i<=p;i++){
int ans=0;
scanf("%d%d",&a,&b);
for(int i=1;i<=n;i++)
if(dis[a][i] + dis[i][b] == dis[a][b])
{ans++;}
printf("%d\n",ans);
}
return 0;
}
2 条评论
-
刘锦钰是超神.. LV 8 @ 2016-11-29 18:48:09
嗯……
-
2016-11-27 19:45:52@
for (int i=1;i<=p;i++){ int ans=0; scanf("%d%d",&a,&b); for(int i=1;i<=n;i++) if(dis[a][i] + dis[i][b] == dis[a][b]) {ans++;} printf("%d\n",ans); }
这一部分两个循环变量都是i
- 1