Problem:


Score : 700 points

Problem Statement

We have a graph with N vertices and M edges, and there are two people on the graph: Takahashi and Aoki.

The i-th edge connects Vertex U_i and Vertex V_i.
The time it takes to traverse this edge is D_i minutes, regardless of direction and who traverses the edge (Takahashi or Aoki).

Takahashi departs Vertex S and Aoki departs Vertex T at the same time. Takahashi travels to Vertex T and Aoki travels to Vertex S, both in the shortest time possible.
Find the number of the pairs of ways for Takahashi and Aoki to choose their shortest paths such that they never meet (at a vertex or on an edge) during the travel, modulo 10^9 + 7.

Constraints

1 \leq N \leq 100 000

1 \leq M \leq 200 000

1 \leq S, T \leq N

S \neq T

1 \leq U_i, V_i \leq N (1 \leq i \leq M)

1 \leq D_i \leq 10^9 (1 \leq i \leq M)

If i \neq j, then (U_i, V_i) \neq (U_j, V_j) and (U_i, V_i) \neq (V_j, U_j).

U_i \neq V_i (1 \leq i \leq M)

D_i are integers.

The given graph is connected.

Input

Input is given from Standard Input in the following format:

N M
S T
U_1 V_1 D_1
U_2 V_2 D_2
:
U_M V_M D_M

Output

Print the answer.

Sample Input 1

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

Sample Output 1

2

There are two ways to choose shortest paths that satisfies the condition:

Takahashi chooses the path 1 \rightarrow 2 \rightarrow 3, and Aoki chooses the path 3 \rightarrow 4 \rightarrow 1.

Takahashi chooses the path 1 \rightarrow 4 \rightarrow 3, and Aoki chooses the path 3 \rightarrow 2 \rightarrow 1.

Sample Input 2

3 3
1 3
1 2 1
2 3 1
3 1 2

Sample Output 2

2

Sample Input 3

3 3
1 3
1 2 1
2 3 1
3 1 2

Sample Output 3

2

Sample Input 4

8 13
4 2
7 3 9
6 2 3
1 6 4
7 6 9
3 8 9
1 2 2
2 8 12
8 6 9
2 5 5
4 2 18
5 3 7
5 1 515371567
4 8 6

Sample Output 4

6



Sample input:
4 4
1 3
1 2 1
2 3 1
3 4 1
4 1 1


Sample output:
2

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3

import heapq

INF = 10 ** 18
MOD = 10 ** 9 + 7

def solve(n, m, s, t, edges, edge_d):

    dist = [[INF] * n for _ in range(2)]
    dist[0][s] = 0
    dist[1][t] = 0

    dp = [[0] * n for _ in range(2)]
    dp[0][s] = 1
    dp[1][t] = 1

    target = [t, s]

    for i in range(2):
        target_i = target[i]
        q = []

        for v in range(n):
            heapq.heappush(q, (dist[i][v], v))

        end = False
        while 0 < len(q):
            du, u = heapq.heappop(q)
            if du != dist[i][u]:
                continue
            if end and dist[i][target_i] < du:
                break
            for v, d in edges[u]:
                alt = du + d
                if dist[i][v] > alt:
                    dist[i][v] = alt
                    dp[i][v] = dp[i][u]
                    heapq.heappush(q, (alt, v))
                elif dist[i][v] == alt:
                    dp[i][v] += dp[i][u]
                if v == target_i:
                    end = True

    spt = dist[0][t]

    c_point = set()
    n_point = set()
    f_point = set()
    c_edge = set()
    for v in range(n):
        if dist[0][v] + dist[1][v] == spt:
            if dist[0][v] == dist[1][v]:
                c_point.add(v)
            elif dist[0][v] < dist[1][v]:
                n_point.add(v)
            else:
                f_point.add(v)

    for u in n_point:
        for v, d in edges[u]:
            if v in f_point:
                if dist[0][u] + d + dist[1][v] == spt:
                    c_edge.add((u, v))


    ans = 0
    for u in c_point:
        for v in c_point:
            if u == v: continue
            ans += dp[0][u] * dp[1][u] * dp[0][v] * dp[1][v]
            ans %= MOD
        for vn, vf in c_edge:
            ans += dp[0][u] * dp[1][u] * dp[0][vn] * dp[1][vf]
            ans %= MOD
    for un, uf in c_edge:
        for v in c_point:
            ans += dp[0][un] * dp[1][uf] * dp[0][v] * dp[1][v]
            ans %= MOD
        for vn, vf in c_edge:
            if un == vn and uf == vf: continue
            ans += dp[0][un] * dp[1][uf] * dp[0][vn] * dp[1][vf]
            ans %= MOD

    return ans


