๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
  • What would life be If we had no courage to attemp anything?
๐“๐จ๐๐š๐ฒ ๐ˆ ๐‹๐ž๐š๐ซ๐ง

๐“๐จ๐๐š๐ฒ ๐ˆ ๐‹๐ž๐š๐ซ๐ง 2022.03.31.๋ชฉ

by DevIseo 2022. 3. 31.

Today I Learn (20220331)


์˜ค๋Š˜์€ ๋ฌธ์ œ๋ฅผ ํ•˜๋‚˜๋„ ์•„์ง ๋‹ค ๋ชปํ’€์—ˆ๋‹ค... ๊ทธ๋ ‡์ง€๋งŒ ์˜ค๋Š˜์€ ์–ด์ œ์— ์ด์–ด ์šฐ์„  ์ˆœ์œ„ ํ์™€ ๋‹ค์ต์ŠคํŠธ๋ผ์— ๋Œ€ํ•ด ๋ฐฐ์› ๋‹ค. ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ์„ธ๊ณ„๋Š” ๋ฐฐ์šฐ๋ฉด ๋ฐฐ์šธ์ˆ˜๋ก ๋ฌด๊ถ๋ฌด์ง„ ํ•œ๊ฑฐ ๊ฐ™๋‹ค...ใ… ใ…  ์–ด์ œ ์ข€.. ๋Šฆ๊ฒŒ์žค๋”๋‹ˆ ์ปจ๋””์…˜์ด ๋งค์šฐ ์ €์กฐํ–ˆ์ง€๋งŒ ์ ๊ฒŒ ์ž”๊ฑฐ ์น˜๊ณ  ์ˆ˜์—… ์ •๋ง ์—ด์‹ฌํžˆ ๋“ค์—ˆ๋‹ค. ๋ณต์Šต๋งŒ์ด ์‚ด๊ธธ,, ๊ทผ๋ฐ ๋‹ค์Œ์ฃผ ๊ณผ๋ชฉํ‰๊ฐ€๊ฐ€ ๋งค์šฐ ๊ฑฑ์ •๋œ๋‹ค. ์ฃผ๋ง์— ์ตœ๋Œ€ํ•œ ๋ฌธ์ œ๋ฅผ ๋งŽ์ด ํ’€์–ด๋ณด๊ณ  ์ด์ œ๊นŒ์ง€ ๋ฐฐ์šด ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ํ‚ค์›Œ๋“œ๋ฅผ ๋˜์„ธ๊ฒจ ๋ณด๋Š” ์‹œ๊ฐ„์„ ๊ฐ–๋„๋ก ํ•ด์•ผ๊ฒ ๋‹ค. ๋‹ค์ต์ŠคํŠธ๋ผ๋ฅผ ๊ทธ๋ฆผ์œผ๋กœ ๊ทธ๋ ค๋ณด๋‹ˆ๊นŒ ์กฐ๊ธˆ์”ฉ ์•Œ ๊ฑฐ ๊ฐ™๋‹ค. ์ด์ œ ์ด๋ฅผ ์ฝ”๋“œ์™€ ์ ‘๋ชฉ์‹œ์ผœ ์ฝ”๋“œ๋กœ ๊ตฌํ˜„ํ•˜๊ณ  ๋‹ค์Œ์ฃผ์— ๋‹ค์ต์ŠคํŠธ๋ผ ๋ฌธ์ œ๋“ค์„ ์—ฌ๋Ÿฌ๊ฐœ ํ’€์–ด์„œ ๋‚ด ๊ฒƒ์œผ๋กœ ๋งŒ๋“ค์–ด์•ผ๊ฒ ๋‹ค.

์šฐ์„  ์ˆœ์œ„ ํ


**ํ(Queue)**๋Š” ์„ ์ž…์„ ์ถœ ํ˜•์‹์˜ ์ž๋ฃŒ๊ตฌ์กฐ.

**์šฐ์„ ์ˆœ์œ„ ํ(Priority Queue)**๋Š” ๋จผ์ € ๋“ค์–ด์˜ค๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ์•„๋‹ˆ๋ผ, ์šฐ์„  ์ˆœ์œ„๊ฐ€ ๋†’์€ ๋ฐ์ดํ„ฐ๊ฐ€ ๋จผ์ € ๋‚˜๊ฐ€๋Š” ํ˜•ํƒœ์˜ ์ž๋ฃŒ๊ตฌ์กฐ.

