time limit per test : 1 second
memory limit per test : 256 megabytes
Little town Nsk consists of n junctions connected by m bidirectional roads. Each road connects two distinct junctions and no two roads connect the same pair of junctions. It is possible to get from any junction to any other junction by these roads. The distance between two junctions is equal to the minimum possible number of roads on a path between them.
In order to improve the transportation system, the city council asks mayor to build one new road. The problem is that the mayor has just bought a wonderful new car and he really enjoys a ride from his home, located near junction s to work located near junction t. Thus, he wants to build a new road in such a way that the distance between these two junctions won’t decrease.
You are assigned a task to compute the number of pairs of junctions that are not connected by the road, such that if the new road between these two junctions is built the distance between s and t won’t decrease.
Input
The firt line of the input contains integers n n ,, s s and (2 ≤ n ≤ 1000,1 ≤ m ≤ 1000,1 ≤ s, t ≤ n,s ≠ t) ( 2 ≤ n ≤ 1000 , 1 ≤ m ≤ 1000 , 1 ≤ s , t ≤ n , s ≠ t ) — the number of junctions and the number of roads in Nsk, as well as the indices of junctions where mayors home and work are located respectively. The i-th of the following m lines contains two integers ui u i and vi(1 ≤ ui, vi ≤ n,ui ≠ vi) v i ( 1 ≤ u i , v i ≤ n , u i ≠ v i ) , meaning that this road connects junctions ui u i and vi v i directly. It is guaranteed that there is a path between any two junctions and no two roads connect the same pair of junctions.
Output
Print one integer — the number of pairs of junctions not connected by a direct road, such that building a road between these two junctions won’t decrease the distance between junctions s and t.
Examples
Input1
5 4 1 5
1 2
2 3
3 4
4 5
Output1
0
Input2
5 4 3 5
1 2
2 3
3 4
4 5
Output2
5
Input3
5 6 1 5
1 2
1 3
1 4
4 5
3 5
2 5
Output3
3
题意:
经典问题:给一张无向图,没有重边,增加一条无向边(不能增加重边),使得s到t的最短路径不变。求问符合这种情况的边有多少条?
题解:
disS[i]
d
i
s
S
[
i
]
表示从
s
s
点出发走到点的最短距离,
disT[i]
d
i
s
T
[
i
]
表示从
t
t
点出发走到点的最短距离,枚举增加的边
<i,j>
<
i
,
j
>
<script type="math/tex" id="MathJax-Element-16">< i,j ></script>,如果
disS[i]+disT[j]+1
d
i
s
S
[
i
]
+
d
i
s
T
[
j
]
+
1
和
disS[j]+disT[i]+1
d
i
s
S
[
j
]
+
d
i
s
T
[
i
]
+
1
都大于等于
disS[t]
d
i
s
S
[
t
]
,那么则
<i,j>
<
i
,
j
>
<script type="math/tex" id="MathJax-Element-20">
</script> 是符合条件的。
#include<bits/stdc++.h>
#define LiangJiaJun main
#define INF 2001052800
using namespace std;
int n,m,s,t;
bool mp[1004][1004],inq[1004];
int dis[2][1004];
queue<int>q;
void spfa(int os,int st){
for(int i=1;i<=n;i++)dis[os][i]=INF;
memset(inq,0,sizeof(inq));
q.push(st);inq[st]=1;dis[os][st]=0;
while(!q.empty()){
int x=q.front();q.pop();
for(int i=1;i<=n;i++){
if(!mp[x][i])continue;
if( dis[os][i]>dis[os][x]+1){
dis[os][i]=dis[os][x]+1;
if(!inq[i]){
inq[i]=1;
q.push(i);
}
}
}
inq[x]=0;
}
}
int LiangJiaJun(){
scanf("%d%d%d%d",&n,&m,&s,&t);
for(int i=1;i<=m;i++){
int u,v;
scanf("%d%d",&u,&v);
mp[u][v]=mp[v][u]=1;
}
spfa(0,s);
spfa(1,t);
int ans=0;
for(int i=1;i<=n;i++){
for(int j=i+1;j<=n;j++){
if(!mp[i][j]){
if(dis[1][i]+dis[0][j]+1>=dis[0][t]&&
dis[1][j]+dis[0][i]+1>=dis[0][t]){
++ans;
}
}
}
}
printf("%d\n",ans);
return 0;
}