Description
Little Town Nsk consists of n junctions connected by M bidirectional
Roads. Each road connects distinct junctions and no roads
Connect the same pair of junctions. It is possible to get from any
Junction to any and junction by these roads. The distance between
Junctions 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, 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 by that the distance between
These junctions won ' t decrease.
You is assigned a task to compute the number of pairs of junctions
That is not a connected by the road, such if the new road between
These 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 is located respectively. The
I-th of the following m lines contains the integers UI and VI
(1≤ui, Vi≤n, ui≠vi), meaning that this road connects junctions
UI and VI directly. It is guaranteed this there is a path between any
Junctions and no 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, junctions
Won ' t decrease the distance between junctions S and T. 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
Ideas
This problem gives the n point and M Edge, and also to a starting point and end point, ask the maximum number of edges, you can make the starting point to the end of the shortest path will not become smaller .
First we can know that there are n points in the graph, up to N (n−1) 2 N (n−1) 2 \frac{n (n-1)}{2}, we can first run from the beginning and end of the shortest path, using the Dijkstra algorithm, assuming that starting from the beginning of the most short-circuit is dis1[] Array, starting at the end of the dis2[] array, we can enumerate the edges that are not present in the graph, and then determine if the condition is met:
Dis1[i]+1+dis2[j]>=dis1[ed] and dis2[i]+1+dis1[j]>= DIS2[ST] to prove that the addition of this edge does not affect the shortest path from the starting point to the end point, then it proves that this edge can be added code
#include <bits/stdc++.h>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define inf 0x3f3f3f3f
const int N=1000+20;
int e[N][N];
int n,m;
void init()
{
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
e[i][j]=i==j?0:inf;
}
struct Dijkstra
{
int vis[N],dis[N];
void dijkstra(int st)
{
for(int i=1; i<=n; i++)
{
dis[i]=inf;
vis[i]=0;
}
dis[st]=0;
for(int i=1; i<=n; i++)
{
int minn=inf,k;
for(int j=1; j<=n; j++)
if(!vis[j]&&dis[j]<minn)
{
minn=dis[j];
k=j;
}
vis[k]=1;
for(int j=1; j<=n; j++)
if(!vis[j]&&dis[k]+e[k][j]<dis[j])
dis[j]=dis[k]+e[k][j];
}
}
} ac1,ac2;
int main()
{
int u,v,st,ed;
scanf("%d%d%d%d",&n,&m,&st,&ed);
init();
for(int i=1; i<=m; i++)
{
scanf("%d%d",&u,&v);
e[u][v]=e[v][u]=1;
}
ac1.dijkstra(st);
ac2.dijkstra(ed);
int ans=0;
for(int i=1; i<=n; i++)
for(int j=i+1; j<=n; j++)
if(e[i][j]==inf)
if(ac1.dis[i]+1+ac2.dis[j]>=ac1.dis[ed]&&ac2.dis[i]+1+ac1.dis[j]>=ac2.dis[st])
ans++;
printf("%d\n",ans);
return 0;
}