D. Fight Against Traffic
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
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 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.
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
Copy
5 4 1 5 1 2 2 3 3 4 4 5
output
Copy
0
input
Copy
5 4 3 5 1 2 2 3 3 4 4 5
output
Copy
5
input
Copy
5 6 1 5 1 2 1 3 1 4 4 5 3 5 2 5
output
Copy
3
这个英语好难啊 , 给一个n个点,m条边,告诉你一个s和t点,作为起点和终点,问你有多少种加边的方案,使得s到t的最短路的距离不变(注意:这个边不能是直接相连接的边)
直接n^2就OK了 处理s和t的所有单源最短路,然后枚举两个点,直接判断即可
#include <bits/stdc++.h>
#include <time.h>
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef double db;
int xx[4] = {1,-1,0,0};
int yy[4] = {0,0,1,-1};
const double eps = 1e-9;
typedef pair<int,int> P;
const int maxn = 1e6;
const ll mod = 1e9 + 7;
inline int sign(db a) { return a < -eps ? -1 : a > eps;}
inline int cmp1(db a,db b){ return sign(a - b);}
ll mul(ll a,ll b,ll c) { ll res = 1; while(b) { if(b & 1) res *= a,res %= c; a *= a,a %= c,b >>= 1; } return res;}
ll phi(ll x) { ll res = x; for(ll i = 2; i * i <= x; i++) { if(x % i == 0) res = res / i * (i - 1); while(x % i == 0) x /= i; } if(x > 1) res = res / x * (x - 1); return res;}
void ex_gcd(ll a, ll b, ll &x, ll &y){if (b == 0) { x = 1;y = 0;return; } else { ex_gcd(b, a%b, x, y); ll t = x; x = y; y = t - (a / b)*y; } return ; }
int fa[maxn];
int Find(int x) { if(x != fa[x]) return fa[x] = Find(fa[x]); return fa[x];}
vector<int>v[maxn];
bool vis[maxn];
int s,t,m,n;
int dis[maxn];
int dis1[maxn];
queue<int>q;
void spfa(int st){
for(int i = 0;i <= n;i++) dis[i] = 1e9,vis[i] = 0;
vis[st] = 1;
dis[st] = 0;
while(!q.empty()) q.pop();
q.push(st);
while(!q.empty()){
int x = q.front(); q.pop();
vis[x] = 0;
for(auto d:v[x]){
if(dis[d] > dis[x] + 1){
dis[d] = dis[x] + 1;
if(!vis[d]) q.push(d),vis[d] = 1;
}
}
}
}void spfa1(int st){
for(int i = 0;i <= n;i++) dis1[i] = 1e9,vis[i] = 0;
vis[st] = 1;
dis1[st] = 0;
while(!q.empty()) q.pop();
q.push(st);
while(!q.empty()){
int x = q.front(); q.pop();
vis[x] = 0;
for(auto d:v[x]){
if(dis1[d] > dis1[x] + 1){
dis1[d] = dis1[x] + 1;
if(!vis[d]) q.push(d),vis[d] = 1;
}
}
}
}
map<P,int>mp;
int main() {
ios::sync_with_stdio(false);
while(cin >> n >> m >> s >> t){
for(int i = 1;i <= m;i++){
int u,vv;
cin >> u >> vv;
v[vv].push_back(u);
v[u].push_back(vv);
mp[P(u,vv)] = 1;
mp[P(vv,u)] = 1;
}
spfa(s);
spfa1(t);
mp[P(s,t)] = 1;
mp[P(t,s)] = 1;
int ans = 0;
for(int i = 1; i<= n;i++){
for(int j = 1;j <= n;j++){
if(i == j) continue;
if(!mp[P(i,j)] && dis[t] <= dis[i] + dis1[j] + 1 && dis1[i] + dis[j] + 1 >= dis[t]){
// cout << i<< " " << j << " *----" << dis[i] << " " <<dis1[j] << endl;
ans++;
}
}
}
cout << ans / 2 << endl;
}
cerr << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
return 0;
}