728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ ๐Ÿ˜ต‍๐Ÿ’ซ

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

 

๋‚ด ํ’€๐Ÿฆท

# 1446. ์ง€๋ฆ„๊ธธ
import sys
import heapq
input = sys.stdin.readline
INF = 1e9

n, d = map(int, input().split())
graph = [[] for _ in range(d + 1)]
distance = [INF] * (d + 1)

for i in range(d):
    graph[i].append((i + 1, 1))

# ์ง€๋ฆ„๊ธธ ์žˆ๋Š” ๊ฒฝ์šฐ ์ธ์ ‘ ๋ฆฌ์ŠคํŠธ์— ์ถ”๊ฐ€ํ•ด์ฃผ๊ธฐ
for _ in range(n):
    s, e, l = map(int, input().split())
    # ๋งˆ์ง€๋ง‰ ๋„์ฐฉ์ง€์ ์ด ์‹ค์ œ ๊ณ ์†๋„๋กœ ๋ณด๋‹ค ๋ฉ€๋ฉด ๋„˜๊ธด๋‹ค
    if e > d:
        continue
    graph[s].append((e, l))

# print(graph)
distance[0] = 0
heap = [[0, 0]]
while heap:
    dist, now = heapq.heappop(heap)

    if dist > distance[now]:
        continue

    for i in graph[now]:
        mn_dist = dist + i[1]
        if mn_dist < distance[i[0]]:
            distance[i[0]] = mn_dist
            heapq.heappush(heap, (mn_dist, i[0]))

print(distance[d])

 

์—ญ์ฃผํ–‰์ด ์•ˆ๋˜๋Š”๋ฐ ๋“ค์–ด์˜ค๊ธฐ๋Š” ์‹ค์ œ ๊ณ ์†๋„๋กœ๋ณด๋‹ค ๋จผ๊ณณ๋„ ๋“ค์–ด์˜ค๊ณ ํ•ด์„œ ์–ด๋–ป๊ฒŒ ์ฒ˜๋ฆฌํ•ด์ค˜์•ผ ํ• ์ง€ ๊ณ ๋ฏผ์„ ์˜ค๋ž˜ํ–ˆ๋‹ค... ๊ทธ๋ฆฌ๊ณ  ์ง€๋ฆ„๊ธธ์ด ์žˆ์ง€๋งŒ ๋” ๋ฉ€์–ด์„œ ์ด์šฉ์„ ์•ˆ ํ•˜๋Š” ๊ฒฝ์šฐ๋Š” ์–ด๋–ป๊ฒŒ ์ฒ˜๋ฆฌํ•ด์•ผํ• ๊นŒ ๊ณ ๋ฏผ์„ ํ–ˆ๋‹ค...

 

๊ฐ€์žฅ ๋จผ์ € ๋ชจ๋“  ๋‘ ๊ธธ์ด1๋งŒํผ ๊ฐˆ๋•Œ 1์ด ๊ฑธ๋ฆฐ๋‹ค๊ณ  ์ดˆ๊ธฐํ™”ํ•ด์คฌ๊ณ ... ์ง€๋ฆ„๊ธธ ๊ฐ’์„ ์ž…๋ ฅํ•˜๋ฉด์„œ ์—ญ์ฃผํ–‰์€ ํ•  ์ˆ˜ ์—†์œผ๋ฏ€๋กœ ๋งˆ์ง€๋ง‰ ๊ฐ’์ด ๊ณ ์†๋„๋กœ์˜ ๋์„ ๋„˜์–ด๋ฒ„๋ฆฌ๋Š” ๊ฒฝ์šฐ๋Š” ๋„ฃ์–ด์ฃผ์ง€ ์•Š์•˜๋‹ค... 

