[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ํ•ฉ์Šน ํƒ์‹œ ์š”๊ธˆ / ํŒŒ์ด์ฌ / Python / Dijkstra / 2021 KAKAO BLIND RECRUITMENT/ ์นด์นด์˜ค ์ฝ”ํ…Œ
Algorithm/Programmers

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ํ•ฉ์Šน ํƒ์‹œ ์š”๊ธˆ / ํŒŒ์ด์ฌ / Python / Dijkstra / 2021 KAKAO BLIND RECRUITMENT/ ์นด์นด์˜ค ์ฝ”ํ…Œ

728x90
๋ฐ˜์‘ํ˜•

๐Ÿ’กsolutions )

๐Ÿ’ฌ ๋‹ค์ต์ŠคํŠธ๋ผ(Dijkstra) ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฌธ์ œ๋ฅผ ํ’€์—ˆ๋‹ค.

๐Ÿ’ฌ ๋‹ค์Œ ๋‘ ๊ฐ€์ง€ ๊ฒฝ์šฐ๋ฅผ ๋ชจ๋‘ ํฌํ•จํ•˜์—ฌ ์ตœ์†Œ ๋น„์šฉ์„ ๊ตฌํ•ด์•ผ ํ•˜๋Š” ๋ฌธ์ œ๋‹ค.

(1) ๋ฌด์ง€์™€ ์–ดํ”ผ์น˜๊ฐ€ ๋”ฐ๋กœ ํƒ์‹œ๋ฅผ ํƒ€๊ณ  ๊ฐ€๋Šฅ ๊ฒฝ์šฐ

(2) ํŠน์ • ์ง€์ ๊นŒ์ง€ ํ•จ๊ป˜ ์ด๋™ ํ›„ ๊ฐ์ž ๋ชฉ์ ์ง€๊นŒ์ง€ ๊ฐ€๋Š” ๊ฒฝ์šฐ -> ์ด๋•Œ ๋‘˜ ์ค‘ ํ•œ์‚ฌ๋žŒ์ด ๋‚˜๋จธ์ง€ ํ•œ์‚ฌ๋žŒ์„ ๋ฐ๋ ค๋‹ค ์ฃผ๊ณ  ๋ณธ์ธ ์ง‘์œผ๋กœ ์ด๋™ํ•˜๋Š” ๊ฒฝ์šฐ๋„ ํฌํ•จ๋œ๋‹ค.

๐Ÿ’ฌ ์นด์นด์˜ค ํ…Œํฌ ๋ธ”๋กœ๊ทธ์— ๋”ฐ๋ฅด๋ฉด, ๋‹ค์ต์ŠคํŠธ๋ผ์™€ ํ”Œ๋กœ์ด๋“œ์™€์ƒฌ ๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์œผ๋กœ ํ•ด๊ฒฐ ๊ฐ€๋Šฅํ•˜๋‹ค. ์ถ”ํ›„ ํ”Œ๋กœ์ด๋“œ์™€์ƒฌ๋กœ๋„ ํ’€์–ด๋ด์•ผ๊ฒ ๋‹ค.

๐Ÿ‘จ‍๐Ÿ’ปcode )

import heapq


def dijkstra(adj, num, s, e):
    heap = []
    heapq.heappush(heap, [0, s])
    INF = float('inf')
    dist = [INF] * (num + 1)
    dist[s] = 0

    while heap:
        cost, node = heapq.heappop(heap)
        if dist[node] < cost:
            continue
        for n, c in adj[node]:
            c += cost
            if c < dist[n]:
                dist[n] = c
                heapq.heappush(heap, [c, n])
    return dist[e]


