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
sys.setrecursionlimit(10**9)
from heapq import *
mod=10**9+7
N,M=map(int,input().split())
S,T=map(int,input().split())
S,T=S-1,T-1
G=[[] for i in range(N)]
D1=[0]*N
D2=[0]*N
u,v,d=0,0,0
for i in range(M):
  u,v,d=map(int,input().split())
  u,v=u-1,v-1
  G[u].append((v,d))
  G[v].append((u,d))
INF=10**17
def ijk(s,D):
  global INF,G
  for i in range(len(G)):
    D[i]=INF
  D[s]=0
  Q=[]
  heapify(Q)
  heappush(Q,(0,s))
  p,v,e=0,0,0
  while len(Q):
    p=heappop(Q)
    v=p[1]
    if D[v]<p[0]:
      continue
    for i in range(len(G[v])):
      e=G[v][i]
      if D[e[0]]>D[v]+e[1]:
        D[e[0]]=D[v]+e[1]
        heappush(Q,(D[e[0]],e[0]))

ijk(S,D1)
ijk(T,D2)
DP1=[-1]*N
DP2=[-1]*N
DP1[S]=1
DP2[T]=1
def f1(n):
  if DP1[n]!=-1:
    return DP1[n]
  DP1[n]=0
  for i in range(len(G[n])):
    if D1[n]==G[n][i][1]+D1[G[n][i][0]]:
      DP1[n]+=f1(G[n][i][0])
  DP1[n]%=mod
  return DP1[n]

def f2(n):
  if DP2[n]!=-1:
    return DP2[n]
  DP2[n]=0
  for i in range(len(G[n])):
    if D2[n]==G[n][i][1]+D2[G[n][i][0]]:
      DP2[n]+=f2(G[n][i][0])
  DP2[n]%=mod
  return DP2[n]

for i in range(N):
  f1(i)
  f2(i)
P=0
X=D1[T]/2
for i in range(N):
  if D1[i]==D2[i]:
    P+=DP1[i]*DP2[i]
  for j in range(len(G[i])):
    if ((D1[i]<X and D1[G[i][j][0]]>X)and(D2[i]>X and D2[G[i][j][0]]<X)):
      P+=DP1[i]*DP2[G[i][j][0]]
print((DP1[T]*DP2[S]-P)%mod)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
from bisect import bisect_left, bisect_right
import random
from itertools import permutations, accumulate, combinations
import sys
import string
from copy import deepcopy

INF = 10 ** 20


def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
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 = 10 ** 9 + 7

def dijkstra(s):
    hq=[(0,s)]
    dist=[INF]*n
    dp=[0]*n
    dp[s]=1
    dist[s]=0
    checked=[0]*n
    while hq:
        min_dist,u=heappop(hq)
        if checked[u]:
            continue
        checked[u]=1
        for v,c in G[u]:
            if dist[u]+c<dist[v]:
                dist[v]=dist[u]+c
                dp[v]=dp[u]
                heappush(hq,(dist[v],v))
            elif dist[u]+c==dist[v]:
                dp[v]+=dp[u]
                dp[v]%=mod
    return dp, dist

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

dp1,dist1=dijkstra(s-1)
dp2,dist2=dijkstra(t-1)
total=dist1[t-1]
ans=dp1[t-1]*dp2[s-1]%mod
for i in range(n):
    if dist1[i]==dist2[i]==total/2:
        ans-=dp1[i]*dp2[i]%mod

for j in range(n):
    for k, ci in G[j]:
        if dist1[j]+dist2[k]+ci==total and dist1[j]<total/2 and dist2[k]<total/2:
            ans-=dp1[j]*dp2[k]%mod

print(ans%mod)

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
mod = 10**9+7

