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: Wrong Answer

Solution code:
import sys
readline = sys.stdin.readline

from heapq import heappop as hpp, heappush as hp
def dijkstra(N, s, Edge):
    geta = 32
    inf = 1<<geta
    dist = [inf] * N
    Q = [s]
    decided = set()
    for _ in range(N):
        while True:
            k = hpp(Q)
            dn = k>>geta
            vn = k^(dn<<geta)
            if vn not in decided:
                decided.add(vn)
                dist[vn] = dn
                break
        for vf, df in Edge[vn]:
            if vf not in decided:
                hp(Q, ((dn + df)<<geta)+vf)
    return dist

MOD = 10**9+7
N, M = map(int, readline().split())

S, T = map(int, readline().split())
S -= 1
T -= 1

Edge = [[] for _ in range(N)]
for _ in range(M):
    u, v, d = map(int, readline().split())
    u -= 1
    v -= 1
    Edge[u].append((v, d))
    Edge[v].append((u, d))


dists = dijkstra(N, S, Edge)
distt = dijkstra(N, T, Edge)
st = dists[T]

onpath = [i for i in range(N) if dists[i] + distt[i] == st]
oset = set(onpath)
Ls = sorted(onpath, key = lambda x: dists[x])
Lt = sorted(onpath, key = lambda x: distt[x])

dps = [0]*N
dpt = [0]*N
dps[S] = 1
dpt[T] = 1
for v in Ls[:-1]:
    for vf, _ in Edge[v]:
        if vf in oset and dists[v] < dists[vf]:
            dps[vf] = (dps[vf] + dps[v])%MOD
for v in Lt[:-1]:
    for vf, _ in Edge[v]:
        if vf in oset and distt[v] < distt[vf]:
            dpt[vf] = (dpt[vf] + dpt[v])%MOD

ans = dps[T]**2%MOD
for i in onpath:
    if dists[i] == distt[i]:
        ans = (ans - dps[i]**2*dpt[i]**2)%MOD
    
    for vf, cost in Edge[i]:
        if dists[i] + cost + distt[vf] == st:
            if dists[i] < st//2 and distt[vf] < st//2:
                ans = (ans - dps[i]**2*dpt[vf]**2)%MOD
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
readline = sys.stdin.readline

from heapq import heappop as hpp, heappush as hp
def dijkstra(N, s, Edge):
    geta = 32
    inf = 1<<geta
    dist = [inf] * N
    Q = [s]
    decided = set()
    for _ in range(N):
        while True:
            k = hpp(Q)
            dn = k>>geta
            vn = k^(dn<<geta)
            if vn not in decided:
                decided.add(vn)
                dist[vn] = dn
                break
        for vf, df in Edge[vn]:
            if vf not in decided:
                hp(Q, ((dn + df)<<geta)+vf)
    return dist

MOD = 10**9+7
N, M = map(int, readline().split())

S, T = map(int, readline().split())
S -= 1
T -= 1

Edge = [[] for _ in range(N)]
for _ in range(M):
    u, v, d = map(int, readline().split())
    u -= 1
    v -=  1
    Edge[u].append((v, d))
    Edge[v].append((u, d))


dists = dijkstra(N, S, Edge)
distt = dijkstra(N, T, Edge)
st = dists[T]

onpath = [i for i in range(N) if dists[i] + distt[i] == st]
oset = set(onpath)
Ls = sorted(onpath, key = lambda x: dists[x])
Lt = sorted(onpath, key = lambda x: distt[x])

dps = [0]*N
dpt = [0]*N
dps[S] = 1
dpt[T] = 1
for v in Ls[:-1]:
    for vf, _ in Edge[v]:
        if vf in oset and dists[v] < dists[vf]:
            dps[vf] = (dps[vf] + dps[v])%MOD
for v in Lt[:-1]:
    for vf, _ in Edge[v]:
        if vf in oset and distt[v] < distt[vf]:
            dpt[vf] = (dpt[vf] + dpt[v])%MOD

ans = dps[T]**2%MOD
for i in onpath:
    if dists[i] == distt[i]:
        ans = (ans - dps[i]*dpt[i])%MOD
    
    for vf, _ in Edge[i]:
        if vf in oset:
            if dists[i] < st//2 and distt[vf] < st//2:
                ans = (ans - dps[i]*dpt[vf])%MOD
