Skip to content

Commit

Permalink
Merge pull request #254 from AlgoLeadMe/80-tgyuuAn
Browse files Browse the repository at this point in the history
80-tgyuuAn
  • Loading branch information
tgyuuAn authored Nov 16, 2024
2 parents 0eca757 + 8a6748f commit ee3bbc0
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
1 change: 1 addition & 0 deletions tgyuuAn/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,5 @@
| 77차시 | 2024.09.27 | 구현 | <a href="https://school.programmers.co.kr/learn/courses/30/lessons/150366">표 병합</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/247
| 78차시 | 2024.10.06 | 그리디 | <a href="https://school.programmers.co.kr/learn/courses/30/lessons/68646">풍선 터뜨리기</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/250
| 79차시 | 2024.10.12 | 이분 매칭 | <a href="https://www.acmicpc.net/problem/9576">책 나눠주기</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/251
| 80차시 | 2024.11.11 | 다익스트라 | <a href="https://www.acmicpc.net/problem/11779">최소비용 구하기 2</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/254
---
44 changes: 44 additions & 0 deletions tgyuuAn/다익스트라/최소비용 구하기 2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from heapq import *
from collections import defaultdict
import sys

def input(): return sys.stdin.readline().rstrip()

N = int(input())
M = int(input())
bus_info = defaultdict(lambda : defaultdict(lambda : int(1e9)))

for _ in range(M):
n1, n2, cost = map(int,input().split())

if bus_info[n1][n2] > cost:
bus_info[n1][n2] = cost

start, destination = map(int, input().split())
heap = []
heappush(heap, [0, 0, start])

previous = [1 for _ in range(N+1)]
cost = [int(1e9) for _ in range(N+1)]
while heap:
now_cost, previous_node, now_node = heappop(heap)

if cost[now_node] <= now_cost: continue
previous[now_node] = previous_node
cost[now_node] = now_cost

for node in bus_info[now_node]:
temp = now_cost + bus_info[now_node][node]
if cost[node] > temp:
heappush(heap, [temp, now_node, node])

print(cost[destination])

history = []
now_history = destination
while now_history != 0:
history.append(now_history)
now_history = previous[now_history]

print(len(history))
print(" ".join(map(str, history[-1::-1])))

0 comments on commit ee3bbc0

Please sign in to comment.