def main():
    n, m = input().split()
    n = int(n)
    m = int(m)
    s, t = input().split()
    s = int(s) - 1
    t = int(t) - 1
    edges = [[] for _ in range(n)]
    edge_d = {}
    for _ in range(m):
        u, v, d = input().split()
        u = int(u) - 1
        v = int(v) - 1
        d = int(d)
        edges[u].append((v, d))
        edges[v].append((u, d))
        edge_d[(u, v)] = d
        edge_d[(v, u)] = d

    print(solve(n, m, s, t, edges, edge_d))


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3

import heapq

INF = 10 ** 18
MOD = 10 ** 9 + 7

def solve(n, m, s, t, edges, edge_d):

    dist = [[INF] * n for _ in range(2)]
    dist[0][s] = 0
    dist[1][t] = 0

    dp = [[0] * n for _ in range(2)]
    dp[0][s] = 1
    dp[1][t] = 1

    target = [t, s]

    for i in range(2):
        target_i = target[i]
        q = []

        for v in range(n):
            heapq.heappush(q, (dist[i][v], v))

        end = False
        while 0 < len(q):
            du, u = heapq.heappop(q)
            if du != dist[i][u]:
                continue
            if end and dist[i][target_i] < du:
                break
            for v, d in edges[u]:
                alt = du + d
                if dist[i][v] > alt:
                    dist[i][v] = alt
                    dp[i][v] = dp[i][u]
                    heapq.heappush(q, (alt, v))
                elif dist[i][v] == alt:
                    dp[i][v] += dp[i][u]
                if v == target_i:
                    end = True

    spt = dist[0][t]

    c_point = set()
    n_point = set()
    f_point = set()
    c_edge = set()
    for v in range(n):
        if dist[0][v] + dist[1][v] == spt:
            if dist[0][v] == dist[1][v]:
                c_point.add(v)
            elif dist[0][v] < dist[1][v]:
                n_point.add(v)
            else:
                f_point.add(v)

    for u in n_point:
        for v in f_point:
            if (u, v) in edge_d.keys():
                if dist[0][u] + edge_d[(u, v)] + dist[1][v] == spt:
                    c_edge.add((u, v))


    ans = 0
    for u in c_point:
        for v in c_point:
            if u == v: continue
            ans += dp[0][u] * dp[1][u] * dp[0][v] * dp[1][v]
            ans %= MOD
        for vn, vf in c_edge:
            ans += dp[0][u] * dp[1][u] * dp[0][vn] * dp[1][vf]
            ans %= MOD
    for un, uf in c_edge:
        for v in c_point:
            ans += dp[0][un] * dp[1][uf] * dp[0][v] * dp[1][v]
            ans %= MOD
        for vn, vf in c_edge:
            if un == vn and uf == vf: continue
            ans += dp[0][un] * dp[1][uf] * dp[0][vn] * dp[1][vf]
            ans %= MOD

    return ans


def main():
    n, m = input().split()
    n = int(n)
    m = int(m)
    s, t = input().split()
    s = int(s) - 1
    t = int(t) - 1
    edges = [[] for _ in range(n)]
    edge_d = {}
    for _ in range(m):
        u, v, d = input().split()
        u = int(u) - 1
        v = int(v) - 1
        d = int(d)
        edges[u].append((v, d))
        edges[v].append((u, d))
        edge_d[(u, v)] = d
        edge_d[(v, u)] = d

    print(solve(n, m, s, t, edges, edge_d))


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from heapq import heapify,heappop,heappush
import sys
input=sys.stdin.readline
N,M=map(int,input().split())
S,T=map(int,input().split())
Graph=[set() for i in range(N)]
Edge=[]
mod=10**9+7
inf=float("inf")
for i in range(M):
  a,b,c=map(int,input().split())
  Graph[a-1].add((c,b-1))
  Graph[b-1].add((c,a-1))
  Edge.append((a-1,b-1,c))
  Edge.append((b-1,a-1,c))