print(ans)

Submission status: Wrong Answer

Solution code:
from heapq import heappop, heappush
MOD = 10**9+7
N,M= map(int,input().split(' '))
S,T= map(int,input().split(' '))
from collections import defaultdict
g = defaultdict(list)

for i in [0]*M:
    u,v,d = list(map(int,input().split(' ')))
    g[u].append((v,d))
    g[v].append((u,d))

def dj(graph,s):
    inf = 10**20
    d = [inf]*(N+1)
    d[s] = 0
    num = [0]*(N+1)
    num[s] = 1
    que = []
    heappush(que,(s,0))
    while que:
        s,dist = heappop(que)
        if d[s] < dist:
            continue
        for t,c in g[s]:
            if d[t] > d[s] + c:
                d[t] = d[s] + c
                num[t] = num[s]
                heappush(que,(t,d[t]))
            elif d[t] == d[s] + c:
                num[t] += num[s]
                num[t] = num[t] % MOD
    return d,num

Sd,Snum = dj(g,S)
Td,Tnum = dj(g,T)
ans = (Snum[T] * Tnum[S]) % MOD
for v in range(1,N+1):
    if Sd[v] == Td[v]:
        ans -= Snum[v] * Tnum[v] * Snum[v] * Tnum[v]
        ans = ans % MOD
    for t, c in g[v]:
        # S->v->t->T
        if Sd[t] == Sd[v] + c and Td[v] == Td[t] + c:
            if abs(Sd[v] - Td[t]) < c:
                ans -= Snum[v] * Tnum[t] * Tnum[t] * Snum[v]
                ans = ans % MOD

print(ans)

Submission status: Wrong Answer

Solution code:
from heapq import heappop, heappush
MOD = 10**9+7
N,M= map(int,input().split(' '))
S,T= map(int,input().split(' '))
from collections import defaultdict
g = defaultdict(list)

for i in [0]*M:
    u,v,d = list(map(int,input().split(' ')))
    g[u].append((v,d))
    g[v].append((u,d))

def dj(graph,s):
    inf = 10**20
    d = [inf]*(N+1)
    d[s] = 0
    num = [0]*(N+1)
    num[s] = 1
    que = []
    heappush(que,(s,0))
    while que:
        s,dist = heappop(que)
        if d[s] < dist:
            continue
        for t,c in g[s]:
            if d[t] > d[s] + c:
                d[t] = d[s] + c
                num[t] = num[s]
                heappush(que,(t,d[t]))
            elif d[t] == d[s] + c:
                num[t] += num[s]
                num[t] = num[t] % MOD
    return d,num

Sd,Snum = dj(g,S)
Td,Tnum = dj(g,T)
ans = Snum[T] * Tnum[S]
for v in range(1,N+1):
    if Sd[v] == Td[v]:
        ans -= Snum[v] * Tnum[v] * Snum[v] * Tnum[v]
    for t, c in g[v]:
        # S->v->t->T
        if Sd[t] == Sd[v] + c and Td[v] == Td[t] + c:
            if abs(Sd[v] - Td[t]) < c:
                ans -= Snum[v] * Tnum[t] * Tnum[t] * Snum[v]

print(ans)

Submission status: Wrong Answer

Solution code:
# 入力を高速に
import sys
input = sys.stdin.readline
from heapq import heappop, heappush
mod = 10**9+7
INF = 1<<60
N, M = map(int, input().split())
S, T = map(int, input().split())
E = [[] for _ in range(N+1)]
edges = []
for _ in range(M):
    u, v, d = map(int, input().split())
    E[u].append((v, d))
    E[v].append((u, d))
    edges.append((u, v, d))

def dijksrtra(start):
    Dist = [INF] * (N+1)
    Dist[start] = 0
    q = [(0, start)]
    while q:
        dist, v = heappop(q)
        if Dist[v] != dist:
            continue
        for u, d in E[v]:
            new_dist = dist + d
            if Dist[u] > new_dist:
                Dist[u] = new_dist
                heappush(q, (new_dist, u))
    return Dist