์ด๋ฅผ ์šฐ๋ฆฌ๋Š” **ํž™(Heap)**์„ ์ด์šฉํ•˜์—ฌ ๊ตฌํ˜„.

Min heap


# insert๊ณผ์ • -- headq๋Š” default๊ฐ’์ด Min heap์ด๋‹ค.
import heapq
arr= []
heapq.heappush(arr,1)
heapq.heappush(arr,3)
heapq.heappush(arr,4)
print(arr)

#top() and pop()๊ณผ์ • -- list์— ์žˆ๋Š”๊ฑฐ ๋ณด๋‹ค ๋งŽ์ด ์‹คํ–‰์‹œ ์˜ค๋ฅ˜
print(heaq.headpop(arr))
print(heaq.headpop(arr))
print(heaq.headpop(arr))

#tuple์„ ํ†ตํ•œ ์šฐ์„ ์ˆœ์œ„ ์ง€์ •
import heapq
arr=[]
heapq.heappush(arr,(1,'hi'))
heapq.heappush(arr,(3,'am'))
heapq.heappush(arr,(2,'i'))
heapq.heappush(arr,(4,'iseo'))

for i in range(len(arr)):
    print(heapq.heappop(arr)[1], end=' ')
# ๋ฆฌ์ŠคํŠธ๋ฅผ heap์˜ ์ž๋ฃŒ์šฉ์œผ๋กœ ํ•œ๋ฒˆ์— ๋ณ€ํ™˜ํ•˜๊ธฐ
import heapq

arr=[1232,5,3,1,5]
heapq.heapify(arr)
#heap์˜ ์ž๋ฃŒํ˜•์ด๋‹ˆ๊นŒ ๋ฃจํˆฌ๋…ธ๋“œ์— ๊ฐ’์€ ๊ฐ€์žฅ ์ž‘์€ ๊ฐ’..... 
#๋‚˜๋จธ์ง€๋Š” ๊ฑ ์ •๋ ฌ ์•ˆ๋จ
print(*arr)

for i in range(len(arr)):
    print(heapq.heappop(arr))

Max heap


  1. ํŠœํ”Œ์„ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•
  2. ์Œ์ˆ˜(-)๋ฅผ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•(๋žŒ๋‹ค ์‚ฌ์šฉ ๊ฐ€๋Šฅ)
  1. ํŠœํ”Œ์„ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•
import heapq
#Max heap๋งŒ๋“ค๊ธฐ
arr = []
#ํŠœํ”Œ์— ์šฐ์„ ์ˆœ์œ„๋ฅผ ์ด์šฉํ•ด ์ถœ๋ ฅํ•˜๋Š” ๋ฐฉ๋ฒ•
heapq.heappush(arr,(-1,1))
heapq.heappush(arr,(-3,3))
heapq.heappush(arr,(-8,8))

for i in range(len(arr)):
    print(heapq.heappop(arr)[1])
  1. ์Œ์ˆ˜(-)๋ฅผ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•
import heapq
#์Œ์ˆ˜๋ฅผ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•
#์šฐ๋ฆฌ๊ฐ€ ์ถœ๋ ฅํ•  ๊ฐ’์— - ๋ถ™์—ฌ push
#popํ•  ๋•Œ -๋ฅผ ๋ถ™์—ฌ ๋‹ค์‹œ ์–‘์ˆ˜๋กœ ๋‚˜์˜ค๊ฒŒ ํ•จ.
arr=[]
heapq.heappush(arr,-1)
heapq.heappush(arr,-4)
heapq.heappush(arr,-7) #-7์ด ๊ฐ€์žฅ ๋จผ์ € ๋‚˜์˜จ๋‹ค

for i in range(len(arr)):
	print(-heapq.heappop(arr)) #๊ฐ€์žฅ ๋จผ์ € ๋‚˜์˜ค๋Š” -7์— ๋‹ค์‹œ -๋ฅผ ๋ถ™์—ฌ ์–‘์ˆ˜ ํ˜•ํƒœ๋กœ ์ถœ๋ ฅ

priority queue๋ฅผ ์ด์šฉํ•ด max heap์ถœ๋ ฅํ•˜์‹œ์˜ค.


