[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ๋ฐฐ๋‹ฌ / ํŒŒ์ด์ฌ / Python / Dijkstra
Algorithm/Programmers

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ๋ฐฐ๋‹ฌ / ํŒŒ์ด์ฌ / Python / Dijkstra

728x90
๋ฐ˜์‘ํ˜•

 

๐Ÿ’กsolutions )

๐Ÿ’ฌ ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•˜์—ฌ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•˜๋Š” ๋กœ์ง

๐Ÿ’ฌ ์ฒ˜์Œ ์‹œ์ž‘ํ•˜๋Š” ๋…ธ๋“œ 1์—์„œ์˜ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๋Š” 0์œผ๋กœ ์ดˆ๊ธฐํ™”

๐Ÿ’ฌ ์ธ์ ‘ํ–‰๋ ฌ ๋งŒ๋“ค๊ณ  -> heappush๋ฅผ ํ†ตํ•ด ์ตœ๋‹จ ๊ฑฐ๋ฆฌ ์ˆœ์œผ๋กœ ์ •๋ ฌ -> ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๊ฐ€ ๋  ๋•Œ๋งˆ๋‹ค ๊ฑฐ๋ฆฌ๋ฅผ ์ €์žฅํ•ด ๋†“๋Š” ๋ฆฌ์ŠคํŠธ์ธ dis ์—…๋ฐ์ดํŠธ

 

๐Ÿ‘จ‍๐Ÿ’ปcode )

import heapq


def dijkstra(dis, adj):
    heap = []
    heapq.heappush(heap, [0, 1])

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


def solution(N, road, K):
    INF = float('inf')
    dis = [INF] * (N + 1)
    dis[1] = 0  # ๋งˆ์„ 1 ์ดˆ๊ธฐํ™”
    adj = [[] for _ in range(N + 1)]
    for r in road:
        adj[r[0]].append([r[2], r[1]])
        adj[r[1]].append([r[2], r[0]])
    dijkstra(dis, adj)
    return len([x for x in dis if x <= K])

๐Ÿ“Œdescription )

 

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ๋ฐฐ๋‹ฌ

5 [[1,2,1],[2,3,3],[5,2,2],[1,4,2],[5,3,1],[5,4,2]] 3 4 6 [[1,2,1],[1,3,2],[2,3,2],[3,4,3],[3,5,2],[3,5,3],[5,6,1]] 4 4

programmers.co.kr


>๋ฌธ์ œ ์„ค๋ช…

 

N๊ฐœ์˜ ๋งˆ์„๋กœ ์ด๋ฃจ์–ด์ง„ ๋‚˜๋ผ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๋‚˜๋ผ์˜ ๊ฐ ๋งˆ์„์—๋Š” 1๋ถ€ํ„ฐ N๊นŒ์ง€์˜ ๋ฒˆํ˜ธ๊ฐ€ ๊ฐ๊ฐ ํ•˜๋‚˜์”ฉ ๋ถ€์—ฌ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ ๋งˆ์„์€ ์–‘๋ฐฉํ–ฅ์œผ๋กœ ํ†ตํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” ๋„๋กœ๋กœ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋Š”๋ฐ, ์„œ๋กœ ๋‹ค๋ฅธ ๋งˆ์„ ๊ฐ„์— ์ด๋™ํ•  ๋•Œ๋Š” ์ด ๋„๋กœ๋ฅผ ์ง€๋‚˜์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋„๋กœ๋ฅผ ์ง€๋‚  ๋•Œ ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„์€ ๋„๋กœ๋ณ„๋กœ ๋‹ค๋ฆ…๋‹ˆ๋‹ค. ํ˜„์žฌ 1๋ฒˆ ๋งˆ์„์— ์žˆ๋Š” ์Œ์‹์ ์—์„œ ๊ฐ ๋งˆ์„๋กœ ์Œ์‹ ๋ฐฐ๋‹ฌ์„ ํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ ๋งˆ์„๋กœ๋ถ€ํ„ฐ ์Œ์‹ ์ฃผ๋ฌธ์„ ๋ฐ›์œผ๋ ค๊ณ  ํ•˜๋Š”๋ฐ, N๊ฐœ์˜ ๋งˆ์„ ์ค‘์—์„œ K ์‹œ๊ฐ„ ์ดํ•˜๋กœ ๋ฐฐ๋‹ฌ์ด ๊ฐ€๋Šฅํ•œ ๋งˆ์„์—์„œ๋งŒ ์ฃผ๋ฌธ์„ ๋ฐ›์œผ๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ์€ N = 5, K = 3์ธ ๊ฒฝ์šฐ์˜ ์˜ˆ์‹œ์ž…๋‹ˆ๋‹ค.

