DevYoon

[๋ฐฑ์ค€] 18352. ํŠน์ • ๊ฑฐ๋ฆฌ์˜ ๋„์‹œ ์ฐพ๊ธฐ (Python) ๋ณธ๋ฌธ

PS/Baekjoon

[๋ฐฑ์ค€] 18352. ํŠน์ • ๊ฑฐ๋ฆฌ์˜ ๋„์‹œ ์ฐพ๊ธฐ (Python)

gimewn 2022. 6. 3. 13:25

link ๐Ÿ”— https://www.acmicpc.net/problem/18352

 

18352๋ฒˆ: ํŠน์ • ๊ฑฐ๋ฆฌ์˜ ๋„์‹œ ์ฐพ๊ธฐ

์ฒซ์งธ ์ค„์— ๋„์‹œ์˜ ๊ฐœ์ˆ˜ N, ๋„๋กœ์˜ ๊ฐœ์ˆ˜ M, ๊ฑฐ๋ฆฌ ์ •๋ณด K, ์ถœ๋ฐœ ๋„์‹œ์˜ ๋ฒˆํ˜ธ X๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. (2 ≤ N ≤ 300,000, 1 ≤ M ≤ 1,000,000, 1 ≤ K ≤ 300,000, 1 ≤ X ≤ N) ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ M๊ฐœ์˜ ์ค„์— ๊ฑธ์ณ์„œ ๋‘ ๊ฐœ

www.acmicpc.net

 

๐Ÿ”ฅ ๊ธฐ๋ณธ์ ์ธ ๋‹ค์ต์ŠคํŠธ๋ผ ๋ฌธ์ œ

๐Ÿ”ฅ ๋‹ค์ต์ŠคํŠธ๋ผ์ผ๊นŒ ๋ฐ์ดํฌ์ŠคํŠธ๋ผ์ผ๊นŒ... ๋ญ๊ฐ€ ๋งž๋Š” ๊ฒƒ์ผ๊นŒ...

 

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)