import heapq

arr=[11,12,6,8,10,4]
maxheap=[]
for i in range(len(arr)):
    heapq.heappush((maxheap,(-arr[i],arr[i]))
                   
for i in range(len(arr)):
	print(heapq.heappop(maxheap)[1])

๋žŒ๋‹ค์‹ ์ด์šฉ


import heapq
arr=[11,12,6,8,10,4]
rev=lambda x:x*-1
#arr๊ฐ’์— ์Œ์ˆ˜๋ฅผ ๋ถ™์—ฌ์„œ maxheap ๋ฆฌ์ŠคํŠธ ๋งŒ๋“  ํ›„
maxheap = list(map(rev,arr))
#heap์˜ ์ž๋ฃŒํ˜•์œผ๋กœ ๋ฐ”๊พธ๊ณ 
heapq.heapify(maxheap)

#์Œ์ˆ˜ ๋ถ™์—ฌ์„œ ์ถœ๋ ฅํ•˜๊ธฐ
for i in range(len(maxheap)):
    print(-heapq.heappop(maxheap))

์—ฐ์Šต๋ฌธ์ œ

์นด๋“œ ์ •๋ ฌํ•˜๊ธฐ

์ •๋ ฌ๋œ ๋‘ ๋ฌถ์Œ์˜ ์ˆซ์ž ์นด๋“œ๊ฐ€ ์žˆ๋‹ค๊ณ  ํ•˜์ž. ๊ฐ ๋ฌถ์Œ์˜ ์นด๋“œ์˜ ์ˆ˜๋ฅผ A, B๋ผ ํ•˜๋ฉด ๋ณดํ†ต ๋‘ ๋ฌถ์Œ์„ ํ•ฉ์ณ์„œ ํ•˜๋‚˜๋กœ ๋งŒ๋“œ๋Š” ๋ฐ์—๋Š” A+B ๋ฒˆ์˜ ๋น„๊ต๋ฅผ ํ•ด์•ผ ํ•œ๋‹ค. ์ด๋ฅผํ…Œ๋ฉด, 20์žฅ์˜ ์ˆซ์ž ์นด๋“œ ๋ฌถ์Œ๊ณผ 30์žฅ์˜ ์ˆซ์ž ์นด๋“œ ๋ฌถ์Œ์„ ํ•ฉ์น˜๋ ค๋ฉด 50๋ฒˆ์˜ ๋น„๊ต๊ฐ€ ํ•„์š”ํ•˜๋‹ค.

๋งค์šฐ ๋งŽ์€ ์ˆซ์ž ์นด๋“œ ๋ฌถ์Œ์ด ์ฑ…์ƒ ์œ„์— ๋†“์—ฌ ์žˆ๋‹ค. ์ด๋“ค์„ ๋‘ ๋ฌถ์Œ์”ฉ ๊ณจ๋ผ ์„œ๋กœ ํ•ฉ์ณ๋‚˜๊ฐ„๋‹ค๋ฉด, ๊ณ ๋ฅด๋Š” ์ˆœ์„œ์— ๋”ฐ๋ผ์„œ ๋น„๊ต ํšŸ์ˆ˜๊ฐ€ ๋งค์šฐ ๋‹ฌ๋ผ์ง„๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด 10์žฅ, 20์žฅ, 40์žฅ์˜ ๋ฌถ์Œ์ด ์žˆ๋‹ค๋ฉด 10์žฅ๊ณผ 20์žฅ์„ ํ•ฉ์นœ ๋’ค, ํ•ฉ์นœ 30์žฅ ๋ฌถ์Œ๊ณผ 40์žฅ์„ ํ•ฉ์นœ๋‹ค๋ฉด (10 + 20) + (30 + 40) = 100๋ฒˆ์˜ ๋น„๊ต๊ฐ€ ํ•„์š”ํ•˜๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ 10์žฅ๊ณผ 40์žฅ์„ ํ•ฉ์นœ ๋’ค, ํ•ฉ์นœ 50์žฅ ๋ฌถ์Œ๊ณผ 20์žฅ์„ ํ•ฉ์นœ๋‹ค๋ฉด (10 + 40) + (50 + 20) = 120 ๋ฒˆ์˜ ๋น„๊ต๊ฐ€ ํ•„์š”ํ•˜๋ฏ€๋กœ ๋œ ํšจ์œจ์ ์ธ ๋ฐฉ๋ฒ•์ด๋‹ค.

N๊ฐœ์˜ ์ˆซ์ž ์นด๋“œ ๋ฌถ์Œ์˜ ๊ฐ๊ฐ์˜ ํฌ๊ธฐ๊ฐ€ ์ฃผ์–ด์งˆ ๋•Œ, ์ตœ์†Œํ•œ ๋ช‡ ๋ฒˆ์˜ ๋น„๊ต๊ฐ€ ํ•„์š”ํ•œ์ง€๋ฅผ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.


์ž…๋ ฅ
์ฒซ์งธ ์ค„์— N์ด ์ฃผ์–ด์ง„๋‹ค. (1 ≤ N ≤ 100,000) ์ด์–ด์„œ N๊ฐœ์˜ ์ค„์— ๊ฑธ์ณ ์ˆซ์ž ์นด๋“œ ๋ฌถ์Œ์˜ ๊ฐ๊ฐ์˜ ํฌ๊ธฐ๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์ˆซ์ž ์นด๋“œ ๋ฌถ์Œ์˜ ํฌ๊ธฐ๋Š” 1,000๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์–‘์˜ ์ •์ˆ˜์ด๋‹ค.


์ถœ๋ ฅ
์ฒซ์งธ ์ค„์— ์ตœ์†Œ ๋น„๊ต ํšŸ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

์ž…๋ ฅ
4
50
20
70
30

์ถœ๋ ฅ
320


์ž…๋ ฅ
3
10
20
40

์ถœ๋ ฅ
100
import heapq

n=int(input())
card=[]

#์ž…๋ ฅ ๋ฐ›์€ ์นด๋“œ insert
for i in range(n):
    heapq.heappush(card,int(input()))
    
answer = 0
while len(card)>1: #์นด๋“œ ๋ฌถ์Œ์˜ ๊ฐœ์ˆ˜๊ฐ€ 1๊ฐœ๊ฐ€ ๋  ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณต
    
    #๊ฐ€์žฅ ์ž‘์€ ๋ฌถ์Œ ์นด๋“œ ๋นผ์˜ค๊ณ 
    temp1=heapq.heappop(card)
    #๊ทธ๋‹ค์Œ ์ž‘์€ ์นด๋“œ ๋ฌถ์Œ ๋นผ์˜ค๊ธฐ
    temp2=heapq.heappop(card)
    #๋‘๊ฐœ๋ฅผ ํ•ฉํ•œ ๊ฒƒ์ด ๋น„๊ต ํšŸ์ˆ˜. ์ด ๋น„๊ตํšŸ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” answer๋ณ€์ˆ˜
    answer+=(temp1+temp2)
    #๋‹ค์‹œ ํ•œ ๋ฌถ์Œ์ด ๋œ ์นด๋“œ pushํ•˜๊ธฐ
    heapq.heappush(card,temp1+temp2)
print(answer)
    
    

๋‹ค์ต์ŠคํŠธ๋ผ(Dijkstra) ์•Œ๊ณ ๋ฆฌ์ฆ˜


  1. ํ•œ ์ •์ ์—์„œ ๋‹ค๋ฅธ ๋ชจ๋“  ๋„์ฐฉ์ ๊นŒ์ง€ ๋“œ๋Š” ์ตœ์†Œ ๋น„์šฉ ๋˜๋Š” ์ตœ๋‹จ ๊ฑฐ๋ฆฌ ๊ตฌํ•  ๋•Œ ์ด์šฉ! - ํšจ์œจ์„ฑ ๋†’์—ฌ์ฃผ๊ณ  ๋น ๋ฅธ ์†๋„.
  2. ๊ฐ„์„ ์˜ ์ •๋ณด๋Š” ๋ชจ๋‘ ์–‘์ˆ˜์—ฌ์•ผํ•จ(๊ฐ€์ค‘์น˜๊ฐ€ ์–‘์ˆ˜์—ฌ์•ผ ํ•จ)
  3. โ€ป ์Œ์ˆ˜๊ฐ€ ์žˆ๋‹ค๋ฉด bellman -ford์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ด์šฉ
  4. ์–‘๋ฐฉํ–ฅ, ๋‹จ๋ฐฉํ–ฅ ๊ฐ€๋Šฅ

โ€ป Floyd washall์€ ๋ชจ๋“  ์ •์ ์—์„œ ๋‹ค๋ฅธ ๋ชจ๋“  ์ •์ ๊นŒ์ง€์˜ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ ๊ตฌํ•  ๋•Œ ์‚ฌ์šฉ

์ด์šฉ ๋ฐฉ๋ฒ•


์›๋ฆฌ๋Š” ๊ฐ„๋‹จํ•˜๋‹ค.

์‹œ์ž‘-> ๋„์ฐฉ vs ์‹œ์ž‘์ ->๊ฒฝ์œ ์ง€->๋„์ฐฉ ์ด ๋‘˜ ์ค‘ ์ž‘์€ ๊ฐ’์œผ๋กœ result๋ฅผ ๊ฐฑ์‹ 

  1. ์ด์ค‘ for๋ฌธ์„ ์ด์šฉ (O(n^2)) - ์ธ์ ‘ ํ–‰๋ ฌ ์ด์šฉ
  2. Priority Queue ์ด์šฉ (O(nlogn)) - ์ธ์ ‘ ๋ฆฌ์ŠคํŠธ ์ด์šฉ

์ด์ค‘ for๋ฌธ์„ ์ด์šฉ (O(n^2)) - ์ธ์ ‘ ํ–‰๋ ฌ ์ด์šฉ

๋‹ค์ต์ŠคํŠธ๋ผ ์ธ์ ‘ํ–‰๋ ฌ ์ด์šฉ ์‹œ ๋กœ์ง

name='ABCDE'
inf=99999
arr=[
    [0, 3, inf, 9, 5],
    [inf, 0, 7, inf, 1],
    [inf, inf, 0, 1, inf],
    [inf, inf, inf, 0, inf],
    [inf, inf, 1, inf, 0]]

used=[0]*5
used[0]=1  # ์‹œ์ž‘์  A์— ํ•ด๋‹นํ•˜๋Š” used๋ฐฐ์—ด์— 1 ์ฒดํฌ
result=[0, 3, inf, 9, 5]

def select_ky():
    minn=99999
    minindex=0
    for i in range(5): # result ๋ฐฐ์—ด์„ ํƒ์ƒ‰ํ•˜๋Š”๋ฐ
        if used[i]==0 and result[i]<minn:  # ๊ฐ’์ด ๊ฐ€์žฅ ์ž‘๊ณ 
            minn=result[i]                 # ๊ฒฝ์œ ์ง€๋กœ ํƒํ•œ์ ์ด ์—†๋Š” ์ •์ ์ด๋ผ๋ฉด
            minindex=i
    return minindex


def dijk():
    for i in range(4):
        via=select_ky() # ๊ฒฝ์œ ์ง€๋ฅผ ์ •ํ•ด์„œ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜
        used[via]=1 # ๊ฒฝ์œ ์ง€๋กœ ํƒํ•œ ๊ณณ์˜ used๋ฐฐ์—ด๊ฐ’์„ 1๋กœ ์ฒดํฌ

        for j in range(5):
            baro=result[j] # ์‹œ์ž‘์ ->๋‹ค๋ฅธ์ •์ (๋„์ฐฉ์ง€)
            kyung=result[via]+arr[via][j] # (์‹œ์ž‘->๊ฒฝ์œ ) + (๊ฒฝ์œ ->๋‹ค๋ฅธ์ •์ (๋„์ฐฉ์ง€))
            if baro>kyung: # ๊ฒฝ์œ ์ง€ ๋“ค๋ ธ๋‹ค ๊ฐ€๋Š”๊ฒƒ์ด ๋” ์ €๋ ดํ•˜๋ฉด
                result[j]=kyung # result ๋ฐฐ์—ด ๊ฐฑ์‹ 

dijk()

print(*result)

Priority Queue ์ด์šฉ (O(nlogn)) - ์ธ์ ‘ ๋ฆฌ์ŠคํŠธ ์ด์šฉ

๋‹ค์ต์ŠคํŠธ๋ผ ์ธ์ ‘๋ฆฌ์ŠคํŠธ ์ด์šฉ ์‹œ ๋กœ์ง

#์ž…๋ ฅ์˜ˆ์ œ ์ž…๋‹ˆ๋‹ค

5
7
0 1 3
0 3 9
0 4 5
1 2 7
1 4 1
2 3 1
4 2 1
0 2

์ถœ๋ ฅ์˜ˆ์ œ ์ž…๋‹ˆ๋‹ค.
5
5 # ์ •์ ์˜ ๊ฐœ์ˆ˜
7 # ๊ฐ„์„ ์˜ ์ •๋ณด
0 1 3
0 3 9 # ์‹œ์ž‘์ง€ ๋„์ฐฉ์ง€ ๋น„์šฉ
0 4 5
1 2 7
1 4 1
2 3 1
4 2 1
0 3 # ์‹œ์ž‘์ง€ ๋„์ฐฉ์ง€


import heapq
n = int(input()) # ์ •์ ์˜ ๊ฐœ์ˆ˜
m = int(input()) # ๊ฐ„์„ ์˜ ์ •๋ณด
graph = [[] for _ in range(n)]

for _ in range(m):
    a, b, c = map(int, input().split()) # ์‹œ์ž‘์  # ๋„์ฐฉ์  # ๋น„์šฉ
    graph[a].append((b, c))

start, target = map(int, input().split())  # ์‹œ์ž‘์ (start) ์œผ๋กœ ๋ถ€ํ„ฐ ๋„์ฐฉ์ (target) ๊นŒ์ง€ ์ตœ์†Œ ๋น„์šฉ ๊ตฌํ•˜๊ธฐ
inf = int(21e8)
result = [inf] * n




def dijkstra(start):
    result[start] = 0   # ์‹œ์ž‘์  start ์— ํ•ด๋‹นํ•˜๋Š” result ๊ฐ’์„ 0   A ๋ฅผ ๊ฒฝ์œ ์ง€๋กœ ์ฒ˜์Œ์— ์…‹ํŒ…
    heap = []
    heapq.heappush(heap, (0, start))

    while heap:
        si_ky_cost, kyungyou = heapq.heappop(heap)


        if si_ky_cost > result[kyungyou]: # ์•ž ๊ณผ์ •์—์„œ ๊ฐฑ์‹ ๋œ ์‹œ์ž‘์ ์—์„œ ๊ฒฝ์œ ์ง€ ๊นŒ์ง€์˜ result ๊ฐ’๋ณด๋‹ค
            continue                      # ๋ฐฉ๊ธˆ ๋ฝ‘์•„๋‚ธ ์‹œ์ž‘์ ์—์„œ ๊ฒฝ์œ ์ง€ ๊นŒ์ง€์˜ ๋น„์šฉ์ด ๋” ํฌ๋ฉด
                                          # ๋„˜๊ฒจ๋ผ ์ด๋ฏธ ๊ฒฝ์œ ์ง€๋กœ ํƒํ•ด์„œ ์—…๋Žƒ๋œ ๊ณณ์ด๋‹ค


        #si_ky_do_cost ( ์‹œ์ž‘->๊ฒฝ์œ  + ๊ฒฝ์œ -> ๋„์ฐฉ )   vs    result[i[0]]  (์‹œ์ž‘์  -> ๋„์ฐฉ) ํ•  ๊ฒƒ์ด๋‹ค.
        for i in graph[kyungyou]:
            si_ky_do_cost = si_ky_cost + i[1]  # ์‹œ์ž‘์ ์—์„œ ๊ฒฝ์œ ์ง€ ๊ฐ”๋‹ค๊ฐ€ ๋„์ฐฉ์  ๊ฐ€๋Š” ๋น„์šฉ๋ถ€ํ„ฐ ๊ตฌํ•จ.

            if si_ky_do_cost < result[i[0]]:     #  ๋น„๊ตํ›„ ๊ฒฝ์œ ์ง€ ๋“ค๋ ธ๋‹ค ๊ฐ€๋Š”๊ฒƒ์ด ๋” ์ €๋ ดํ•˜๋ฉด
                result[i[0]] = si_ky_do_cost     #  result ๋ฐฐ์—ด ๊ฐฑ์‹ 
                heapq.heappush(heap, (si_ky_do_cost, i[0])) # heap์— ๊ฐฑ์‹ ๋œ!! ๋‚ด์šฉ push ํ•˜๊ธฐ

dijkstra(start)
print(result[target])

๋Œ“๊ธ€