diff --git a/suhyun113/README.md b/suhyun113/README.md index ac4efaf..163899f 100644 --- a/suhyun113/README.md +++ b/suhyun113/README.md @@ -9,4 +9,5 @@ | 5차시 | 2024.04.10 | 스택 | [크레인 인형 뽑기 게임](https://school.programmers.co.kr/learn/courses/30/lessons/64061) | [#19](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/19) | | 6차시 | 2024.04.14 | 스택 | [컨트롤 제트](https://school.programmers.co.kr/learn/courses/30/lessons/120853) | [#21](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/21) | | 7차시 | 2024.05.09 | 트리 | [원숭이 매달기](https://www.acmicpc.net/problem/2716) | [#31](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/31) | -| 8차시 | 2024.05.14 | 수학 | [어린 왕자](https://www.acmicpc.net/problem/1004) | [#32](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/32) | \ No newline at end of file +| 8차시 | 2024.05.14 | 수학 | [어린 왕자](https://www.acmicpc.net/problem/1004) | [#32](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/32) | +| 9차시 | 2024.05.28 | 다익스트라 | [최소비용 구하기](https://www.acmicpc.net/problem/1916) | [#33](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/33) | \ No newline at end of file diff --git "a/suhyun113/\353\213\244\354\235\265\354\212\244\355\212\270\353\235\274/9-suhyun113.py" "b/suhyun113/\353\213\244\354\235\265\354\212\244\355\212\270\353\235\274/9-suhyun113.py" new file mode 100644 index 0000000..1778f1f --- /dev/null +++ "b/suhyun113/\353\213\244\354\235\265\354\212\244\355\212\270\353\235\274/9-suhyun113.py" @@ -0,0 +1,36 @@ +# 1916 : 최소비용 구하기 + +import heapq + +INF = float('inf') # 최대값 정의 + +N = int(input()) # 도시의 개수(노드) +M = int(input()) # 버스의 개수(에지) + +graph = [[] for _ in range(N+1)] # 그래프 인접 리스트로 초기화(방문하지 않은 노드들) +distance = [INF] * (N+1) # 각 노드까지의 거리 무한대로 초기화 + +for _ in range(M): + x, y, cost = map(int, input().split()) # x -> y 도시로 가는 데 필요한 비용 cost + graph[x].append([y, cost]) # 그래프에 에지 추가 + +start, end = map(int, input().split()) # 출발, 도착 노드 입력 받기 + +# 다익스트라 알고리즘 +def Dijkstra(start): + q = [] # 우선순위 큐 생성 + heapq.heappush(q, [0, start]) # 출발할 도시 큐에 넣기([거리, 노드] 형태) + distance[start] = 0 # 시작 도시의 거리 0으로 초기화 + + while q: # 큐가 빌 때까지 반복 + weight, node = heapq.heappop(q) # 현재 노드까지의 거리, 현재 노드(큐에서 가장 작은 값) + if distance[node] < weight: # 현재 노드가 이미 처리된 노드인지 확인 + continue + for n, w in graph[node]: + cost = w + weight # 현재 노드를 통해 인접 노드까지 가는 새로운 거리 계산 + if distance[n] > cost: # 새로운 거리가 기존에 저장된 거리보다 짧은지 확인 + distance[n] = cost # 최단 거리 갱신 + heapq.heappush(q, [cost, n]) # 인접 노드를 우선순위 큐에 추가 + +Dijkstra(start) +print(distance[end]) \ No newline at end of file