https://www.acmicpc.net/problem/1916
1916번: 최소비용 구하기
첫째 줄에 도시의 개수 N(1 ≤ N ≤ 1,000)이 주어지고 둘째 줄에는 버스의 개수 M(1 ≤ M ≤ 100,000)이 주어진다. 그리고 셋째 줄부터 M+2줄까지 다음과 같은 버스의 정보가 주어진다. 먼저 처음에는 그
www.acmicpc.net
풀이 과정
N개의 도시, M개의 버스가 주어지고 버스의 출발점과 도착지점, 그리고 드는 버스 비용이 주어질 때, A번째 도시에서 B번째 도시로 가는 경로의 최소 버스 비용을 구하는 문제이다.
https://greentea31.tistory.com/205
LeetCode - 743. Network Delay Time [Python]
https://leetcode.com/problems/network-delay-time/ Network Delay Time - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared f..
greentea31.tistory.com
이 문제와 비슷한 조건의 문제이다. 똑같이 다익스트라 알고리즘을 구현해서 문제를 해결할 수 있다.
소스 코드
import sys
import heapq
import collections
N = int(sys.stdin.readline())
M = int(sys.stdin.readline())
graph = collections.defaultdict(list)
dist = collections.defaultdict(int)
for _ in range(M):
depart, arrive, cost = map(int, sys.stdin.readline().split())
graph[depart].append((cost, arrive))
start, end = map(int, sys.stdin.readline().split())
Q = [[0, start]]
while Q:
cost, node = heapq.heappop(Q)
if node not in dist:
dist[node] = cost
for w, v in graph[node]:
temp = cost + w
heapq.heappush(Q, [temp, v])
print(dist[end])
'알고리즘 문제 풀이 > 백준' 카테고리의 다른 글
백준 1043 - 거짓말 [Python] (0) | 2022.11.23 |
---|---|
백준 11725 - 트리의 부모 찾기 [파이썬] (0) | 2022.09.20 |
백준 25559 - 패스 [Python] (0) | 2022.09.10 |
백준 25565 - 딸기와 토마토 [Python] (0) | 2022.09.10 |
백준 25562 - 차의 개수 [Python] (0) | 2022.09.07 |