DevYoon
[๋ฐฑ์ค] 18352. ํน์ ๊ฑฐ๋ฆฌ์ ๋์ ์ฐพ๊ธฐ (Python) ๋ณธ๋ฌธ
link ๐ https://www.acmicpc.net/problem/18352
๐ฅ ๊ธฐ๋ณธ์ ์ธ ๋ค์ต์คํธ๋ผ ๋ฌธ์
๐ฅ ๋ค์ต์คํธ๋ผ์ผ๊น ๋ฐ์ดํฌ์คํธ๋ผ์ผ๊น... ๋ญ๊ฐ ๋ง๋ ๊ฒ์ผ๊น...
import heapq
import sys
N, M, K, X = map(int, sys.stdin.readline().split())
inf = 2e18
board = [[] for _ in range(N+1)]
result = [inf] * (N+1)
def dijkstra(start):
result[start] = 0
heap = []
heapq.heappush(heap, (0, start)) # ๋น์ฉ, ๊ฒฝ์ ์ง
while heap:
dis, now = heapq.heappop(heap)
if dis > result[now]:
continue
for b in board[now]:
cost = dis+b[1]
if cost < result[b[0]]:
result[b[0]] = cost
heapq.heappush(heap, (cost, b[0]))
for _ in range(M):
y, x = map(int, sys.stdin.readline().split())
board[y].append((x, 1)) # ๋์ฐฉ์ง, ๋น์ฉ
dijkstra(X)
flag = 1
for i in range(1, N+1):
if result[i] == K:
flag = 0
print(i)
if flag:
print(-1)