def dijkstra(s,n,links):
  Visited=[False]*n
  cost=[inf]*n
  P=[0]*n
  P[s]=1
  cost[s]=0
  heap=[(0,s)]
  heapify(heap)
  while heap:
    hc,hp=heappop(heap)
    if Visited[hp]:
      continue
    Visited[hp]=True
    for c,p in links[hp]:
      if Visited[p]:
        continue
      if c+hc<cost[p]:
        cost[p]=c+hc
        P[p]=P[hp]
        heappush(heap,(cost[p],p))
      elif c+hc==cost[p]:
        P[p]=(P[p]+P[hp])%mod
  return cost,P

cost_S,P_S=dijkstra(S-1,N,Graph)
cost_T,P_T=dijkstra(T-1,N,Graph)
print
D=cost_S[T-1]
Pat=P_S[T-1]
ans=(Pat**2)%mod
for a,b,c in Edge:
  if cost_S[a]+cost_T[b]+c==D:
    if 2*cost_S[a]<D and 2*cost_T[b]<D:
      ans-=((P_S[a]*P_T[b])**2)%mod
for i in range(N):
  if 2*cost_S[i]==2*cost_T[i]==D:
    ans-=((P_S[i]*P_T[i])**2)%mod
print(ans%mod)

Submission status: Time Limit Exceeded

Solution code:
from heapq import heapify,heappop,heappush
N,M=map(int,input().split())
S,T=map(int,input().split())
Graph=[set() for i in range(N)]
Edge=[]
mod=10**9+7
inf=float("inf")
for i in range(M):
  a,b,c=map(int,input().split())
  Graph[a-1].add((c,b-1))
  Graph[b-1].add((c,a-1))
  Edge.append((a-1,b-1,c))
  Edge.append((b-1,a-1,c))
def dijkstra(s,n,links):
  Visited=[False]*n
  cost=[inf]*n
  P=[0]*n
  P[s]=1
  cost[s]=0
  heap=[(0,s)]
  heapify(heap)
  while heap:
    hc,hp=heappop(heap)
    if Visited[hp]:
      continue
    Visited[hp]=True
    for c,p in links[hp]:
      if Visited[p]:
        continue
      if c+hc<cost[p]:
        cost[p]=c+hc
        P[p]=P[hp]
        heappush(heap,(cost[p],p))
      elif c+hc==cost[p]:
        P[p]=(P[p]+P[hp])%mod
  return cost,P

cost_S,P_S=dijkstra(S-1,N,Graph)
cost_T,P_T=dijkstra(T-1,N,Graph)
print
D=cost_S[T-1]
Pat=P_S[T-1]
ans=(Pat**2)%mod
for a,b,c in Edge:
  if cost_S[a]+cost_T[b]+c==D:
    if 2*cost_S[a]<D and 2*cost_T[b]<D:
      ans-=((P_S[a]*P_T[b])**2)%mod
for i in range(N):
  if 2*cost_S[i]==2*cost_T[i]==D:
    ans-=((P_S[i]*P_T[i])**2)%mod
print(ans%mod)

Submission status: Time Limit Exceeded

Solution code:
from heapq import heapify,heappop,heappush
N,M=map(int,input().split())
S,T=map(int,input().split())
Graph=[set() for i in range(N)]
Edge=[]
mod=10**9+7
inf=float("inf")
for i in range(N):
  Edge.append((i,i,0))
for i in range(M):
  a,b,c=map(int,input().split())
  Graph[a-1].add((c,b-1))
  Graph[b-1].add((c,a-1))
  Edge.append((a-1,b-1,c))
  Edge.append((b-1,a-1,c))
