๋ฌธ์
https://www.acmicpc.net/problem/1238
ํ์ด
# ๋ฐฑ์ค 1238๋ฒ ๋ฌธ์ - ํํฐ
import heapq
n, m, x = map(int, input().split())
# n: ๋
ธ๋, m: ๊ฐ์ ์ ๊ฐ์
# ๋ชจ๋ ํ์ x์ ๊ฐ ์ ์๊ณ x์์ ์ง์ผ๋ก ๋์์ฌ ์ ์๋ ๋ฐ์ดํฐ๋ง ์
๋ ฅ
graph = [[] for _ in range(n+1)]
for _ in range(m):
a, b, cost = map(int, input().split())
graph[a].append((b,cost))
def dijkstra(start):
q = []
distance = [1e9] * (n+1)
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 distance[i[0]] > cost:
distance[i[0]] = cost
heapq.heappush(q, (cost, i[0]))
return distance
result = 0
for i in range(1,n+1):
go = dijkstra(i)
back = dijkstra(x)
result = max(result, go[x] + back[i])
print(result)
์ต๋จ๊ฒฝ๋ก๋ฅผ ๊ตฌํ๋ ๋ค์ต์คํธ๋ผ ์๊ณ ๋ฆฌ์ฆ์ ์ด์ฉํ๋ ๋ฌธ์ ์ด๋ค.
x๋ก ๊ฐ๋ ์ต๋จ๋น์ฉ๊ณผ ๋ค์ ์ง์ผ๋ก ๋์์ค๋ ์ต๋จ๋น์ฉ์ ๋ํ์ฌ ๊ฐ์ฅ ํฐ ๊ฐ์ ๊ตฌํ๋ฉด ๋๋ค.
'๐๏ธ Algorithm > ๐ฉ ๋ฐฑ์ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๐ฉ [๋ฐฑ์ค] [Python] [Gold4] 1043๋ฒ_๊ฑฐ์ง๋ง (0) | 2023.05.12 |
---|---|
๐ฉ [๋ฐฑ์ค] [Python] [Gold5] 13023๋ฒ_ABCDE (0) | 2023.05.11 |
๐ฉ [๋ฐฑ์ค] [Python] [Gold4] 11054๋ฒ_๊ฐ์ฅ ๊ธด ๋ฐ์ดํ ๋ ๋ถ๋ถ ์์ด (1) | 2023.05.09 |
๐ฉ [๋ฐฑ์ค] [Python] [Gold4] 14500๋ฒ_ํ ํธ๋ก๋ฏธ๋ ธ (1) | 2023.05.08 |
๐ฉ [๋ฐฑ์ค] [Python] [Silver2] 1535๋ฒ_์๋ (2) | 2023.05.05 |