ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Python] ๋ฐฑ์ค€ 18352 ํŠน์ • ๊ฑฐ๋ฆฌ์˜ ๋„์‹œ ์ฐพ๊ธฐ (Dijkstra)
    ๐Ÿ’ป Algorithm/Python 2022. 7. 8. 23:40

    ๐Ÿ“Œ ๋ฌธ์ œ

    ์–ด๋–ค ๋‚˜๋ผ์—๋Š” 1๋ฒˆ๋ถ€ํ„ฐ N๋ฒˆ๊นŒ์ง€์˜ ๋„์‹œ์™€ M๊ฐœ์˜ ๋‹จ๋ฐฉํ–ฅ ๋„๋กœ๊ฐ€ ์กด์žฌํ•œ๋‹ค. ๋ชจ๋“  ๋„๋กœ์˜ ๊ฑฐ๋ฆฌ๋Š” 1์ด๋‹ค.

     

    ์ด ๋•Œ ํŠน์ •ํ•œ ๋„์‹œ X๋กœ๋ถ€ํ„ฐ ์ถœ๋ฐœํ•˜์—ฌ ๋„๋‹ฌํ•  ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ๋„์‹œ ์ค‘์—์„œ, ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๊ฐ€ ์ •ํ™•ํžˆ K์ธ ๋ชจ๋“  ๋„์‹œ๋“ค์˜ ๋ฒˆํ˜ธ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค. ๋˜ํ•œ ์ถœ๋ฐœ ๋„์‹œ X์—์„œ ์ถœ๋ฐœ ๋„์‹œ X๋กœ ๊ฐ€๋Š” ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๋Š” ํ•ญ์ƒ 0์ด๋ผ๊ณ  ๊ฐ€์ •ํ•œ๋‹ค.

    ์˜ˆ๋ฅผ ๋“ค์–ด N=4, K=2, X=1์ผ ๋•Œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ทธ๋ž˜ํ”„๊ฐ€ ๊ตฌ์„ฑ๋˜์–ด ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜์ž.

     

    ์ด ๋•Œ 1๋ฒˆ ๋„์‹œ์—์„œ ์ถœ๋ฐœํ•˜์—ฌ ๋„๋‹ฌํ•  ์ˆ˜ ์žˆ๋Š” ๋„์‹œ ์ค‘์—์„œ, ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๊ฐ€ 2์ธ ๋„์‹œ๋Š” 4๋ฒˆ ๋„์‹œ ๋ฟ์ด๋‹ค. 2๋ฒˆ๊ณผ 3๋ฒˆ ๋„์‹œ์˜ ๊ฒฝ์šฐ, ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๊ฐ€ 1์ด๊ธฐ ๋•Œ๋ฌธ์— ์ถœ๋ ฅํ•˜์ง€ ์•Š๋Š”๋‹ค.

    ์ž…๋ ฅ

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

    ์ถœ๋ ฅ

    X๋กœ๋ถ€ํ„ฐ ์ถœ๋ฐœํ•˜์—ฌ ๋„๋‹ฌํ•  ์ˆ˜ ์žˆ๋Š” ๋„์‹œ ์ค‘์—์„œ, ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๊ฐ€ K์ธ ๋ชจ๋“  ๋„์‹œ์˜ ๋ฒˆํ˜ธ๋ฅผ ํ•œ ์ค„์— ํ•˜๋‚˜์”ฉ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ถœ๋ ฅํ•œ๋‹ค.

     

    ์ด ๋•Œ ๋„๋‹ฌํ•  ์ˆ˜ ์žˆ๋Š” ๋„์‹œ ์ค‘์—์„œ, ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๊ฐ€ K์ธ ๋„์‹œ๊ฐ€ ํ•˜๋‚˜๋„ ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด -1์„ ์ถœ๋ ฅํ•œ๋‹ค.

    ์˜ˆ์ œ ์ž…๋ ฅ 1

    4 4 2 1
    1 2
    1 3
    2 3
    2 4

    ์˜ˆ์ œ ์ถœ๋ ฅ 1

    4

    ์˜ˆ์ œ ์ž…๋ ฅ 2

    4 3 2 1
    1 2
    1 3
    1 4

    ์˜ˆ์ œ ์ถœ๋ ฅ 2

    -1

    ์˜ˆ์ œ ์ž…๋ ฅ 3

    4 4 1 1
    1 2
    1 3
    2 3
    2 4

    ์˜ˆ์ œ ์ถœ๋ ฅ 3

    2
    3

     


    ๐Ÿ“Œ ํ’€์ด

    ๐Ÿ’ฌ Code

    import sys, heapq
    input = sys.stdin.readline
    INF = float('inf')
    
    
    def dijkstra(start):
        q = []
        heapq.heappush(q, (0, start))
        distance[start] = 0
    
        while q:
            dist, now = heapq.heappop(q)
            if dist > distance[now]:
                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]))
    
    
    n, m, k, start = map(int, input().split())
    graph = [[] for _ in range(n+1)]
    distance = [INF] * (n+1)
    
    for _ in range(m):
        a, b = map(int, input().split())
        graph[a].append((b, 1))
    
    dijkstra(start)
    
    isNone = 1
    for i in range(1, n+1):
        if distance[i] == k:
            isNone = 0
            print(i)
    
    if isNone:
        print(-1)

    ๐Ÿ’ก Solution

    BFS๋กœ๋„ ํ’€ ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ์ง€๋งŒ ๋‹ค์ต์ŠคํŠธ๋ผ๋ฅผ ๋ฐฐ์šด ๊น€์— ๋‹ค์ต์ŠคํŠธ๋ผ๋กœ ํ’€์–ด๋ณด์•˜์Šต๋‹ˆ๋‹ค.

     

     

     

    ๋Œ“๊ธ€

Designed by Tistory.