# dijkstra
import heapq
def dijkstra(links, start,goal, n):
    inf = 10**20
    d = [inf] * (n)
    cnt = [0] * n
    d[start] = 0
    cnt[start] = 1
    # befs = [[] for _ in range(n+1)]
    hq = []
    for i in links[start]:
        heapq.heappush(hq, (i[0],i[1],start))
    while(hq):
        cost,i,bef = heapq.heappop(hq)
        if(cost > d[goal]):
            return d,cnt
        if( d[i] != inf):
            if(d[i] == cost):
                cnt[i] += cnt[bef]
                cnt[i] %= mod
                # befs[i].append(bef)
            continue
        d[i] = cost
        cnt[i] += cnt[bef]
        # befs[i].append(bef)
        for tmp in links[i]:
            cost_next, j = tmp
            if(d[j] == inf):
                heapq.heappush(hq, (cost+cost_next, j,i))
    return d,cnt

n,m = map(int,readline().split())
s,t = map(int,readline().split())
uvd = list(map(int,read().split()))

links = [[] for _ in range(n+1)]
it = iter(uvd)
for u,v,d in zip(it,it,it):
    links[u].append((d*2,v))
    links[v].append((d*2,u))

d_st,cnt_st = dijkstra(links, s,t, n+1)
d_ts,cnt_ts = dijkstra(links, t,s, n+1)

half = d_st[t]//2

collision = 0
for i in range(1,n+1):
    if(d_st[i] == half)&(d_ts[i]==half):
        tmp = (cnt_st[i] * cnt_ts[i])%mod
        collision += (tmp**2)%mod
        collision %= mod

it = iter(uvd)
for u,v,d in zip(it,it,it):
    for p1,p2 in zip([u,v],[v,u]):
        if(d_st[p1] < half < d_st[p2])&(d_ts[p1] > half > d_ts[p2])& \
            (d_st[p2] - d_st[p1] == d*2)&(d_ts[p1] - d_ts[p2] == d*2) :
                tmp = (cnt_st[p1] * cnt_ts[p2])%mod
                collision += (tmp**2)%mod
                collision %= mod

ans = (cnt_st[t]**2 - collision)%mod
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
mod = 10**9+7

# dijkstra
import heapq
def dijkstra(links, start,goal, n):
    inf = 10**20
    d = [inf] * (n)
    cnt = [0] * n
    d[start] = 0
    cnt[start] = 1
    # befs = [[] for _ in range(n+1)]
    hq = []
    for i in links[start]:
        heapq.heappush(hq, (i[0],i[1],start))
    while(hq):
        cost,i,bef = heapq.heappop(hq)
        if(cost > d[goal]):
            return d,cnt
        if( d[i] != inf):
            if(d[i] == cost):
                cnt[i] += cnt[bef]
                cnt[i] %= mod
                # befs[i].append(bef)
            continue
        d[i] = cost
        cnt[i] += cnt[bef]
        # befs[i].append(bef)
        for tmp in links[i]:
            cost_next, j = tmp
            if(d[j] == inf):
                heapq.heappush(hq, (cost+cost_next, j,i))
    return d,cnt

n,m = map(int,readline().split())
s,t = map(int,readline().split())
uvd = list(map(int,read().split()))

links = [[] for _ in range(n+1)]
it = iter(uvd)
for u,v,d in zip(it,it,it):
    links[u].append((d*2,v))
    links[v].append((d*2,u))

d_st,cnt_st = dijkstra(links, s,t, n+1)
d_ts,cnt_ts = dijkstra(links, t,s, n+1)

half = d_st[t]//2

way = 0
collision = 0
for i in range(1,n+1):
    if(d_st[i] == half)&(d_ts[i]==half):
        tmp = (cnt_st[i] * cnt_ts[i])%mod
        way += tmp
        way %= mod
        collision += tmp**2
        collision %= mod

