Skip to content

Commit fe5c8ea

Browse files
committed
[BOJ] #1916. 최소 비용 구하기 / 골드5 / 1시간 / 성공
1 parent bb0ae3b commit fe5c8ea

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import sys
2+
import heapq
3+
input = sys.stdin.readline
4+
5+
# 입력
6+
N = int(input()) # 도시의 개수
7+
M = int(input()) # 버스의 개수
8+
graph = [[] for _ in range(N+1)]
9+
for _ in range(M):
10+
start, end, weight = map(int, input().split())
11+
graph[start].append([end, weight])
12+
13+
# 우리가 구하고자 하는 구간의 출발지와 도착지 번호
14+
goal_s, goal_e = map(int, input().split())
15+
16+
# 최단거리 테이블 초기화
17+
distances = [float('inf')] * (N+1)
18+
distances[goal_s] = 0
19+
heap = []
20+
heapq.heappush(heap, (goal_s, 0)) # 힙(heap)에 (시작노드, 가중치) 추가
21+
22+
while heap:
23+
current, dist = heapq.heappop(heap) # 최소힙
24+
25+
if distances[current] >= dist:
26+
for node, weight in graph[current]:
27+
if dist + weight < distances[node]: # 지금 발견한 게 더 작으면
28+
distances[node] = dist + weight
29+
heapq.heappush(heap, (node, dist + weight))
30+
31+
print(distances[goal_e])

0 commit comments

Comments
 (0)