def solution(num, s, a, b, fares):
    adj = [[] for _ in range(num + 1)]  # ์ธ์ ‘ํ–‰๋ ฌ
    for x, y, c in fares:
        adj[x].append([y, c])
        adj[y].append([x, c])
    res = dijkstra(adj, num, s, a) + dijkstra(adj, num, s, b)  # (1)์‹œ์ž‘์ ์—์„œ a, b๊นŒ์ง€ ๊ฐ์ž ๋”ฐ๋กœ ๊ฐ€๋Š” ๊ฒฝ์šฐ
    for i in range(1, num + 1):
        if s != i:
            # (2)ํŠน์ • ์ง€์ ๊นŒ์ง€ ์ด๋™ ํ›„, ๊ฐ์ž ๋ชฉ์ ์ง€๊นŒ์ง€ ๊ฐ€๋Š” ๊ฒฝ์šฐ
            # (1)๊ณผ(2) ๋น„์šฉ ์ค‘ ์ตœ์†Œ๊ฐ’ ๊ตฌํ•˜๊ธฐ
            res = min(res, dijkstra(adj, num, s, i) + dijkstra(adj, num, i, a) + dijkstra(adj, num, i, b))
    return res

๐Ÿ“Œdescription )

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ํ•ฉ์Šน ํƒ์‹œ ์š”๊ธˆ