it = iter(uvd)
for u,v,d in zip(it,it,it):
    for p1,p2 in zip([u,v],[v,u]):
        if(d_st[p1] < half < d_st[p2])&(d_ts[p1] > half > d_ts[p2])& \
            (d_st[p2] - d_st[p1] == d*2)&(d_ts[p1] - d_ts[p2] == d*2) :
                tmp = (cnt_st[p1] * cnt_ts[p2])%mod
                way += tmp
                way %= mod
                collision += tmp**2
                collision %= mod

ans = (way**2 - collision)%mod
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
mod = 10**9+7

# dijkstra
import heapq
def dijkstra(links, start,goal, n):
    inf = 10**20
    d = [inf] * (n)
    cnt = [0] * n
    d[start] = 0
    cnt[start] = 1
    hq = []
    for i in links[start]:
        heapq.heappush(hq, (i[0],i[1],start))
    while(hq):
        cost,i,bef = heapq.heappop(hq)
        if( d[i] != inf):
            if(d[i] == cost):
                cnt[i] += cnt[bef]
                cnt[i] %= mod
            continue
        d[i] = cost
        cnt[i] += cnt[bef]
        for tmp in links[i]:
            cost_next, j = tmp
            if(d[j] == inf):
                heapq.heappush(hq, (cost+cost_next, j,i))
    return d,cnt

n,m = map(int,readline().split())
s,t = map(int,readline().split())
uvd = list(map(int,read().split()))

links = [[] for _ in range(n+1)]
it = iter(uvd)
for u,v,d in zip(it,it,it):
    links[u].append((d*2,v))
    links[v].append((d*2,u))

d_st,cnt_st = dijkstra(links, s,t, n+1)

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
mod = 10**9+7

# dijkstra
import heapq
def dijkstra(links, start,goal, n):
    inf = 10**20
    d = [inf] * (n)
    cnt = [0] * n
    d[start] = 0
    cnt[start] = 1
    hq = []
    for i in links[start]:
        heapq.heappush(hq, (i[0],i[1],start))
    while(hq):
        cost,i,bef = heapq.heappop(hq)
        if( d[i] != inf):
            if(d[i] == cost):
                cnt[i] += cnt[bef]
                cnt[i] %= mod
            continue
        d[i] = cost
        cnt[i] += cnt[bef]
        for tmp in links[i]:
            cost_next, j = tmp
            if(d[j] == inf):
                heapq.heappush(hq, (cost+cost_next, j,i))
    return d,cnt

n,m = map(int,readline().split())
s,t = map(int,readline().split())
uvd = list(map(int,read().split()))

links = [[] for _ in range(n+1)]
it = iter(uvd)
for u,v,d in zip(it,it,it):
    links[u].append((d*2,v))
    links[v].append((d*2,u))

d_st,cnt_st = dijkstra(links, s,t, n+1)
d_ts,cnt_ts = dijkstra(links, t,s, n+1)

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
mod = 10**9+7

# dijkstra
import heapq
def dijkstra(links, start,goal, n):
    inf = 10**20
    d = [inf] * (n)
    cnt = [0] * n
    d[start] = 0
    cnt[start] = 1
    hq = []
    for i in links[start]:
        heapq.heappush(hq, (i[0],i[1],start))
    while(hq):
        cost,i,bef = heapq.heappop(hq)
        if( d[i] != inf):
            if(d[i] == cost):
                cnt[i] += cnt[bef]
                cnt[i] %= mod
            continue
        d[i] = cost
        cnt[i] += cnt[bef]
        for tmp in links[i]:
            cost_next, j = tmp
            if(d[j] == inf):
                heapq.heappush(hq, (cost+cost_next, j,i))
    return d,cnt

n,m = map(int,readline().split())
s,t = map(int,readline().split())
uvd = list(map(int,read().split()))

links = [[] for _ in range(n+1)]
it = iter(uvd)
for u,v,d in zip(it,it,it):
    links[u].append((d*2,v))
    links[v].append((d*2,u))

d_st,cnt_st = dijkstra(links, s,t, n+1)
d_ts,cnt_ts = dijkstra(links, t,s, n+1)