์œ„ ๊ทธ๋ฆผ์—์„œ 1๋ฒˆ ๋งˆ์„์— ์žˆ๋Š” ์Œ์‹์ ์€ [1, 2, 4, 5] ๋ฒˆ ๋งˆ์„๊นŒ์ง€๋Š” 3 ์ดํ•˜์˜ ์‹œ๊ฐ„์— ๋ฐฐ๋‹ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ 3๋ฒˆ ๋งˆ์„๊นŒ์ง€๋Š” 3์‹œ๊ฐ„ ์ด๋‚ด๋กœ ๋ฐฐ๋‹ฌํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒฝ๋กœ๊ฐ€ ์—†์œผ๋ฏ€๋กœ 3๋ฒˆ ๋งˆ์„์—์„œ๋Š” ์ฃผ๋ฌธ์„ ๋ฐ›์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ 1๋ฒˆ ๋งˆ์„์— ์žˆ๋Š” ์Œ์‹์ ์ด ๋ฐฐ๋‹ฌ ์ฃผ๋ฌธ์„ ๋ฐ›์„ ์ˆ˜ ์žˆ๋Š” ๋งˆ์„์€ 4๊ฐœ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.
๋งˆ์„์˜ ๊ฐœ์ˆ˜ N, ๊ฐ ๋งˆ์„์„ ์—ฐ๊ฒฐํ•˜๋Š” ๋„๋กœ์˜ ์ •๋ณด road, ์Œ์‹ ๋ฐฐ๋‹ฌ์ด ๊ฐ€๋Šฅํ•œ ์‹œ๊ฐ„ K๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, ์Œ์‹ ์ฃผ๋ฌธ์„ ๋ฐ›์„ ์ˆ˜ ์žˆ๋Š” ๋งˆ์„์˜ ๊ฐœ์ˆ˜๋ฅผ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

 

>์ œํ•œ์‚ฌํ•ญ

  • ๋งˆ์„์˜ ๊ฐœ์ˆ˜ N์€ 1 ์ด์ƒ 50 ์ดํ•˜์˜ ์ž์—ฐ์ˆ˜์ž…๋‹ˆ๋‹ค.
  • road์˜ ๊ธธ์ด(๋„๋กœ ์ •๋ณด์˜ ๊ฐœ์ˆ˜)๋Š” 1 ์ด์ƒ 2,000 ์ดํ•˜์ž…๋‹ˆ๋‹ค.
  • road์˜ ๊ฐ ์›์†Œ๋Š” ๋งˆ์„์„ ์—ฐ๊ฒฐํ•˜๊ณ  ์žˆ๋Š” ๊ฐ ๋„๋กœ์˜ ์ •๋ณด๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.
  • road๋Š” ๊ธธ์ด๊ฐ€ 3์ธ ๋ฐฐ์—ด์ด๋ฉฐ, ์ˆœ์„œ๋Œ€๋กœ (a, b, c)๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.
    • a, b(1 ≤ a, b ≤ N, a != b)๋Š” ๋„๋กœ๊ฐ€ ์—ฐ๊ฒฐํ•˜๋Š” ๋‘ ๋งˆ์„์˜ ๋ฒˆํ˜ธ์ด๋ฉฐ, c(1 ≤ c ≤ 10,000, c๋Š” ์ž์—ฐ์ˆ˜)๋Š” ๋„๋กœ๋ฅผ ์ง€๋‚˜๋Š”๋ฐ ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„์ž…๋‹ˆ๋‹ค.
    • ๋‘ ๋งˆ์„ a, b๋ฅผ ์—ฐ๊ฒฐํ•˜๋Š” ๋„๋กœ๋Š” ์—ฌ๋Ÿฌ ๊ฐœ๊ฐ€ ์žˆ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
    • ํ•œ ๋„๋กœ์˜ ์ •๋ณด๊ฐ€ ์—ฌ๋Ÿฌ ๋ฒˆ ์ค‘๋ณตํ•ด์„œ ์ฃผ์–ด์ง€์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
  • K๋Š” ์Œ์‹ ๋ฐฐ๋‹ฌ์ด ๊ฐ€๋Šฅํ•œ ์‹œ๊ฐ„์„ ๋‚˜ํƒ€๋‚ด๋ฉฐ, 1 ์ด์ƒ 500,000 ์ดํ•˜์ž…๋‹ˆ๋‹ค.
  • ์ž„์˜์˜ ๋‘ ๋งˆ์„๊ฐ„์— ํ•ญ์ƒ ์ด๋™ ๊ฐ€๋Šฅํ•œ ๊ฒฝ๋กœ๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค.
  • 1๋ฒˆ ๋งˆ์„์— ์žˆ๋Š” ์Œ์‹์ ์ด K ์ดํ•˜์˜ ์‹œ๊ฐ„์— ๋ฐฐ๋‹ฌ์ด ๊ฐ€๋Šฅํ•œ ๋งˆ์„์˜ ๊ฐœ์ˆ˜๋ฅผ return ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.




๋ฐ˜์‘ํ˜•