๊ทธ๋ฆฌ๊ณ  ๋‚˜๋จธ์ง€๋Š” ์šฐ๋ฆฌ๊ฐ€ ์•„๋Š” ๋‹ค์ต์ŠคํŠธ๋ผ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๊ตฌํ˜„ํ•˜๋ฉด ๋!!! ๐Ÿ˜Š๐Ÿ˜Š๐Ÿ˜Š

 

์ฐธ๊ณ ํ•˜๋ฉด ์ข‹์„ ๊ฐœ๋…๋“ค โœ๏ธ

Dijkstra

 

1. ์ž‘๋™์›๋ฆฌ

  • ๊ฐ„์„  : ์ธ์ ‘ ๋ฆฌ์ŠคํŠธ, ๊ฑฐ๋ฆฌ ๋ฐฐ์—ด : ์ดˆ๊ธฐ๊ฐ’ ๋ฌดํ•œ๋Œ€๋กœ ์„ค์ •, ํž™ ์‹œ์ž‘์  ์ถ”๊ฐ€
  • ํž™์—์„œ ํ˜„์žฌ ๋…ธ๋“œ ๋นผ๋ฉด์„œ, ๊ฐ„์„  ํ†ตํ•  ๋•Œ ๋” ๊ฑฐ๋ฆฌ๊ฐ€ ์งง์•„์ง„๋‹ค๋ฉด ๊ฑฐ๋ฆฌ ๊ฐฑ์‹  ๋ฐ ํž™์— ์ถ”๊ฐ€ํ•ด์ค€๋‹ค
  • ํž™ : ์ตœ๋Œ“๊ฐ’, ์ตœ์†Ÿ๊ฐ’์„ ๋น ๋ฅด๊ฒŒ ์ฐพ์„ ์ˆ˜ ์žˆ๋Š” ์ž๋ฃŒ ๊ตฌ์กฐ

 

2. ๊ธฐ๋ณธ ์ฝ”๋“œ

# ๋”์ต์ŠคํŠธ๋ผ ๊ตฌํ˜„ํ•˜๊ธฐ

# k : ์‹œ์ž‘์ 
# dist : ๊ฑฐ๋ฆฌ๋ฐฐ์—ด

dist[k] = 0 
heapq.heappush(heap, (0, K))

while heap:
	w, y = heapq.heappop(heap)
    if w > dist[v] : continue
    for nw, nv in edge[v]:
    	if dist[nv] > dist[v] + nw:
        	dist[nv] = dist[v] + nw
            heapq.heappush(heap, dist[nv], nv))

 

3,  ์•„์ด๋””์–ด

  • ํ•œ์ ์—์„œ ๋‹ค๋ฅธ ๋ชจ๋“  ์ ์œผ๋กœ์˜ ์ตœ๋‹จ ๊ฒฝ๋กœ > ๋‹ค์ต์ŠคํŠธ๋ผ ์‚ฌ์šฉ
  • ๋ชจ๋“  ์  ๊ฑฐ๋ฆฌ ์ดˆ๊ธฐ๊ฐ’ ๋ฌดํ•œ๋Œ€๋กœ ์„ค์ •
  • ์‹œ์ž‘์  ๊ฑฐ๋ฆฌ 0 ์„ค์ • ๋ฐ ํž™์— ์ถ”๊ฐ€
  • ํž™์—์„œ ํ•˜๋‚˜์”ฉ ๋นผ๋ฉด์„œ ์ˆ˜ํ–‰ํ•  ๊ฒƒ
  • ํ˜„์žฌ ๊ฑฐ๋ฆฌ๊ฐ€ ์ƒˆ๋กœ์šด ๊ฐ„์„ ์„ ๊ฑฐ์น ๋•Œ๋ณด๋‹ค ํฌ๋‹ค๋ฉด ๊ฐฑ์‹ 
  • ์ƒˆ๋กœ์šด ๊ฑฐ๋ฆฌํž™์— ์ถ”๊ฐ€!!!
728x90
๋ฐ˜์‘ํ˜•

+ Recent posts