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
- MYSQL
- BOJ
- python
- 프로그래머스
- 구현
- 브루트포스
- 운영체제
- 백준
- 그리디
- level0
- N과M
- 파이썬
- 스택
- 다이나믹 프로그래밍
- 딕셔너리
- dict
- DP
- 코딩테스트
- 힙
- 가상메모리 관리
- 다익스트라
- dfs
- 가상메모리
- programmers
- level3
- 수학
- BFS
- level1
- level2
- 재귀
Archives
- Today
- Total
동캄의 코딩도장
백준 5972 [택배 배송] 파이썬 본문
https://www.acmicpc.net/problem/5972
5972번: 택배 배송
농부 현서는 농부 찬홍이에게 택배를 배달해줘야 합니다. 그리고 지금, 갈 준비를 하고 있습니다. 평화롭게 가려면 가는 길에 만나는 모든 소들에게 맛있는 여물을 줘야 합니다. 물론 현서는
www.acmicpc.net
# 백준 5972 택배배송
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):
distance = {node: float('inf') for node in range(1, N+1)}
distance[start] = 0
queue = []
heapq.heappush(queue, [distance[start], start])
while queue:
current_distance, current_destination = heapq.heappop(queue)
if distance[current_destination] < current_distance:
continue
for new_destination, new_distance in graph[current_destination]:
dist = new_distance+current_distance
if dist < distance[new_destination]:
distance[new_destination] = dist
heapq.heappush(queue, [dist, new_destination])
return distance
distance = dijkstra(graph, 1)
print(distance[N])
다익스트라를 이용한 문제이다.
'코테 > BOJ' 카테고리의 다른 글
백준 14284 [간선 이어가기2] 파이썬 (0) | 2022.05.17 |
---|---|
백준 17396 [백도어] 파이썬 (0) | 2022.05.17 |
백준 1753 [최단경로] 파이썬 (0) | 2022.05.17 |
백준 9019 [DSLR] 파이썬 (0) | 2022.04.18 |
백준 16236 [아기 상어] 파이썬 (0) | 2022.04.17 |