Dist_S = dijksrtra(S)
Dist_T = dijksrtra(T)

Submission status: Wrong Answer

Solution code:
# INF を変える
from heapq import heappop, heappush
mod = 10**9+7
INF = 1<<60
N, M = map(int, input().split())
S, T = map(int, input().split())
E = [[] for _ in range(N+1)]
edges = []
for _ in range(M):
    u, v, d = map(int, input().split())
    E[u].append((v, d))
    E[v].append((u, d))
    edges.append((u, v, d))

def dijksrtra(start):
    Dist = [INF] * (N+1)
    Dist[start] = 0
    q = [(0, start)]
    while q:
        dist, v = heappop(q)
        if Dist[v] != dist:
            continue
        for u, d in E[v]:
            new_dist = dist + d
            if Dist[u] > new_dist:
                Dist[u] = new_dist
                heappush(q, (new_dist, u))
    return Dist

Dist_S = dijksrtra(S)
Dist_T = dijksrtra(T)

Submission status: Wrong Answer

Solution code:
# ダイクストラの速度確認
from heapq import heappop, heappush
mod = 10**9+7
N, M = map(int, input().split())
S, T = map(int, input().split())
E = [[] for _ in range(N+1)]
edges = []
for _ in range(M):
    u, v, d = map(int, input().split())
    E[u].append((v, d))
    E[v].append((u, d))
    edges.append((u, v, d))

def dijksrtra(start):
    Dist = [float("inf")] * (N+1)
    Dist[start] = 0
    q = [(0, start)]
    while q:
        dist, v = heappop(q)
        if Dist[v] != dist:
            continue
        for u, d in E[v]:
            new_dist = dist + d
            if Dist[u] > new_dist:
                Dist[u] = new_dist
                heappush(q, (new_dist, u))
    return Dist

Dist_S = dijksrtra(S)
Dist_T = dijksrtra(T)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from functools import reduce


sys.setrecursionlimit(2147483647)
INF = 10 ** 15
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


n, m = LI()
s, t = LI()
G = [[] for _ in range(n)]
for i in range(m):
    a, b, c = LI()
    G[a - 1] += [(b - 1, c)]
    G[b - 1] += [(a - 1, c)]


def dijkstra(graph, start=0):
    route_cnt = [start] * n
    route_cnt[start] = 1
    que = [(0, start)]
    dist = [INF] * n
    dist[start] = 0
    while que:
        min_dist, u = heappop(que)
        if min_dist > dist[u]:
            continue
        for v, c in graph[u]:
            if dist[u] + c < dist[v]:
                dist[v] = dist[u] + c
                route_cnt[v] = route_cnt[u]
                heappush(que, (dist[u] + c  , v))
            elif dist[u] + c == dist[v]:
                route_cnt[v] = (route_cnt[v] + route_cnt[u]) % mod
    return route_cnt, dist


route_cnt, dist = dijkstra(G, s - 1)
r_route_cnt, r_dist = dijkstra(G, t - 1)
total_dist = dist[t - 1]
ret = 0
for i in range(n):
    if dist[i] == r_dist[i]:
        ret = (ret + route_cnt[i] * r_route_cnt[i] % mod) ** 2 % mod


for u in range(n):
    for v, c in G[u]:
        if dist[u] < total_dist / 2 < dist[v] and dist[u] + c + r_dist[v] == total_dist:
            ret = (ret + route_cnt[u] ** 2 % mod * r_route_cnt[v] ** 2 % mod) % mod


print((route_cnt[t - 1] ** 2 - ret) % mod)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from functools import reduce


sys.setrecursionlimit(2147483647)
INF = 10 ** 20
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


n, m = LI()
s, t = LI()
G = [[] for _ in range(n)]
for i in range(m):
    a, b, c = LI()
    G[a - 1] += [(b - 1, c)]
    G[b - 1] += [(a - 1, c)]


def dijkstra(graph, start=0):
    route_cnt = [start] * n
    route_cnt[start] = 1
    que = [(0, start)]
    dist = [INF] * n
    dist[start] = 0
    while que:
        min_dist, u = heappop(que)
        if min_dist > dist[u]:
            continue
        for v, c in graph[u]:
            if dist[u] + c < dist[v]:
                dist[v] = dist[u] + c
                route_cnt[v] = route_cnt[u]
                heappush(que, (dist[u] + c  , v))
            elif dist[u] + c == dist[v]:
                route_cnt[v] = (route_cnt[v] + route_cnt[u]) % mod
    return route_cnt, dist


route_cnt, dist = dijkstra(G, s - 1)
r_route_cnt, r_dist = dijkstra(G, t - 1)
total_dist = dist[t - 1]
ret = 0
for i in range(n):
    if dist[i] == total_dist / 2 and r_dist[i] == total_dist / 2:
        ret = (ret + route_cnt[i] * r_route_cnt[i] % mod) ** 2 % mod


for u in range(n):
    for v, c in G[u]:
        if dist[u] < total_dist / 2 < dist[v] and dist[u] + c + r_dist[v] == total_dist:
            ret = (ret + route_cnt[u] ** 2 % mod * r_route_cnt[v] ** 2 % mod) % mod


print((route_cnt[t - 1] ** 2 - ret) % mod)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from functools import reduce


sys.setrecursionlimit(2147483647)
INF = 10 ** 20
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


n, m = LI()
s, t = LI()
G = [[] for _ in range(n)]
for i in range(m):
    a, b, c = LI()
    G[a - 1] += [(b - 1, c)]
    G[b - 1] += [(a - 1, c)]


def dijkstra(graph, start=0):
    route_cnt = [start] * n
    route_cnt[start] = 1
    que = [(0, start)]
    dist = [INF] * n
    dist[start] = 0
    while que:
        min_dist, u = heappop(que)
        if min_dist > dist[u]:
            continue
        for v, c in graph[u]:
            if dist[u] + c < dist[v]:
                dist[v] = dist[u] + c
                route_cnt[v] = route_cnt[u]
                heappush(que, (dist[u] + c  , v))
            elif dist[u] + c == dist[v]:
                route_cnt[v] = (route_cnt[v] + route_cnt[u]) % mod
    return route_cnt, dist


route_cnt, dist = dijkstra(G, s - 1)
r_route_cnt, r_dist = dijkstra(G, t - 1)
total_dist = dist[t - 1]
ret = 0
for i in range(n):
    if dist[i] == total_dist / 2 and r_dist[i] == total_dist / 2:
        ret = (ret + route_cnt[i] * r_route_cnt[i] % mod) ** 2 % mod


for u in range(n):
    for v, c in G[u]:
        if dist[u] < total_dist / 2 < dist[v] and dist[u] + c + r_dist[v] == total_dist:
            ret = (ret + route_cnt[u] ** 2 % mod * r_route_cnt[v] ** 2 % mod) % mod


print(route_cnt[t - 1] ** 2 - ret)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from functools import reduce


sys.setrecursionlimit(2147483647)
INF = 10 ** 20
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


n, m = LI()
s, t = LI()
G = [[] for _ in range(n)]
for i in range(m):
    a, b, c = LI()
    G[a - 1] += [(b - 1, c)]
    G[b - 1] += [(a - 1, c)]


def dijkstra(graph, start=0):
    route_cnt = [start] * n
    route_cnt[start] = 1
    que = [(0, start)]
    dist = [INF] * n
    dist[start] = 0
    while que:
        min_dist, u = heappop(que)
        if min_dist > dist[u]:
            continue
        for v, c in graph[u]:
            if dist[u] + c < dist[v]:
                dist[v] = dist[u] + c
                route_cnt[v] = route_cnt[u]
                heappush(que, (dist[u] + c, v))
            elif dist[u] + c == dist[v]:
                route_cnt[v] = (route_cnt[v] + route_cnt[u]) % mod
    return route_cnt, dist


route_cnt, dist = dijkstra(G, s - 1)
r_route_cnt, r_dist = dijkstra(G, t - 1)
total_dist = dist[t - 1]
ret = 0
for i in range(n):
    if dist[i] == total_dist / 2 and r_dist[i] == total_dist / 2:
        ret = (ret + route_cnt[i] * r_route_cnt[i] % mod) % mod