def dijkstra(s,n,links):
  Visited=[False]*n
  cost=[inf]*n
  P=[0]*n
  P[s]=1
  cost[s]=0
  heap=[(0,s)]
  heapify(heap)
  while heap:
    hc,hp=heappop(heap)
    if Visited[hp]:
      continue
    for c,p in links[hp]:
      if Visited[p]:
        continue
      if c+hc<cost[p]:
        cost[p]=c+hc
        P[p]=P[hp]
        heappush(heap,(cost[p],p))
      elif c+hc==cost[p]:
        P[p]=(P[p]+P[hp])%mod
    Visited[hp]=True
  return cost,P

def dp(t,n,links,d):
  Visited=[False]*n
  cost=[inf]*n
  P=[0]*n
  P[t]=1
  cost[t]=0
  heap=[(0,t)]
  heapify(heap)
  while heap:
    hc,hp=heappop(heap)
    if hc>d/2:
      break
    if Visited[hp]:
      continue
    for c,p in links[hp]:
      if Visited[p]:
        continue
      if c+hc<cost[p]:
        cost[p]=c+hc
        P[p]=P[hp]
        heappush(heap,(cost[p],p))
      elif c+hc==cost[p]:
        P[p]=(P[p]+P[hp])%mod
    Visited[hp]=True
  return cost,P

cost_S,P_S=dijkstra(S-1,N,Graph)
D=cost_S[T-1]
cost_T,P_T=dp(T-1,N,Graph,D)
List=[]
for a,b,c in Edge:
  if cost_S[a]+c+cost_T[b]==D and cost_S[a]<D/2 and cost_T[b]<D/2 and c>0:
    List.append((P_S[a]*P_T[b])%mod)
  if cost_S[a]+c+cost_T[b]==D and cost_S[a]==D/2 and cost_T[b]==D/2 and c==0:
    List.append((P_S[a]*P_T[b])%mod)
Sum=0
for i in List:
  Sum=(Sum+i)%mod
ans=0
for i,c in enumerate(List):
  ans=(ans+(Sum-List[i])*List[i])%mod
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from heapq import heapify,heappop,heappush
N,M=map(int,input().split())
S,T=map(int,input().split())
Graph=[set() for i in range(N)]
Edge=[]
mod=10**9+7
inf=float("inf")
for i in range(N):
  Edge.append((i,i,0))
for i in range(M):
  a,b,c=map(int,input().split())
  Graph[a-1].add((c,b-1))
  Graph[b-1].add((c,a-1))
  Edge.append((a-1,b-1,c))
  Edge.append((b-1,a-1,c))
def dijkstra(s,n,links):
  Visited=[False]*n
  cost=[inf]*n
  P=[0]*n
  P[s]=1
  cost[s]=0
  heap=[(0,s)]
  heapify(heap)
  while heap:
    hc,hp=heappop(heap)
    for c,p in links[hp]:
      if Visited[p]:
        continue
      if c+hc<cost[p]:
        cost[p]=c+hc
        P[p]=P[hp]
        heappush(heap,(cost[p],p))
      elif c+hc==cost[p]:
        P[p]=(P[p]+P[hp])%mod
    Visited[hp]=True
  return cost,P

def dp(t,n,links,d):
  Visited=[False]*n
  cost=[inf]*n
  P=[0]*n
  P[t]=1
  cost[t]=0
  heap=[(0,t)]
  heapify(heap)
  while heap:
    hc,hp=heappop(heap)
    if hc>d/2:
      break
    for c,p in links[hp]:
      if Visited[p]:
        continue
      if c+hc<cost[p]:
        cost[p]=c+hc
        P[p]=P[hp]
        heappush(heap,(cost[p],p))
      elif c+hc==cost[p]:
        P[p]=(P[p]+P[hp])%mod
    Visited[hp]=True
  return cost,P

