728x90
λ¬Έμ
νμ΄
λ€μ΅μ€νΈλΌ μκ³ λ¦¬μ¦μ μ¬μ©νλ©΄ λλ€.
λ¨, νμλ€μ΄ λ§μλ‘ λμκ°λ κ²½μ°λ ꡬν΄μΌ νλλ°, μ΄λμ κ°μ μ μ£Όμ΄μ§ μ λ ₯μ μμμ κ³Ό λμ μ μλ‘ λ°κΎΈλ©΄ λλ€.
μ½λ
from sys import stdin
import heapq
input = stdin.readline
n, m, x = map(int, input().split())
toX = [[] for _ in range(n + 1)] #μ§μμ νν° λ§μλ‘ κ°λ μ΅λ¨ κ²½λ‘
toHome = [[] for _ in range(n + 1)] #νν° λ§μμμ μ§μΌλ‘ κ°λ μ΅λ¨ κ²½λ‘
for _ in range(m):
a, b, t = map(int, input().split())
toX[a].append((b, t))
toHome[b].append((a, t))
def dijk(graph, start):
distance = [1e9] * (n + 1)
q = []
heapq.heappush(q, (0, start))
distance[start] = 0
while q:
dist, now = heapq.heappop(q)
if distance[now] < dist:
continue
for i in graph[now]:
cost = dist + i[1]
if cost < distance[i[0]]:
distance[i[0]] = cost
heapq.heappush(q, (cost, i[0]))
return distance[1:]
print(max([a+b for a, b in zip(dijk(toX, x), dijk(toHome, x))]))
λ§λ¬΄λ¦¬
맨 μ²μμλ μμμ κ³Ό λμ μ λ°κΏ μκ°μ νμ§ λͺ»ν΄ i (1<= i <=n) λ Έλμ distance[x] κ°μ ꡬν λ€, κ·Έ κ°μ x λ Έλμ distance[i]μ λν΄μ£Όμλ€. λ¬Έμ λ ν μ μμΌλ μμ§ μ€λ ₯μ΄ λ§μ΄ λΆμ‘±ν¨μ λκΌλ€...
728x90