Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |
Tags
- 그리디
- 프로그래머스
- python
- 가상메모리
- BFS
- level1
- level2
- 다이나믹 프로그래밍
- 수학
- 딕셔너리
- 파이썬
- dfs
- BOJ
- 운영체제
- 코딩테스트
- MYSQL
- 재귀
- 힙
- 브루트포스
- level0
- programmers
- level3
- dict
- 가상메모리 관리
- 스택
- N과M
- 다익스트라
- 구현
- 백준
- DP
Archives
- Today
- Total
동캄의 코딩도장
백준 14284 [간선 이어가기2] 파이썬 본문
https://www.acmicpc.net/problem/14284
# 백준 14284 간선 이어가기2
import sys
import heapq
from collections import defaultdict
input = sys.stdin.readline
N, M = map(int, input().split())
graph = defaultdict(list)
for _ in range(M):
a, b, cost = map(int, input().split())
graph[a].append([b, cost])
graph[b].append([a, cost])
def dijkstra(graph, start):
distances = {node: float('inf') for node in range(1, N+1)}
distances[start] = 0
queue = []
heapq.heappush(queue, [distances[start], start])
while queue:
curr_dist, curr_dest = heapq.heappop(queue)
if distances[curr_dest] < curr_dist:
continue
for new_dest, new_dist in graph[curr_dest]:
dist = curr_dist+new_dist
if dist < distances[new_dest]:
distances[new_dest] = dist
heapq.heappush(queue, [dist, new_dest])
return distances
start, end = map(int, input().split())
distances = dijkstra(graph, start)
print(distances[end])
다익스트라를 이용한 문제이다.
'코테 > BOJ' 카테고리의 다른 글
백준 9935 [문자열 폭발] 파이썬 (0) | 2022.05.17 |
---|---|
백준 11909 [배열 탈출] 파이썬 (0) | 2022.05.17 |
백준 17396 [백도어] 파이썬 (0) | 2022.05.17 |
백준 5972 [택배 배송] 파이썬 (0) | 2022.05.17 |
백준 1753 [최단경로] 파이썬 (0) | 2022.05.17 |