half = d_st[t]//2

way = 0
collision = 0
for i in range(1,n+1):
    if(d_st[i] == half)&(d_ts[i]==half):
        tmp = (cnt_st[i] * cnt_ts[i])%mod
        way += tmp
        way %= mod
        collision += tmp**2
        collision %= mod

it = iter(uvd)
for u,v,d in zip(it,it,it):
    for p1,p2 in zip([u,v],[v,u]):
        if(d_st[p1] < half < d_st[p2])&(d_ts[p1] > half > d_ts[p2])& \
            (d_st[p2] - d_st[p1] == d*2)&(d_ts[p1] - d_ts[p2] == d*2) :
                tmp = (cnt_st[p1] * cnt_ts[p2])%mod
                way += tmp
                way %= mod
                collision += tmp**2
                collision %= mod

ans = (way**2 - collision)%mod
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
mod = 10**9+7

# dijkstra
import heapq
def dijkstra(links, start,goal, n):
    inf = 10**10
    d = [inf] * (n)
    cnt = [0] * n
    d[start] = 0
    cnt[start] = 1
    hq = []
    for i in links[start]:
        heapq.heappush(hq, (i[0],i[1],start))
    while(hq):
        cost,i,bef = heapq.heappop(hq)
        if( d[i] != inf):
            if(d[i] == cost):
                cnt[i] += cnt[bef]
                cnt[i] %= mod
            continue
        d[i] = cost
        cnt[i] += cnt[bef]
        for tmp in links[i]:
            cost_next, j = tmp
            if(d[j] == inf):
                heapq.heappush(hq, (cost+cost_next, j,i))
    return d,cnt

n,m = map(int,readline().split())
s,t = map(int,readline().split())
uvd = list(map(int,read().split()))

links = [[] for _ in range(n+1)]
it = iter(uvd)
for u,v,d in zip(it,it,it):
    links[u].append((d*2,v))
    links[v].append((d*2,u))

d_st,cnt_st = dijkstra(links, s,t, n+1)
d_ts,cnt_ts = dijkstra(links, t,s, n+1)

half = d_st[t]//2

way = 0
collision = 0
for i in range(1,n+1):
    if(d_st[i] == half)&(d_ts[i]==half):
        tmp = cnt_st[i] * cnt_ts[i]
        way += tmp
        way %= mod
        collision += tmp**2
        collision %= mod

it = iter(uvd)
for u,v,d in zip(it,it,it):
    for p1,p2 in zip([u,v],[v,u]):
        if(d_st[p1] < half < d_st[p2])&(d_ts[p1] > half > d_ts[p2])& \
            (d_st[p2] - d_st[p1] == d*2)&(d_ts[p1] - d_ts[p2] == d*2) :
                tmp = cnt_st[p1] * cnt_ts[p2]
                way += tmp
                way %= mod
                collision += tmp**2
                collision %= mod

ans = (way**2 - collision)%mod
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
def MI(): return map(int,sys.stdin.readline().rstrip().split())

N,M = MI()
S,T = MI()
edges = [[] for _ in range(N+1)]
edges2 = []
for _ in range(M):
    U,V,D = MI()
    edges[U].append((V,D))
    edges[V].append((U,D))
    edges2.append((U,V,D))
    edges2.append((V,U,D))

mod = 10**9+7

from heapq import heappop,heappush

distS = [(10**18,0)]*(N+1)  # (最短距離,経路数)
distS[S] = (0,1)
qS = []
heappush(qS,(0,S))
vS = [False]*(N+1)
while qS:
    d,i = heappop(qS)
    if vS[i]:
        continue
    vS[i] = True
    for j,d in edges[i]:
        if vS[j]:
            continue
        if distS[j][0] > distS[i][0] + d:
            distS[j] = (distS[i][0] + d,distS[i][1])
            heappush(qS,(distS[i][0] + d,j))
        elif distS[j][0] == distS[i][0] + d:
            distS[j] = (distS[j][0],distS[j][1]+distS[i][1])
            heappush(qS,(distS[i][0] + d,j))

