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.
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 ui and vi (1 ≤ ui, vi ≤ n, ui ≠ vi), meaning that this road connects junctions ui and vi directly. It is guaranteed that there is a path between any two junctions and no two roads connect the same pair of junctions.
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
Copy
5 4 1 5
1 2
2 3
3 4
4 5
Output
0
Input
Copy
5 4 3 5
1 2
2 3
3 4
4 5
Output
5
Input
Copy
5 6 1 5
1 2
1 3
1 4
4 5
3 5
2 5
Output
3
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
#define INF 0x3f3f3f3f
const int MAXN =1e3+10;
int mp[MAXN][MAXN];
int d[MAXN],n;
int dt[MAXN];
int vis[MAXN],pre[MAXN];
void SPFA(int src,int d[]){
memset(vis,0,sizeof(vis));
for(int i=1;i<=n;i++) d[i]=INF,pre[i]=i;
d[src]=0;
queue<int> q;
q.push(src);
while(!q.empty()){
int u=q.front();
q.pop();
vis[u]=0;
for(int i=1;i<=n;i++)
{
if(i==u) continue;
if(mp[u][i]!=-1)
{
int tmp=d[u]+mp[u][i];
if(d[i]>tmp){
d[i]=tmp;
pre[i]=u;
if(!vis[i]){
vis[i]=true;
q.push(i);
}
}
}
}
}
}
int main()
{
int m,s,t;
while(scanf("%d%d%d%d",&n,&m,&s,&t)!=EOF)
{
memset(mp,-1,sizeof(mp));
for(int i=0;i<m;i++)
{
int u,v;
scanf("%d%d",&u,&v);
mp[u][v]=1;
mp[v][u]=1;
}
SPFA(s,d);
SPFA(t,dt);
memset(vis,0,sizeof(vis));
int xx=s;
while(xx!=pre[xx])
{
vis[xx]=1;
xx=pre[xx];
}
if(xx!=s) vis[xx]=1;
int ans=0;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(i==j) continue;
if(mp[i][j]==1) continue;
if(vis[i]&&vis[j]) continue;
if(i==s)
{
if(dt[j]+1>=d[t])
{
ans++;
mp[i][j]=1;
mp[j][i]=1;
}
}
else if(i==t)
{
if(d[j]+1>=d[t])
{
ans++;
mp[i][j]=1;
mp[j][i]=1;
}
}
else if(j==s)
{
if(dt[i]+1>=d[t])
{
ans++;
mp[i][j]=1;
mp[j][i]=1;
}
}
else if(j==t)
{
if(d[i]+1>=d[t])
{
ans++;
mp[i][j]=1;
mp[j][i]=1;
}
}
else if(d[i]+dt[j]+1>=d[t]&&d[j]+dt[i]+1>=d[t])
{
ans++;
mp[i][j]=1;
mp[j][i]=1;
}
}
}
printf("%d\n",ans);
}
return 0;
}