-
알고리즘: 백준 1753번 최단경로 (feat.python)알고리즘/백준(BaekJoon) 2021. 1. 11. 15:25
1753번: 최단경로
첫째 줄에 정점의 개수 V와 간선의 개수 E가 주어진다. (1≤V≤20,000, 1≤E≤300,000) 모든 정점에는 1부터 V까지 번호가 매겨져 있다고 가정한다. 둘째 줄에는 시작 정점의 번호 K(1≤K≤V)가 주어진다.
www.acmicpc.net
import sys import heapq read = sys.stdin.readline N = int(read()) M = int(read()) graph = {i:[] for i in range(1, N+1)} for i in range(1, M+1): s, e, v = map(int, read().split()) graph[s].append([e,v]) start, end = map(int, read().split()) INF = sys.maxsize dist = [0] + [INF for _ in range(N)] dist[start] = 0 hq = list() heapq.heappush(hq, [0, start]) while hq: current_weight, current_node = heapq.heappop(hq) for near_node, near_weight in graph[current_node]: next_weight = near_weight + current_weight if next_weight < dist[near_node]: dist[near_node] = next_weight heapq.heappush(hq, [dist[near_node], near_node]) print(dist[end])
반응형'알고리즘 > 백준(BaekJoon)' 카테고리의 다른 글
알고리즘: 백준 1238번 파티 (feat. python) (0) 2021.01.11 알고리즘: 백준 1261번 알고스팟 (feat.python) (0) 2021.01.11 알고리즘: 백준 11724번 연결 요소의 개수 (feat.python) (0) 2021.01.08 알고리즘: 백준 1012번 유기농 배추 (feat. python) (0) 2021.01.07 알고리즘: 백준 1260번 DFS와 BFS (feat.python) (0) 2021.01.07