This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
from heapq import *
def solve():
n, m = map(int, input().split())
s, t = map(int, input().split())
u, v = map(int, input().split())
s -= 1
t -= 1
u -= 1
v -= 1
g = [[] for i in range(n)]
rev = [[] for i in range(n)]
dag = [[] for i in range(n)]
for _ in range(m):
a, b, c = map(int, (input().split()))
a -= 1
b -= 1
g[a].append((c, b))
g[b].append((c, a))
def dijkstra(src):
dist = [float('inf')]*n
dist[src]=0
q=[]
heappush(q, (0, src))
while q:
d,u = heappop(q)
for w,v in g[u]:
if d+w<dist[v]:
dist[v]=d+w
heappush(q,(dist[v],v))
return dist
du = dijkstra(u)
dv = dijkstra(v)
ans = du[v]
dist = [float('inf')]*n
q = []
heappush(q,(0,s))
dist[s] = 0
while q:
d, u = heappop(q)
for w,v in g[u]:
if d+w<dist[v]:
dist[v]=d+w
heappush(q,(dist[v],v))
rev[v] = []
if d+w==dist[v]:
rev[v].append(u)
for u,vs in enumerate(rev):
for v in vs:
dag[v].append(u)
dpB = [float('inf')]*n
dpF = [float('inf')]*n
def dfs(i):
if (len(rev[i])):
dpB[i] = min(dpB[j] for j in rev[i])
dpB[i] = min(dpB[i], dv[i])
for j in dag[i]:
dfs(j)
dfs(s)
in_dag = []
def dfs2(i):
in_dag.append(i)
if (len(dag[i])):
dpF[i] = min(dpF[j] for j in dag[i])
dpF[i] = min(dpF[i], dv[i])
for j in rev[i]:
dfs2(j)
dfs2(t)
for i in in_dag:
ans = min(ans, du[i]+min(dpF[i], dpB[i]))
print(ans)
solve()
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |