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 | 31 |
Tags
- 해시
- GROUP BY
- 다익스트라
- BFS
- 다이나믹 프로그래밍
- 트리
- 서브쿼리
- 다시
- 재귀
- 우선순위큐
- 브루트포스
- 수학
- MST
- 구현
- 크루스칼
- 플로이드-워셜
- 시뮬레이션
- 그래프 이론
- 그리디
- 자료구조
- join
- 에라토스테네스의 체
- 백트래킹
- DFS
- 그래프 탐색
- DP
- 투포인터
- 다이나믹프로그래밍
- 분할정복
- 누적합
Archives
- Today
- Total
기록하고 까먹지 말기
1753 본문
날짜 : 2022. 12. 08
사용 언어 : python
문제
코드
import sys
import heapq
INF = int(1e9)
v, e = map(int, sys.stdin.readline().split()) # 정점의 개수, 간선의 개수
k = int(sys.stdin.readline()) # 시작정점
graph = [[] for i in range(v + 1)] # 그래프
distance = [INF] * (v + 1) # 시작점 기준 각 노드간 최소거리
for _ in range(e):
a, b, c = map(int, sys.stdin.readline().split()) # 시작, 도착, 길이
graph[a].append((b, c))
def dijkstra(start):
q = []
heapq.heappush(q, (0, start)) # start 로 가기위한 최단거리 0
distance[start] = 0
while q:
d, now = heapq.heappop(q) # now 까지의 최단거리 추출
if distance[now] < d : continue
for i in graph[now]:
cost = d + i[1] # now - 인접노드간 거리
if cost < distance[i[0]]:
distance[i[0]] = cost
heapq.heappush(q, (cost, i[0]))
dijkstra(k)
for i in range(1, v + 1):
if distance[i] == INF: print("INF")
else: print(distance[i])
풀이
- 다익스트라 알고리즘을 활용한 문제였다.
알게된 점
- 아직은 바로바로 알고리즘이 나오는 단계는 아닌듯 하다.
- 계속 문제를 풀어보면서 DFS, BFS처럼 생각하면 바로 꺼낼 수 있도록 연습이 필요해 보인다.
참고 사이트
-
'전공 > 백준' 카테고리의 다른 글
13417 (0) | 2022.12.10 |
---|---|
12845 (0) | 2022.12.08 |
최단경로 응용문제(이코테) - 2 (0) | 2022.12.07 |
최단경로 응용문제(이코테) - 1 (0) | 2022.12.07 |
10974 (0) | 2022.12.07 |