250x250
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
- shared lock
- 지연로딩
- fetch
- 비관적락
- dfs
- 스프링 폼
- execute
- PS
- exclusive lock
- 백트래킹
- 다대다
- 데코레이터
- FetchType
- 동적sql
- 연관관계
- 낙관적락
- 이진탐색
- CHECK OPTION
- 다대일
- BOJ
- JPQL
- SQL프로그래밍
- 유니크제약조건
- querydsl
- 즉시로딩
- 일대다
- 힙
- eager
- 스토어드 프로시저
- 연결리스트
Archives
- Today
- Total
흰 스타렉스에서 내가 내리지
다익스트라 알고리즘 경로 역추적 본문
728x90
https://www.acmicpc.net/problem/11779
from collections import defaultdict
from heapq import heappop, heappush
N = int(input())
M = int(input())
graph = defaultdict(list)
for _ in range(M):
a, b, c = map(int, input().split())
graph[a].append((b, c))
start_point, end_point = map(int, input().split())
distance = [1e9] * (N+1)
distance[start_point] = 0
q = []
heappush(q, (0, start_point, [start_point]))
# 경로 추적을 위한 배열
route = [-1] * (N+1)
route[start_point] = start_point # 도착지점은 자기자신
while q:
dist, now, path = heappop(q)
if distance[now] < dist:
continue
for b, c in graph[now]:
cost = c + dist
if distance[b] > cost:
distance[b] = cost
route[b] = now # 이전 정점 기억
heappush(q, (cost, b, path + [b]))
cnt = 0
path = []
def trace(node):
global cnt
cnt += 1
if route[node] == node:
path.append(node)
return
trace(route[node])
path.append(node)
trace(end_point)
print(distance[end_point])
print(cnt)
print(*path)
'Problem Solving' 카테고리의 다른 글
작업 스케쥴링 (ex. 강의실 배정) (0) | 2024.07.10 |
---|---|
[BOJ] 1918 후위표현식 (0) | 2024.05.14 |
[BOJ] 1865번 웜홀 (0) | 2024.05.13 |
[프로그래머스] 110 옮기기 ⭐️ - 문자열 처리 (0) | 2024.05.08 |
[프로그래머스] Prim 인듯 아닌듯 Prim 같은 문제 (0) | 2024.04.24 |