cost_S,P_S=dijkstra(S-1,N,Graph)
D=cost_S[T-1]
cost_T,P_T=dp(T-1,N,Graph,D)
List=[]
for a,b,c in Edge:
  if cost_S[a]+c+cost_T[b]==D and cost_S[a]<D/2 and cost_T[b]<D/2 and c>0:
    List.append((P_S[a]*P_T[b])%mod)
  if cost_S[a]+c+cost_T[b]==D and cost_S[a]==D/2 and cost_T[b]==D/2 and c==0:
    List.append((P_S[a]*P_T[b])%mod)
Sum=0
for i in List:
  Sum=(Sum+i)%mod
ans=0
for i,c in enumerate(List):
  ans=(ans+(Sum-List[i])*List[i])%mod
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from heapq import heapify,heappop,heappush
N,M=map(int,input().split())
S,T=map(int,input().split())
Graph=[set() for i in range(N)]
Edge=[]
mod=10**9+7
inf=float("inf")
for i in range(N):
  Edge.append((i,i,0))
for i in range(M):
  a,b,c=map(int,input().split())
  Graph[a-1].add((c,b-1))
  Graph[b-1].add((c,a-1))
  Edge.append((a-1,b-1,c))
  Edge.append((b-1,a-1,c))
def dijkstra(s,n,links):
  Visited=[False]*n
  cost=[inf]*n
  P=[0]*n
  P[s]=1
  cost[s]=0
  heap=[(0,s)]
  heapify(heap)
  while heap:
    hc,hp=heappop(heap)
    for c,p in links[hp]:
      if Visited[p]:
        continue
      if c+hc<cost[p]:
        cost[p]=c+hc
        P[p]=P[hp]
        heappush(heap,(cost[p],p))
      elif c+hc==cost[p]:
        P[p]=(P[p]+P[hp])%mod
    Visited[hp]=True
  return cost,P
  
cost_S,P_S=dijkstra(S-1,N,Graph)
cost_T,P_T=dijkstra(T-1,N,Graph)
D=cost_S[T-1]
List=[]
for a,b,c in Edge:
  if cost_S[a]+c+cost_T[b]==D and cost_S[a]<D/2 and cost_T[b]<D/2 and c>0:
    List.append((P_S[a]*P_T[b])%mod)
  if cost_S[a]+c+cost_T[b]==D and cost_S[a]==D/2 and cost_T[b]==D/2 and c==0:
    List.append((P_S[a]*P_T[b])%mod)
Sum=0
for i in List:
  Sum=(Sum+i)%mod
ans=0
for i,c in enumerate(List):
  ans=(ans+(Sum-List[i])*List[i])%mod
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from heapq import heapify,heappop,heappush
N,M=map(int,input().split())
S,T=map(int,input().split())
Graph=[set() for i in range(N)]
Edge=[]
mod=10**9+7
inf=float("inf")
for i in range(N):
  Edge.append((i,i,0))
for i in range(M):
  a,b,c=map(int,input().split())
  Graph[a-1].add((c,b-1))
  Graph[b-1].add((c,a-1))
  Edge.append((a-1,b-1,c))
  Edge.append((b-1,a-1,c))
def dijkstra(s,n,links):
  Visited=[False]*n
  Visited[0]=True
  cost=[inf]*n
  P=[0]*n
  P[s]=1
  cost[s]=0
  heap=[(0,s)]
  heapify(heap)
  while heap:
    hc,hp=heappop(heap)
    for c,p in links[hp]:
      if Visited[p]:
        continue
      if c+hc<cost[p]:
        cost[p]=c+hc
        P[p]=P[hp]
        heappush(heap,(cost[p],p))
      elif c+hc==cost[p]:
        P[p]+=P[hp]
      if c+hc>cost[p]:
        Visited[p]=True
  return cost,P
  
cost_S,P_S=dijkstra(S-1,N,Graph)
cost_T,P_T=dijkstra(T-1,N,Graph)
D=cost_S[T-1]
List=[]
for a,b,c in Edge:
  if cost_S[a]+c+cost_T[b]==D and cost_S[a]<D/2 and cost_T[b]<D/2 and c>0:
    List.append(P_S[a]*P_T[b]%mod)
  if cost_S[a]+c+cost_T[b]==D and cost_S[a]==D/2 and cost_T[b]==D/2 and c==0:
    List.append(P_S[a]*P_T[b]%mod)