6 4 6 2 [[4, 1, 10], [3, 5, 24], [5, 6, 2], [3, 1, 41], [5, 1, 24], [4, 6, 50], [2, 4, 66], [2, 3, 22], [1, 6, 25]] 82 7 3 4 1 [[5, 7, 9], [4, 6, 4], [3, 6, 1], [3, 2, 3], [2, 1, 6]] 14 6 4 5 6 [[2,6,6], [6,3,7], [4,6,7], [6,5,11], [2,5,12], [5,3,20], [2,4

programmers.co.kr


[๋ฌธ์ œ ์„ค๋ช…][๋ณธ ๋ฌธ์ œ๋Š” ์ •ํ™•์„ฑ๊ณผ ํšจ์œจ์„ฑ ํ…Œ์ŠคํŠธ ๊ฐ๊ฐ ์ ์ˆ˜๊ฐ€ ์žˆ๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.]

๋ฐค๋Šฆ๊ฒŒ ๊ท€๊ฐ€ํ•  ๋•Œ ์•ˆ์ „์„ ์œ„ํ•ด ํ•ญ์ƒ ํƒ์‹œ๋ฅผ ์ด์šฉํ•˜๋˜ ๋ฌด์ง€๋Š” ์ตœ๊ทผ ์•ผ๊ทผ์ด ์žฆ์•„์ ธ ํƒ์‹œ๋ฅผ ๋” ๋งŽ์ด ์ด์šฉํ•˜๊ฒŒ ๋˜์–ด ํƒ์‹œ๋น„๋ฅผ ์•„๋‚„ ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์„ ๊ณ ๋ฏผํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฌด์ง€๋Š” ์ž์‹ ์ด ํƒ์‹œ๋ฅผ ์ด์šฉํ•  ๋•Œ ๋™๋ฃŒ์ธ ์–ดํ”ผ์น˜ ์—ญ์‹œ ์ž์‹ ๊ณผ ๋น„์Šทํ•œ ๋ฐฉํ–ฅ์œผ๋กœ ๊ฐ€๋Š” ํƒ์‹œ๋ฅผ ์ข…์ข… ์ด์šฉํ•˜๋Š” ๊ฒƒ์„ ์•Œ๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๋ฌด์ง€๋Š” ์–ดํ”ผ์น˜์™€ ๊ท€๊ฐ€ ๋ฐฉํ–ฅ์ด ๋น„์Šทํ•˜์—ฌ ํƒ์‹œ ํ•ฉ์Šน์„ ์ ์ ˆํžˆ ์ด์šฉํ•˜๋ฉด ํƒ์‹œ์š”๊ธˆ์„ ์–ผ๋งˆ๋‚˜ ์•„๋‚„ ์ˆ˜ ์žˆ์„ ์ง€ ๊ณ„์‚ฐํ•ด ๋ณด๊ณ  ์–ดํ”ผ์น˜์—๊ฒŒ ํ•ฉ์Šน์„ ์ œ์•ˆํ•ด ๋ณด๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

์œ„ ์˜ˆ์‹œ ๊ทธ๋ฆผ์€ ํƒ์‹œ๊ฐ€ ์ด๋™ ๊ฐ€๋Šฅํ•œ ๋ฐ˜๊ฒฝ์— ์žˆ๋Š” 6๊ฐœ ์ง€์  ์‚ฌ์ด์˜ ์ด๋™ ๊ฐ€๋Šฅํ•œ ํƒ์‹œ๋…ธ์„ ๊ณผ ์˜ˆ์ƒ์š”๊ธˆ์„ ๋ณด์—ฌ์ฃผ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆผ์—์„œ A์™€ B ๋‘ ์‚ฌ๋žŒ์€ ์ถœ๋ฐœ์ง€์ ์ธ 4๋ฒˆ ์ง€์ ์—์„œ ์ถœ๋ฐœํ•ด์„œ ํƒ์‹œ๋ฅผ ํƒ€๊ณ  ๊ท€๊ฐ€ํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. A์˜ ์ง‘์€ 6๋ฒˆ ์ง€์ ์— ์žˆ์œผ๋ฉฐ B์˜ ์ง‘์€ 2๋ฒˆ ์ง€์ ์— ์žˆ๊ณ  ๋‘ ์‚ฌ๋žŒ์ด ๋ชจ๋‘ ๊ท€๊ฐ€ํ•˜๋Š” ๋ฐ ์†Œ์š”๋˜๋Š” ์˜ˆ์ƒ ์ตœ์ € ํƒ์‹œ์š”๊ธˆ์ด ์–ผ๋งˆ์ธ ์ง€ ๊ณ„์‚ฐํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

 

[๋ฌธ์ œ]

์ง€์ ์˜ ๊ฐœ์ˆ˜ n, ์ถœ๋ฐœ์ง€์ ์„ ๋‚˜ํƒ€๋‚ด๋Š” s, A์˜ ๋„์ฐฉ์ง€์ ์„ ๋‚˜ํƒ€๋‚ด๋Š” a, B์˜ ๋„์ฐฉ์ง€์ ์„ ๋‚˜ํƒ€๋‚ด๋Š” b, ์ง€์  ์‚ฌ์ด์˜ ์˜ˆ์ƒ ํƒ์‹œ์š”๊ธˆ์„ ๋‚˜ํƒ€๋‚ด๋Š” fares๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. ์ด๋•Œ, A, B ๋‘ ์‚ฌ๋žŒ์ด s์—์„œ ์ถœ๋ฐœํ•ด์„œ ๊ฐ๊ฐ์˜ ๋„์ฐฉ ์ง€์ ๊นŒ์ง€ ํƒ์‹œ๋ฅผ ํƒ€๊ณ  ๊ฐ„๋‹ค๊ณ  ๊ฐ€์ •ํ•  ๋•Œ, ์ตœ์ € ์˜ˆ์ƒ ํƒ์‹œ์š”๊ธˆ์„ ๊ณ„์‚ฐํ•ด์„œ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด ์ฃผ์„ธ์š”.
๋งŒ์•ฝ, ์•„์˜ˆ ํ•ฉ์Šน์„ ํ•˜์ง€ ์•Š๊ณ  ๊ฐ์ž ์ด๋™ํ•˜๋Š” ๊ฒฝ์šฐ์˜ ์˜ˆ์ƒ ํƒ์‹œ์š”๊ธˆ์ด ๋” ๋‚ฎ๋‹ค๋ฉด, ํ•ฉ์Šน์„ ํ•˜์ง€ ์•Š์•„๋„ ๋ฉ๋‹ˆ๋‹ค.





๋ฐ˜์‘ํ˜•