Python/백준

[백준 파이썬] 1753번 최단경로

AI 꿈나무 2021. 4. 21. 14:49
반응형

1753번 최단경로

www.acmicpc.net/problem/1753

 

1753번: 최단경로

첫째 줄에 정점의 개수 V와 간선의 개수 E가 주어진다. (1≤V≤20,000, 1≤E≤300,000) 모든 정점에는 1부터 V까지 번호가 매겨져 있다고 가정한다. 둘째 줄에는 시작 정점의 번호 K(1≤K≤V)가 주어진다.

www.acmicpc.net

 

문제

 

파이썬 풀이

 다익스트라 알고리즘을 활용하여 최단 경로를 찾습니다.

 다익스트라 알고리즘은 1) 모든 가중치가 양수, 2) 방향 그래프 인 경우 활용할 수 있습니다.

 가중치가 음수인 경우는 벨만포드 알고리즘을 사용합니다.

import sys
import collections
import heapq

sys.setrecursionlimit(10**6)
input = sys.stdin.readline

V, E = map(int,input().split()) # 정점 수, 간선 수
start = int(input()) # 시작 정점
graph = collections.defaultdict(list) # 빈 그래프 생성

for _ in range(E):
    u, v, w = map(int,input().split()) # (정점, 다음 정점, 가중치)
    graph[u].append((v,w)) # 그래프 생성

# 다익스트라 알고리즘
def daikstra(graph, V, start):
    # input: graph, V:정점 수, start: 시작 정점
    Q = [(0, start)] # 우선순위 큐 생성, (거리, 정점)
    distance = collections.defaultdict(int) # 거리 정보를 담을 dict 생성

    while Q: # Q가 빌때 까지 반복
        dist, node = heapq.heappop(Q) # 거리, 정점 추출
        if node not in distance: # 탐색하지 않은 node이면 탐색 진행
            distance[node] = dist # 거리 정보 갱신
            for v, w in graph[node]: # 인접 노드로 이동
                update = dist + w # 거리 정보 갱신
                heapq.heappush(Q, (update, v)) # 우선순위 큐에 삽입

    for i in range(1,V+1):
        if i == start: # 시작 정점은 거리 0 출력
            print('0')
        elif not distance[i]: # 경로가 존재하지 않은 경우
            print('INF')
        else: # 최단 경로가 존재하면
            print(distance[i])

daikstra(graph, V, start)
반응형