# Codeforces 954D. Fight Against Traffic (最短路)

Description
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, m, s and 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 u i and v i (1 ≤ u i, v i ≤ n, u i ≠ v i), meaning that this road connects junctions u i and 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
Input
5 4 1 5
1 2
2 3
3 4
4 5
Output
0

Input
5 4 3 5
1 2
2 3
3 4
4 5
Output
5

Input
5 6 1 5
1 2
1 3
1 4
4 5
3 5
2 5
Output
3

Solution

Code

const int maxn = 2e3 + 7;
int n,m,s,t;
vector<int>Edge[maxn];
int dis1[maxn],dis2[maxn];
void bfs1(){
clr(dis1,-1);
queue<int>q;q.push(s);
dis1[s] = 0;
while(!q.empty()){
int u = q.front();q.pop();
for(auto v : Edge[u]){
if(dis1[v] == -1){
dis1[v] = dis1[u] + 1;
q.push(v);
}
}
}
}
void bfs2(){
clr(dis2,-1);
queue<int>q;q.push(t);
dis2[t] = 0;
while(!q.empty()){
int u = q.front();q.pop();
for(auto v : Edge[u]){
if(dis2[v] == -1){
dis2[v] = dis2[u] + 1;
q.push(v);
}
}
}
}
bool vis[maxn][maxn];
int main() {
scanf("%d%d%d%d",&n,&m,&s,&t);
for(int i= 1,u,v;i <= m;++i){
scanf("%d%d",&u,&v);
Edge[u].pb(v);Edge[v].pb(u);
vis[u][v] = vis[v][u] = true;
}
bfs1(), bfs2();
int d = dis1[t], cnt = 0;
for(int i = 1;i <= n;++i){
for(int j = i + 1;j <= n;++j){
if(vis[i][j]) continue;
int mi = min(dis1[i] + dis2[j], dis1[j] + dis2[i]);
if(mi + 1 >= d) cnt++;
}
}
printf("%d\n", cnt);
return 0;
}


10-04 128