Fight Against Traffic (Codeforces 954D)
Tags: Others, shortest paths

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.


Sample Inputs:
5 4 1 5
1 2
2 3
3 4
4 5

5 4 3 5
1 2
2 3
3 4
4 5

5 6 1 5
1 2
1 3
1 4
4 5
3 5
2 5

Sample Outputs:
0

5

3

Solution (Python):
rd = lambda : map(int, input().split())
(n, m, s, t) = rd()
a = [0] * (n + 1)
b = a + []
G = [[] for i in range(n + 1)]
for i in range(m):
    (u, v) = rd()
    G[u].append(v)
    G[v].append(u)

def bfs(x, a):
    q = [x]
    while q:
        c = q.pop()
        for y in G[c]:
            if not a[y]:
                a[y] = a[c] + 1
                q.insert(0, y)
    a[x] = 0
bfs(s, a)
bfs(t, b)
print(sum((min(a[i] + b[j], a[j] + b[i]) > a[t] - 2 for i in range(1, n + 1) for j in range(i + 1, n + 1))) - m)

Solution (Python):
(n, m, s, t) = list(map(int, input().split(' ')))
s -= 1
t -= 1
G = [[] for i in range(n)]
M = [[False for i in range(n)] for j in range(n)]
for i in range(m):
    (a, b) = input().split(' ')
    a = int(a) - 1
    b = int(b) - 1
    G[a].append(b)
    G[b].append(a)
    M[a][b] = M[b][a] = True
from collections import deque as dq
from math import inf

def bfs(G, visited, s):
    q = dq([s])
    visited[s] = 0
    while len(q) != 0:
        top = q.popleft()
        for i in G[top]:
            if visited[i] == inf:
                visited[i] = visited[top] + 1
                q.append(i)
dist_s = [inf for i in range(n)]
bfs(G, dist_s, s)
mx_depth = dist_s[t]
dist_t = [inf for i in range(n)]
bfs(G, dist_t, t)
ans = 0
for u in range(n):
    for v in range(u + 1, n):
        if dist_s[u] + dist_t[v] + 1 >= mx_depth and dist_s[v] + dist_t[u] + 1 >= mx_depth and (not M[u][v]):
            ans += 1
            M[u][v] = M[v][u] = True
print(ans)

Solution (Python):
(n, m, s, t) = map(int, input().split())
adj = [[] for i in range(n + 1)]
for i in range(m):
    (x, y) = map(int, input().split())
    adj[x] += [y]
    adj[y] += [x]
ds = [0 for i in range(n + 1)]
dt = [0 for i in range(n + 1)]
q = [s]
while len(q) != 0:
    cur = q.pop(0)
    for i in adj[cur]:
        if ds[i] == 0:
            ds[i] = ds[cur] + 1
            q += [i]
ds[s] = 0
q = [t]
while len(q) != 0:
    cur = q.pop(0)
    for i in adj[cur]:
        if dt[i] == 0:
            dt[i] = dt[cur] + 1
            q += [i]
dt[t] = 0
ans = 0
for i in range(1, n + 1):
    for j in range(i + 1, n + 1):
        if i in adj[j]:
            continue
        a = min(ds[i] + dt[j] + 1, dt[i] + ds[j] + 1)
        if a >= ds[t]:
            ans += 1
print(ans)