Submission #1351305

#TimeUsernameProblemLanguageResultExecution timeMemory
1351305sed9871Commuter Pass (JOI18_commuter_pass)Pypy 3
0 / 100
132 ms133256 KiB
import sys
import heapq

input = sys.stdin.readline
INF = 10**18

def dijkstra(start, adj, n):
    dist = [INF] * (n + 1)
    dist[start] = 0
    pq = [(0, start)]  # (distance, node)

    while pq:
        d, u = heapq.heappop(pq)
        if d > dist[u]:
            continue
        for v, w in adj[u]:
            if dist[v] > d + w:
                dist[v] = d + w
                heapq.heappush(pq, (dist[v], v))
    return dist


# Input
n, m = map(int, input().split())

adj = [[] for _ in range(n + 1)]
for _ in range(m):
    a, b, c = map(int, input().split())
    adj[a].append((b, c))
    adj[b].append((a, c))

S, T, U, V = map(int, input().split())

# Step 1: distances from S and T
dS = dijkstra(S, adj, n)
dT = dijkstra(T, adj, n)

D = dS[T]

# Step 2: build modified graph
adj2 = [[] for _ in range(n + 1)]

for u in range(1, n + 1):
    for v, w in adj[u]:
        cost = w
        if dS[u] + w + dT[v] == D or dS[v] + w + dT[u] == D:
            cost = 0
        adj2[u].append((v, cost))

# Step 3: shortest path from U to V
dist = [INF] * (n + 1)
dist[U] = 0
pq = [(0, U)]

while pq:
    d, u = heapq.heappop(pq)
    if d > dist[u]:
        continue
    for v, w in adj2[u]:
        if dist[v] > d + w:
            dist[v] = d + w
            heapq.heappush(pq, (dist[v], v))

print(dist[V])

Compilation message (stdout)

Compiling 'commuter_pass.py'...

=======
  adding: __main__.pyc (deflated 40%)

=======
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...