for u in range(n):
    for v, c in G[u]:
        if u > v:
            continue
        if dist[u] < total_dist/2 < dist[v] and dist[u] + c + r_dist[v] == total_dist:
            ret = (ret + route_cnt[u] * r_route_cnt[v] % mod) % mod
        elif dist[v] < total_dist/2 < dist[u] and dist[v] + c + r_dist[u] == total_dist:
            ret = (ret + route_cnt[v] * r_route_cnt[u] % mod) % mod


print(route_cnt[t - 1] ** 2 - ret)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)
import numpy as np
from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import dijkstra

"""
とりあえず何通りあるかを数える：各頂点を通る時点での場合の数
最後に衝突する方法を除外する：頂点での衝突、辺での衝突
辺での衝突：距離を見れば分かる
"""

MOD = 10 ** 9 + 7
eps = 1e-9

N,M = map(int,input().split())
S,T = map(int,input().split())
UVD = np.array([input().split() for _ in range(M)], dtype=np.int64)
U,V,D = UVD.T
dist_S, dist_T = dijkstra(csr_matrix((D,(U,V)), (N+1,N+1)), indices = [S,T], directed = False)

L = dist_S[T]

graph_ST = [[] for _ in range(N+1)]
select = np.abs(dist_S[U] + dist_T[V] + D - L) < eps
for u,v,_ in UVD[select]:
    graph_ST[u].append(v)
select = np.abs(dist_S[V] + dist_T[U] + D - L) < eps
for u,v,_ in UVD[select]:
    graph_ST[v].append(u)

graph_TS = [[] for _ in range(N+1)]
select = np.abs(dist_T[U] + dist_S[V] + D - L) < eps
for u,v,_ in UVD[select]:
    graph_TS[u].append(v)
select = np.abs(dist_T[V] + dist_S[U] + D - L) < eps
for u,v,_ in UVD[select]:
    graph_TS[v].append(u)

graph_TS

def DAG_cnt_path(x,graph,cnt_path):
    if x not in cnt_path:
        cnt_path[x] = sum(DAG_cnt_path(y,graph,cnt_path) for y in graph[x]) % MOD
    return cnt_path[x]

cnt_path_S = {S:1}
DAG_cnt_path(T,graph_TS,cnt_path_S)
cnt_path_T = {T:1}
DAG_cnt_path(S,graph_ST,cnt_path_T)

answer = cnt_path_S[T] * cnt_path_T[S] % MOD
# 頂点での衝突
for x in range(1,N+1):
    if abs(2 * dist_S[x] - L) < eps:
        answer -= cnt_path_S[x] * cnt_path_T[x] % MOD
# 辺での衝突。S側がx、T側がy
for u in range(N+1):
    for v in graph_ST[u]:
        du = dist_S[u]
        dv = dist_T[v]
        if (du < L/2) and (dv < L/2):
            answer -= cnt_path_S[u] * cnt_path_T[v]

answer %= MOD
print(answer)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
from heapq import heappop, heappush

"""
とりあえず何通りあるかを数える：各頂点を通る時点での場合の数
最後に衝突する方法を除外する：頂点での衝突、辺での衝突
辺での衝突：距離を見れば分かる
"""

MOD = 10 ** 9 + 7

N,M = map(int,input().split())
S,T = map(int,input().split())
graph = [[] for _ in range(N+1)]
for _ in range(M):
    u,v,d = map(int,input().split())
    graph[u].append((v,d))
    graph[v].append((u,d))

def dijkstra(S):
    # 距離、パスの個数を返す
    INF = 10 ** 15
    dist = [INF] * (N+1)
    cnt_path = [0] * (N+1)
    cnt_path[0] = 1
    q = [(0,S,0)] # 距離、場所、直前
    while q:
        d,x,pred = heappop(q)
        if dist[x] < d:
            continue
        if dist[x] == d:
            cnt_path[x] += cnt_path[pred]
            cnt_path[x] %= MOD
            continue
        dist[x] = d
        cnt_path[x] += cnt_path[pred]
        cnt_path[x] %= MOD
        for y,dy in graph[x]:
            if d + dy > dist[y]:
                continue
            heappush(q,(d+dy,y,x))
    return dist, cnt_path