distT = [(10**18,0)]*(N+1)
distT[T] = (0,1)
qT = []
heappush(qT,(0,T))
vT = [False]*(N+1)
while qT:
    d,i = heappop(qT)
    if vT[i]:
        continue
    vT[i] = True
    for j,d in edges[i]:
        if vT[j]:
            continue
        if distT[j][0] > distT[i][0] + d:
            distT[j] = (distT[i][0] + d,distT[i][1])
            heappush(qT,(distT[i][0] + d,j))
        elif distT[j][0] == distT[i][0] + d:
            distT[j] = (distT[j][0],distT[j][1]+distT[i][1])
            heappush(qT,(distT[i][0] + d,j))

a,b = distS[T]  # SからTまでの最短距離,経路数
ans = b**2 % mod

# 頂点で出くわす場合
if a % 2 == 0:
    for i in range(1,N+1):
        if distS[i][0] == distT[i][0] == a//2:
            ans -= (distS[i][1]*distT[i][1])**2
            ans = mod

# 辺上で出くわす場合
for i in range(2*M):
    U,V,D = edges2[i]
    if distS[U][0] < (a+1)//2 and distT[V][0] < (a+1)//2 and a == distS[U][0] + D + distT[V][0]:
        ans -= (distS[U][1]*distT[V][1])**2
        ans %= mod

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
def MI(): return map(int,sys.stdin.readline().rstrip().split())

N,M = MI()
S,T = MI()
edges = [[] for _ in range(N+1)]
edges2 = []
for _ in range(M):
    U,V,D = MI()
    edges[U].append((V,D))
    edges[V].append((U,D))
    edges2.append((U,V,D))
    edges2.append((V,U,D))

mod = 10**9+7

from heapq import heappop,heappush

distS = [(10**18,0)]*(N+1)  # (最短距離,経路数)
distS[S] = (0,1)
qS = []
heappush(qS,(0,S))
vS = [False]*(N+1)
while qS:
    d,i = heappop(qS)
    if vS[i]:
        continue
    vS[i] = True
    for j,d in edges[i]:
        if vS[j]:
            continue
        if distS[j][0] > distS[i][0] + d:
            distS[j] = (distS[i][0] + d,distS[i][1])
            heappush(qS,(distS[i][0] + d,j))
        elif distS[j][0] == distS[i][0] + d:
            distS[j] = (distS[j][0],distS[j][1]+distS[i][1])
            heappush(qS,(distS[i][0] + d,j))

distT = [(10**18,0)]*(N+1)
distT[T] = (0,1)
qT = []
heappush(qT,(0,T))
vT = [False]*(N+1)
while qT:
    d,i = heappop(qT)
    if vT[i]:
        continue
    vT[i] = True
    for j,d in edges[i]:
        if vT[j]:
            continue
        if distT[j][0] > distT[i][0] + d:
            distT[j] = (distT[i][0] + d,distT[i][1])
            heappush(qT,(distT[i][0] + d,j))
        elif distT[j][0] == distT[i][0] + d:
            distT[j] = (distT[j][0],distT[j][1]+distT[i][1])
            heappush(qT,(distT[i][0] + d,j))

a,b = distS[T]  # SからTまでの最短距離,経路数
ans = b**2

# 頂点で出くわす場合
if a % 2 == 0:
    for i in range(1,N+1):
        if distS[i][0] == distT[i][0] == a//2:
            ans -= (distS[i][1]*distT[i][1])**2

# 辺上で出くわす場合
for i in range(2*M):
    U,V,D = edges2[i]
    if distS[U][0] < (a+1)//2 and distT[V][0] < (a+1)//2 and a == distS[U][0] + D + distT[V][0]:
        ans -= (distS[U][1]*distT[V][1])**2