Sum=sum(List)%mod
ans=0
for i,c in enumerate(List):
  ans=(ans+(Sum-List[i])*List[i])%mod
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from heapq import heapify,heappop,heappush
N,M=map(int,input().split())
S,T=map(int,input().split())
Graph=[set() for i in range(N)]
Edge=[]
mod=10**9+7
inf=float("inf")
for i in range(N):
  Edge.append((i,i,0))
for i in range(M):
  a,b,c=map(int,input().split())
  Graph[a-1].add((c,b-1))
  Graph[b-1].add((c,a-1))
  Edge.append((a-1,b-1,c))
  Edge.append((b-1,a-1,c))
def dijkstra(s,n,links):
  cost=[inf]*n
  P=[0]*n
  P[s]=1
  cost[s]=0
  heap=[(0,s)]
  heapify(heap)
  while heap:
    hc,hp=heappop(heap)
    for c,p in links[hp]:
      if c+hc<cost[p]:
        cost[p]=c+hc
        P[p]=P[hp]
        heappush(heap,(cost[p],p))
      elif c+hc==cost[p]:
        P[p]+=P[hp]
  return cost,P
  
cost_S,P_S=dijkstra(S-1,N,Graph)
cost_T,P_T=dijkstra(T-1,N,Graph)
D=cost_S[T-1]
List=[]
for a,b,c in Edge:
  if cost_S[a]+c+cost_T[b]==D and cost_S[a]<D/2 and cost_T[b]<D/2 and c>0:
    List.append(P_S[a]*P_T[b]%mod)
  if cost_S[a]+c+cost_T[b]==D and cost_S[a]==D/2 and cost_T[b]==D/2 and c==0:
    List.append(P_S[a]*P_T[b]%mod)
Sum=sum(List)%mod
ans=0
for i,c in enumerate(List):
  ans=(ans+(Sum-List[i])*List[i])%mod
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    n,m = LI()
    s,t = LI()
    e = collections.defaultdict(list)
    for _ in range(m):
        u,v,d = LI()
        e[u].append((v,d))
        e[v].append((u,d))

    def search(s,t):
        d = collections.defaultdict(lambda: inf)
        dc = collections.defaultdict(int)
        d[s] = 0
        dc[s] = 1
        q = []
        heapq.heappush(q, (0, s))
        v = collections.defaultdict(bool)
        while len(q):
            k, u = heapq.heappop(q)
            if v[u]:
                continue
            v[u] = True

            dc[u] %= mod
            if u == t:
                return (d,dc)

            uc = dc[u]

            for uv, ud in e[u]:
                if v[uv]:
                    continue
                vd = k + ud
                if d[uv] < vd:
                    continue
                if d[uv] > vd:
                    d[uv] = vd
                    dc[uv] = uc
                    heapq.heappush(q, (vd, uv))
                elif d[uv] == vd:
                    dc[uv] += uc

        return (d,dc)

    d1,dc1 = search(s,t)
    d2,dc2 = search(t,s)
    rd = d1[t]
    kk = rd / 2.0

    r = dc1[t] ** 2 % mod
    for k in list(d1.keys()):
        t = d1[k]
        c = dc1[k]
        if t > kk:
            continue
        if t == kk:
            if d2[k] == t:
                r -= (c**2 * dc2[k]**2) % mod
            continue

        for uv, ud in e[k]:
            if d2[uv] >= kk or t + ud + d2[uv] != rd:
                continue
            r -= (c**2 * dc2[uv]**2) % mod

    return r % mod




print(main())