dist_S, cnt_path_S = dijkstra(S)
dist_T, cnt_path_T = dijkstra(T)

D = dist_S[T]
# 最短路の組
answer = cnt_path_S[T] * cnt_path_T[S] % MOD
# 頂点での衝突
for x in range(1,N+1):
    if dist_S[x] == dist_T[x]:
        answer -= cnt_path_S[x] * cnt_path_T[x] % MOD
# 辺での衝突。S側がx、T側がy
for x in range(1,N+1):
    for y,d in graph[x]:
        dx = dist_S[x]
        dy = dist_T[y]
        if dx + dy + d == D and dx + d > dy and dy + d > dx:
            answer -= cnt_path_S[x] * cnt_path_T[y] % MOD

answer %= MOD
print(answer)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
from heapq import heappop, heappush

"""
とりあえず何通りあるかを数える：各頂点を通る時点での場合の数
最後に衝突する方法を除外する：頂点での衝突、辺での衝突
辺での衝突：距離を見れば分かる
"""

N,M = map(int,input().split())
S,T = map(int,input().split())
graph = [[] for _ in range(N+1)]
for _ in range(M):
    u,v,d = map(int,input().split())
    graph[u].append((v,d))
    graph[v].append((u,d))

def dijkstra(S):
    # 距離、パスの個数を返す
    INF = 10 ** 15
    dist = [INF] * (N+1)
    cnt_path = [0] * (N+1)
    cnt_path[0] = 1
    q = [(0,S,0)] # 距離、場所、直前
    while q:
        d,x,pred = heappop(q)
        if dist[x] < d:
            continue
        if dist[x] == d:
            cnt_path[x] += cnt_path[pred]
            continue
        dist[x] = d
        cnt_path[x] += cnt_path[pred]
        for y,dy in graph[x]:
            if d + dy > dist[y]:
                continue
            heappush(q,(d+dy,y,x))
    return dist, cnt_path

dist_S, cnt_path_S = dijkstra(S)
dist_T, cnt_path_T = dijkstra(T)

D = dist_S[T]
# 最短路の組
answer = cnt_path_S[T] * cnt_path_T[S]
# 頂点での衝突
for x in range(1,N+1):
    if dist_S[x] == dist_T[x]:
        answer -= cnt_path_S[x] * cnt_path_T[x]
# 辺での衝突。S側がx、T側がy
for x in range(1,N+1):
    for y,d in graph[x]:
        dx = dist_S[x]
        dy = dist_T[y]
        if dx + dy + d == D and dx + d > dy and dy + d > dx:
            answer -= cnt_path_S[x] * cnt_path_T[y]

print(answer)

Submission status: Wrong Answer

Solution code:
# 参考: https://qiita.com/y-tsutsu/items/aa7e8e809d6ac167d6a1

from heapq import heappush, heappop

# 多次元配列
# dp = [[0] * 3 for _ in range(5)


INF = 1 << 28
MOD = int(1e9) + 7


def dijkstra(graph, s: int, t: int):
    n = len(graph)
    dist = [INF for _ in range(n)]
    cnts = [0 for _ in range(n)]

    pq = []
    heappush(pq, (0, s))
    dist[s] = 0
    cnts[s] = 1

    while pq:
        (cost, cur) = heappop(pq)

        if dist[cur] != cost:
            continue
        
        if cur == t:
            return dist, cnts

        for next_idx, next_cost in graph[cur]:
            if dist[next_idx] > cost + next_cost:
                heappush(pq, (cost + next_cost, next_idx))
                dist[next_idx] = cost + next_cost
                cnts[next_idx] = cnts[cur]
            elif dist[next_idx] == cost + next_cost:
                cnts[next_idx] = (cnts[next_idx] + cnts[cur]) % MOD

    return dist, cnts


# 入力を受け取る
n, m = map(int, input().split())

s, t = map(lambda i: int(i) - 1, input().split())

g = [[] for _ in range(n)]

es = [list(map(int, input().split())) for _ in range(m)]

for e in es:
    u, v, d = e
    g[u - 1].append((v - 1, d))
    g[v - 1].append((u - 1, d))

