Skip to content

Commit

Permalink
2024-08-28 solved
Browse files Browse the repository at this point in the history
  • Loading branch information
alstjr7437 committed Aug 28, 2024
1 parent 4d6a1f8 commit 7da00d1
Showing 1 changed file with 38 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from collections import deque
import sys

input = sys.stdin.readline
INF = int(1e9)

N, M, X = map(int, input().split())

graph = [[] for _ in range(N+1)]

for _ in range(M):
start, target, dist = map(int, input().split())
graph[start].append((target, dist))

def dijkstra(root):
queue = deque()
distance = [INF] * (N + 1)

distance[root] = 0
queue.append((root, 0))
while queue:
now, dist = queue.popleft()

for target, cost in graph[now]:
target_cost = dist + cost

if distance[target] > target_cost:
distance[target] = target_cost
queue.append((target, target_cost))
return distance

result = []
for i in range(1, N+1):
go = dijkstra(i)[X]
back = dijkstra(X)[i]
result.append(go+back)

print(max(result))

0 comments on commit 7da00d1

Please sign in to comment.