Submission status: Time Limit Exceeded

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    n,m = LI()
    s,t = LI()
    e = collections.defaultdict(list)
    for _ in range(m):
        u,v,d = LI()
        e[u].append((v,d))
        e[v].append((u,d))

    def search(s,t):
        d = collections.defaultdict(lambda: inf)
        dc = collections.defaultdict(int)
        d[s] = 0
        dc[s] = 1
        q = []
        heapq.heappush(q, (0, s))
        v = collections.defaultdict(bool)
        while len(q):
            k, u = heapq.heappop(q)
            if v[u]:
                continue
            v[u] = True

            if u == t:
                return (d,dc)

            uc = dc[u]

            for uv, ud in e[u]:
                if v[uv]:
                    continue
                vd = k + ud
                if d[uv] < vd:
                    continue
                if d[uv] > vd:
                    d[uv] = vd
                    dc[uv] = uc
                    heapq.heappush(q, (vd, uv))
                elif d[uv] == vd:
                    dc[uv] += uc

        return (d,dc)

    d1,dc1 = search(s,t)
    d2,dc2 = search(t,s)
    rd = d1[t]
    kk = rd / 2.0

    r = dc1[t] ** 2 % mod
    for k in list(d1.keys()):
        t = d1[k]
        c = dc1[k]
        if t > kk:
            continue
        if t == kk:
            if d2[k] == t:
                r -= (c**2 * dc2[k]**2) % mod
            continue

        for uv, ud in e[k]:
            if d2[uv] >= kk or t + ud + d2[uv] != rd:
                continue
            r -= (c**2 * dc2[uv]**2) % mod

    return r % mod




print(main())

Submission status: Time Limit Exceeded

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    n,m = LI()
    s,t = LI()
    e = collections.defaultdict(list)
    for _ in range(m):
        u,v,d = LI()
        e[u].append((v,d))
        e[v].append((u,d))

    def search(s,t):
        d = collections.defaultdict(lambda: [inf, 0])
        d[s] = [0, 1]
        q = []
        heapq.heappush(q, (0, s))
        v = collections.defaultdict(bool)
        while len(q):
            k, u = heapq.heappop(q)
            if v[u]:
                continue
            v[u] = True

            if u == t:
                return d

            uc = d[u][1]

            for uv, ud in e[u]:
                if v[uv]:
                    continue
                vd = k + ud
                if d[uv][0] < vd:
                    continue
                if d[uv][0] > vd:
                    d[uv] = [vd, uc]
                    heapq.heappush(q, (vd, uv))
                elif d[uv][0] == vd:
                    d[uv][1] += uc

        return d

    d1 = search(s,t)
    d2 = search(t,s)
    rd = d1[t][0]
    kk = rd / 2.0

    r = d1[t][1] ** 2 % mod
    for k in list(d1.keys()):
        t,c = d1[k]
        if t > kk:
            continue
        if t == kk:
            if d2[k][0] == t:
                r -= (c**2 * d2[k][1]**2) % mod
            continue

        for uv, ud in e[k]:
            if d2[uv][0] >= kk or t + ud + d2[uv][0] != rd:
                continue
            r -= (c**2 * d2[uv][1]**2) % mod

    return r % mod




print(main())

Submission status: Time Limit Exceeded

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    n,m = LI()
    s,t = LI()
    e = collections.defaultdict(list)
    for _ in range(m):
        u,v,d = LI()
        e[u].append((v,d))
        e[v].append((u,d))

    def search(s,t):
        d = collections.defaultdict(lambda: [inf, 0])
        d[s] = [0, 1]
        q = []
        heapq.heappush(q, (0, s))
        v = collections.defaultdict(bool)
        while len(q):
            k, u = heapq.heappop(q)
            if v[u]:
                continue
            v[u] = True

            if u == t:
                return d

            uc = d[u][1]

            for uv, ud in e[u]:
                if v[uv]:
                    continue
                vd = k + ud
                if d[uv][0] > vd:
                    d[uv] = [vd, uc]
                    heapq.heappush(q, (vd, uv))
                elif d[uv][0] == vd:
                    d[uv][1] += uc

        return d

    d1 = search(s,t)
    rd = d1[t][0]
    kk = rd / 2.0

    def search2(s,t):
        d = collections.defaultdict(lambda: [inf, 0])
        d[s] = [0, 1]
        q = []
        heapq.heappush(q, (0, s))
        v = collections.defaultdict(bool)
        while len(q):
            k, u = heapq.heappop(q)
            if v[u]:
                continue
            v[u] = True

            if u == t or k > kk:
                return d

            uc = d[u][1]
            dt = d1[u][0]

            for uv, ud in e[u]:
                if v[uv] or dt - ud != d1[uv][0]:
                    continue
                vd = k + ud
                if d[uv][0] > vd:
                    d[uv] = [vd, uc]
                    heapq.heappush(q, (vd, uv))
                elif d[uv][0] == vd:
                    d[uv][1] += uc

        return d


    d2 = search2(t,s)

    r = d1[t][1] ** 2 % mod
    for k in list(d1.keys()):
        t,c = d1[k]
        if t > kk:
            continue
        if t == kk:
            if d2[k][0] == t:
                r -= (c**2 * d2[k][1]**2) % mod
            continue

        for uv, ud in e[k]:
            if d2[uv][0] >= kk or t + ud + d2[uv][0] != rd:
                continue
            r -= (c**2 * d2[uv][1]**2) % mod

    return r % mod