dst1, cnt1 = dijkstra(g, s, t)
dst2, cnt2 = dijkstra(g, t, s)

ret = cnt1[t] * cnt1[t] % MOD

for i in range(n):
    if 2 * dst1[i] == dst1[t] == 2 * dst2[i]:
        sub = cnt1[i] * cnt2[i] % MOD
        sub = sub ** 2 % MOD
        ret = (ret + MOD - sub) % MOD

sum_sub = 0

for e in es:
    u, v, d = e
    u -= 1
    v -= 1

    if cnt1[u] > cnt1[v]:
        v, u = u, v

    if 2 * dst1[u] < dst1[t] < 2 * dst1[v] and dst1[u] + d + dst2[v] == dst1[t]:
        sub = cnt1[u] * cnt2[v] % MOD
        sub = sub ** 2 % MOD
        ret = (ret + MOD - sub) % MOD

print(ret)

Submission status: Wrong Answer

Solution code:
# 参考: https://qiita.com/y-tsutsu/items/aa7e8e809d6ac167d6a1

from heapq import heappush, heappop

# 多次元配列
# dp = [[0] * 3 for _ in range(5)


INF = 1 << 28
MOD = int(1e9) + 7


def dijkstra(graph, s: int, t: int):
    n = len(graph)
    dist = [INF for _ in range(n)]
    cnts = [0 for _ in range(n)]

    pq = []
    heappush(pq, (0, s))
    dist[s] = 0
    cnts[s] = 1

    while pq:
        (cost, cur) = heappop(pq)

        if dist[cur] != cost:
            continue

        if cur == t:
            return dist, cnts

        for next_idx, next_cost in graph[cur]:
            if dist[next_idx] > cost + next_cost:
                heappush(pq, (cost + next_cost, next_idx))
                dist[next_idx] = cost + next_cost
                cnts[next_idx] = cnts[cur]
            elif dist[next_idx] == cost + next_cost:
                cnts[next_idx] = (cnts[next_idx] + cnts[cur]) % MOD

    return dist, cnts


# 入力を受け取る
n, m = map(int, input().split())

s, t = map(lambda i: int(i) - 1, input().split())

g = [[] for _ in range(n)]

es = [list(map(int, input().split())) for _ in range(m)]

for e in es:
    u, v, d = e
    g[u - 1].append((v - 1, d))
    g[v - 1].append((u - 1, d))

dst1, cnt1 = dijkstra(g, s, t)
dst2, cnt2 = dijkstra(g, t, s)

ret = cnt1[t] * cnt1[t] % MOD

for i in range(n):
    if 2 * dst1[i] == dst1[t]:
        sub = cnt1[i] * cnt2[i] % MOD
        sub = sub ** 2 % MOD
        ret = (ret + MOD - sub) % MOD

sum_sub = 0

for e in es:
    u, v, d = e
    u -= 1
    v -= 1

    if cnt1[u] > cnt1[v]:
        v, u = u, v

    if 2 * dst1[u] < dst1[t] < 2 * dst1[v] and dst1[u] + d + dst2[v] == dst1[t]:
        sub = cnt1[u] * cnt2[v] % MOD
        sub = sub ** 2 % MOD
        ret = (ret + MOD - sub) % MOD

print(ret)

Submission status: Wrong Answer

Solution code:
# 参考: https://qiita.com/y-tsutsu/items/aa7e8e809d6ac167d6a1

from heapq import heappush, heappop


# 多次元配列
# dp = [[0] * 3 for _ in range(5)


class Edge:
    def __init__(self, to: int, cost: int):
        self._to = to
        self._cost = cost

    @property
    def to(self):
        return self._to

    @property
    def cost(self):
        return self._cost


class Edges(list):
    def __init__(self):
        list.__init__(self)


INF = 1 << 28
MOD = int(1e9) + 7