print(ans)

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def MI1(): return map(int1, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

from heapq import *

def solve():
    dist = [inf] * n
    cnt = [0] * n
    hp = []
    heappush(hp, (0, s))
    dist[s] = 0
    cnt[s] = 1
    while hp:
        d, u = heappop(hp)
        if d > dist[t]: break
        if d > dist[u]: continue
        for v, c in to[u]:
            nd = d + c
            if nd == dist[v]: cnt[v] += cnt[u]
            elif nd < dist[v]:
                dist[v] = nd
                cnt[v] = cnt[u]
                heappush(hp, (nd, v))
    # print(dist)
    # print(cnt)

    distr = [inf] * n
    cntr = [0] * n
    hp = []
    heappush(hp, (0, t))
    distr[t] = 0
    cntr[t] = 1
    while hp:
        d, u = heappop(hp)
        if d * 2 > dist[t]: continue
        if d > distr[u]: continue
        for v, c in to[u]:
            nd = d + c
            if nd == distr[v]: cntr[v] += cntr[u]
            elif nd < distr[v]:
                distr[v] = nd
                cntr[v] = cntr[u]
                heappush(hp, (nd, v))
    # print(distr)
    # print(cntr)

    ans = pow(cnt[t], 2, md)
    dt = dist[t]
    for u in range(n):
        if dist[u] * 2 == dt:
            ans -= cnt[u] * cntr[u]
            ans %= md
    for u, v, c in uvd:
        for _ in range(2):
            if dist[u] + distr[v] + c == dt and dist[u] * 2 < dt and distr[v] * 2 < dt:
                ans -= cnt[u] * cntr[v]
                ans %= md
            u, v = v, u
    print(ans)

md = 10 ** 9 + 7
inf = 10 ** 16
n, m = MI()
s, t = MI1()
to = [[] for _ in range(n)]
uvd = []
for _ in range(m):
    u, v, d = MI()
    to[u - 1].append((v - 1, d))
    to[v - 1].append((u - 1, d))
    uvd.append((u - 1, v - 1, d))

solve()

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def MI1(): return map(int1, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

from heapq import *

def solve():
    dist = [inf] * n
    cnt = [0] * n
    hp = []
    heappush(hp, (0, s))
    dist[s] = 0
    cnt[s] = 1
    while hp:
        d, u = heappop(hp)
        if d > dist[t]: break
        if d > dist[u]: continue
        for v, c in to[u]:
            nd = d + c
            if nd == dist[v]: cnt[v] += cnt[u]
            elif nd < dist[v]:
                dist[v] = nd
                cnt[v] = cnt[u]
                heappush(hp, (nd, v))
    # print(dist)
    # print(cnt)

    distr = [inf] * n
    cntr = [0] * n
    hp = []
    heappush(hp, (0, t))
    distr[t] = 0
    cntr[t] = 1
    while hp:
        d, u = heappop(hp)
        if d * 2 > dist[t]: continue
        if d > distr[u]: continue
        for v, c in to[u]:
            nd = d + c
            if nd == distr[v]: cntr[v] += cntr[u]
            elif nd < distr[v]:
                distr[v] = nd
                cntr[v] = cntr[u]
                heappush(hp, (nd, v))
    # print(distr)
    # print(cntr)

    ans = pow(cnt[t], 2, md)
    dt = dist[t]
    for u in range(n):
        if dist[u] * 2 == dt:
            ans -= cnt[u] * cntr[u]
            ans %= md
    for u, v, c in uvd:
        for _ in range(2):
            if dist[u] + distr[v] + c == dt and dist[u] * 2 < dt and dist[v] * 2 > dt:
                ans -= cnt[u] * cntr[v]
                ans %= md
            u, v = v, u
    print(ans)

md=10**9+7
inf=10**16
n,m=MI()
s,t=MI1()
to=[[] for _ in range(n)]
uvd=[]
for _ in range(m):
    u,v,d=MI()
    to[u-1].append((v-1,d))
    to[v-1].append((u-1,d))
    uvd.append((u-1,v-1,d))

solve()

Submission status: Wrong Answer

Solution code:
def examC():
    ans = 0
    print(ans)
    return

def examD():
    ans = 0
    print(ans)
    return

def examE():
    def dijkstra(edges, start=0):
        dist = [inf] * len(edges)
        dist[start] = 0
        Q = [(0, start)]  # (dist,vertex)
        while (Q):
            d, v = heapq.heappop(Q)
            if dist[v] < d: continue  # 候補として挙がったd,vだが、他に短いのがある
            for u, cost in edges[v]:
                if dist[u] > dist[v] + cost:
                    dist[u] = dist[v] + cost
                    heapq.heappush(Q, (dist[u], u))
        return dist
    N, M = LI()
    S, T = LI()
    S -= 1; T -= 1
    V = [set()for _ in range(N)]
    for _ in range(M):
        u, v, c = LI()
        u -= 1; v -= 1
        V[u].add((v,c))
        V[v].add((u,c))
    distS = dijkstra(V,S)
    distT = dijkstra(V,T)
    L = distS[T]

    dp1 = [0]*N
    dp2 = [0]*N
    dp1[S] = 1; dp2[T] = 1
    S_collision_points = set()
    S_collision_edges = set()
    que = [(0,S)]
    used = set()
    while(que):
        d, now = heapq.heappop(que)
        if distS[now] + distT[now] == L and distS[now] == distT[now]:
            S_collision_points.add(now)
        used.add(now)
        #print(now,used,dp1)
        for v,c in V[now]:
            if distT[v]+distS[now]==L-c:
                #print(distS[v],distS[now],now)
                dp1[v] += dp1[now]
                dp1[v] %= mod
                if distS[now]*2<L and L<distS[v]*2:
                    S_collision_edges.add((now,v))
                if v in used:
                    continue
                heappush(que,(distS[v],v))
    T_collision_points = set()
    T_collision_edges = set()
    que = [(0,T)]
    used = set()
    while(que):
        d, now = heapq.heappop(que)
        used.add(now)
        for v,c in V[now]:
            if distS[v]+distT[now]==L-c:
                dp2[v] += dp2[now]
                dp2[v] %= mod
                if distT[now]<L/2 and L/2<distT[v]:
                    T_collision_edges.add((v,now))
                if v in used:
                    continue
                heappush(que,(distS[v],v))
                if distT[v]==L/2:
                    T_collision_points.add(v)
    collision_points = S_collision_points
    collision_edges = S_collision_edges
    #print(distS)
    #print(distT)
    #print(dp1)
    #print(dp2)
    #print(S_collision_edges)
    ans = 0
    ans += dp1[T]**2
    ans %= mod
    for p in collision_points:
        if distS[p]==distT[p]:
            ans -= (dp1[p]**2)*(dp2[p]**2)
            ans += supermod
            ans %= mod
            #print(ans,p)
    for u,v in collision_edges:
        ans -= (dp1[u]**2)*(dp2[v]**2)
        ans += supermod
        ans %= mod
        #print(ans,u,v)
    print(ans)
    return

def examF():
    ans = 0
    print(ans)
    return


from decimal import getcontext,Decimal as dec
import sys,bisect,itertools,heapq,math,random
from copy import deepcopy
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
def I(): return int(input())
def LI(): return list(map(int,sys.stdin.readline().split()))
def DI(): return dec(input())
def LDI(): return list(map(dec,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet,_ep
mod = 10**9 + 7
supermod = mod**4
mod2 = 998244353
inf = 10**18
_ep = dec("0.000000000001")
alphabet = [chr(ord('a') + i) for i in range(26)]
alphabet_convert = {chr(ord('a') + i): i for i in range(26)}

getcontext().prec = 28

sys.setrecursionlimit(10**7)

if __name__ == '__main__':
    examE()

"""
142
12 9 1445 0 1
asd dfg hj o o
aidn
"""

Submission status: Wrong Answer

Solution code:
def examC():
    ans = 0
    print(ans)
    return

def examD():
    ans = 0
    print(ans)
    return

def examE():
    def dijkstra(edges, start=0):
        dist = [inf] * len(edges)
        dist[start] = 0
        Q = [(0, start)]  # (dist,vertex)
        while (Q):
            d, v = heapq.heappop(Q)
            if dist[v] < d: continue  # 候補として挙がったd,vだが、他に短いのがある
            for u, cost in edges[v]:
                if dist[u] > dist[v] + cost:
                    dist[u] = dist[v] + cost
                    heapq.heappush(Q, (dist[u], u))
        return dist
    N, M = LI()
    S, T = LI()
    S -= 1; T -= 1
    V = [set()for _ in range(N)]
    for _ in range(M):
        u, v, c = LI()
        u -= 1; v -= 1
        V[u].add((v,c))
        V[v].add((u,c))
    distS = dijkstra(V,S)
    distT = dijkstra(V,T)
    L = distS[T]

    dp1 = [0]*N
    dp2 = [0]*N
    dp1[S] = 1; dp2[T] = 1
    S_collision_points = set()
    S_collision_edges = set()
    que = deque([S])
    used = set()
    while(que):
        now = que.pop()
        if distS[now] + distT[now] == L and distS[now] == distT[now]:
            S_collision_points.add(now)
        used.add(now)
        #print(now,used,dp1)
        for v,c in V[now]:
            if distT[v]+distS[now]==L-c:
                #print(distS[v],distS[now],now)
                dp1[v] += dp1[now]
                dp1[v] %= mod
                if distS[now]*2<L and L<distS[v]*2:
                    S_collision_edges.add((now,v))
                if v in used:
                    continue
                que.append(v)
    T_collision_points = set()
    T_collision_edges = set()
    que = deque([T])
    used = set()
    while(que):
        now = que.pop()
        used.add(now)
        for v,c in V[now]:
            if distS[v]+distT[now]==L-c:
                dp2[v] += dp2[now]
                dp2[v] %= mod
                if distT[now]<L/2 and L/2<distT[v]:
                    T_collision_edges.add((v,now))
                if v in used:
                    continue
                que.append(v)
                if distT[v]==L/2:
                    T_collision_points.add(v)
    collision_points = S_collision_points
    collision_edges = S_collision_edges
    #print(distS)
    #print(distT)
    #print(dp1)
    #print(dp2)
    #print(S_collision_edges)
    ans = 0
    ans += dp1[T]**2
    ans %= mod
    for p in collision_points:
        if distS[p]==distT[p]:
            ans -= (dp1[p]**2)*(dp2[p]**2)
            ans += supermod
            ans %= mod
            #print(ans,p)
    for u,v in collision_edges:
        ans -= (dp1[u]**2)*(dp2[v]**2)
        ans += supermod
        ans %= mod
        #print(ans,u,v)
    print(ans)
    return

def examF():
    ans = 0
    print(ans)
    return


from decimal import getcontext,Decimal as dec
import sys,bisect,itertools,heapq,math,random
from copy import deepcopy
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
def I(): return int(input())
def LI(): return list(map(int,sys.stdin.readline().split()))
def DI(): return dec(input())
def LDI(): return list(map(dec,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet,_ep
mod = 10**9 + 7
supermod = mod**4
mod2 = 998244353
inf = 10**18
_ep = dec("0.000000000001")
alphabet = [chr(ord('a') + i) for i in range(26)]
alphabet_convert = {chr(ord('a') + i): i for i in range(26)}

getcontext().prec = 28

sys.setrecursionlimit(10**7)

if __name__ == '__main__':
    examE()

"""
142
12 9 1445 0 1
asd dfg hj o o
aidn
"""