print(main())

Submission status: Time Limit Exceeded

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    n,m = LI()
    s,t = LI()
    e = collections.defaultdict(list)
    for _ in range(m):
        u,v,d = LI()
        e[u].append((v,d))
        e[v].append((u,d))

    kk = inf
    def search(s,t):
        d = collections.defaultdict(lambda: [inf, 0])
        d[s] = [0, 1]
        q = []
        heapq.heappush(q, (0, s))
        v = collections.defaultdict(bool)
        while len(q):
            k, u = heapq.heappop(q)
            if v[u]:
                continue
            v[u] = True

            if u == t or k > kk:
                return d

            uc = d[u][1]

            for uv, ud in e[u]:
                if v[uv]:
                    continue
                vd = k + ud
                if d[uv][0] > vd:
                    d[uv] = [vd, uc]
                    heapq.heappush(q, (vd, uv))
                elif d[uv][0] == vd:
                    d[uv][1] += uc

        return d

    d1 = search(s,t)
    rd = d1[t][0]
    kk = rd / 2.0
    d2 = search(t,s)

    r = d1[t][1] ** 2 % mod
    for k in list(d1.keys()):
        t,c = d1[k]
        if t > kk:
            continue
        if t == kk:
            if d2[k][0] == t:
                r -= (c**2 * d2[k][1]**2) % mod
            continue

        for uv, ud in e[k]:
            if d2[uv][0] >= kk or t + ud + d2[uv][0] != rd:
                continue
            r -= (c**2 * d2[uv][1]**2) % mod

    return r % mod




print(main())

Submission status: Time Limit Exceeded

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    n,m = LI()
    s,t = LI()
    e = collections.defaultdict(list)
    for _ in range(m):
        u,v,d = LI()
        e[u].append((v,d))
        e[v].append((u,d))

    def search(s,t):
        d = collections.defaultdict(lambda: [inf, 0])
        d[s] = [0, 1]
        q = []
        heapq.heappush(q, (0, s))
        v = collections.defaultdict(bool)
        while len(q):
            k, u = heapq.heappop(q)
            if v[u]:
                continue
            v[u] = True

            if u == t:
                return d

            uc = d[u][1]

            for uv, ud in e[u]:
                if v[uv]:
                    continue
                vd = k + ud
                if d[uv][0] > vd:
                    d[uv] = [vd, uc]
                    heapq.heappush(q, (vd, uv))
                elif d[uv][0] == vd:
                    d[uv][1] += uc

        return d

    d1 = search(s,t)
    d2 = search(t,s)
    rd = d1[t][0]
    kk = rd / 2.0

    r = d1[t][1] ** 2 % mod
    for k in list(d1.keys()):
        t,c = d1[k]
        if t > kk:
            continue
        if t == kk:
            if d2[k][0] == t:
                r -= (c**2 * d2[k][1]**2) % mod
            continue

        for uv, ud in e[k]:
            if d2[uv][0] >= kk or t + ud + d2[uv][0] != rd:
                continue
            r -= (c**2 * d2[uv][1]**2) % mod

    return r % mod




print(main())