def dijkstra(graph, s: int, t: int):
    n = len(graph)
    dist = [INF for _ in range(n)]
    cnts = [0 for _ in range(n)]

    pq = []
    heappush(pq, (0, s))
    dist[s] = 0
    cnts[s] = 1

    while pq:
        (cost, cur) = heappop(pq)

        if dist[cur] != cost:
            continue

        if cur == t:
            return dist, cnts

        for next in graph[cur]:
            if dist[next.to] > cost + next.cost:
                heappush(pq, (cost + next.cost, next.to))
                dist[next.to] = cost + next.cost
                cnts[next.to] = cnts[cur]
            elif dist[next.to] == cost + next.cost:
                cnts[next.to] = (cnts[next.to] + cnts[cur]) % MOD

    return dist, cnts


# 入力を受け取る
n, m = map(int, input().split())

s, t = map(lambda i: int(i) - 1, input().split())

g = [Edges() for _ in range(n)]

es = [list(map(int, input().split())) for _ in range(m)]

for e in es:
    u, v, d = e
    g[u - 1].append(Edge(v - 1, d))
    g[v - 1].append(Edge(u - 1, d))

dst1, cnt1 = dijkstra(g, s, t)
dst2, cnt2 = dijkstra(g, t, s)

ret = cnt1[t] * cnt1[t] % MOD

for i in range(n):
    if 2 * dst1[i] == dst1[t]:
        sub = cnt1[i] * cnt2[i] % MOD
        sub = sub ** 2 % MOD
        ret = (ret + MOD - sub) % MOD

sum_sub = 0

for e in es:
    u, v, d = e
    u -= 1
    v -= 1

    if cnt1[u] > cnt1[v]:
        v, u = u, v

    if 2 * dst1[u] < dst1[t] < 2 * dst1[v] and dst1[u] + d + dst2[v] == dst1[t]:
        sub = cnt1[u] * cnt2[v] % MOD
        sub = sub ** 2 % MOD
        ret = (ret + MOD - sub) % MOD

print(ret)

Submission status: Wrong Answer

Solution code:
# 参考: https://qiita.com/y-tsutsu/items/aa7e8e809d6ac167d6a1

from heapq import heappush, heappop


# 多次元配列
# dp = [[0] * 3 for _ in range(5)


class Edge:
    def __init__(self, to: int, cost: int):
        self._to = to
        self._cost = cost

    @property
    def to(self):
        return self._to

    @property
    def cost(self):
        return self._cost


class Edges(list):
    def __init__(self):
        list.__init__(self)


INF = 1 << 28
MOD = int(1e9) + 7


def dijkstra(graph, s: int):
    n = len(graph)
    dist = [INF for _ in range(n)]
    cnts = [0 for _ in range(n)]

    pq = []
    heappush(pq, (0, s))
    dist[s] = 0
    cnts[s] = 1

    while pq:
        (cost, cur) = heappop(pq)

        if dist[cur] != cost:
            continue

        for next in graph[cur]:
            if dist[next.to] > cost + next.cost:
                heappush(pq, (cost + next.cost, next.to))
                dist[next.to] = cost + next.cost
                cnts[next.to] = cnts[cur]
            elif dist[next.to] == cost + next.cost:
                cnts[next.to] = (cnts[next.to] + cnts[cur]) % MOD

    return dist, cnts


# 入力を受け取る
n, m = map(int, input().split())

s, t = map(lambda i: int(i) - 1, input().split())

g = [Edges() for _ in range(n)]

es = [list(map(int, input().split())) for _ in range(m)]

for e in es:
    u, v, d = e
    g[u - 1].append(Edge(v - 1, d))
    g[v - 1].append(Edge(u - 1, d))

dst1, cnt1 = dijkstra(g, s)
dst2, cnt2 = dijkstra(g, t)

ret = cnt1[t] * cnt1[t] % MOD

for i in range(n):
    if 2 * dst1[i] == dst1[t]:
        sub = cnt1[i] * cnt2[i] % MOD
        sub = sub ** 2 % MOD
        ret = (ret + MOD - sub) % MOD

sum_sub = 0

for e in es:
    u, v, d = e
    u -= 1
    v -= 1

    if cnt1[u] > cnt2[v]:
        v, u = u, v

    if 2 * dst1[u] < dst1[t] < 2 * dst1[v] and dst1[u] + d + dst2[v] == dst1[t]:
        sub = cnt1[u] * cnt2[v] % MOD
        sub = sub ** 2 % MOD
        ret = (ret + MOD - sub) % MOD

print(ret)