문제
https://www.acmicpc.net/problem/1446
1446번: 지름길
첫째 줄에 지름길의 개수 N과 고속도로의 길이 D가 주어진다. N은 12 이하인 양의 정수이고, D는 10,000보다 작거나 같은 자연수이다. 다음 N개의 줄에 지름길의 시작 위치, 도착 위치, 지름길의 길이
www.acmicpc.net
코드
import sys
input=sys.stdin.readline
n,d=map(int, input().split())
graph=[[] for _ in range(d+1)]
for _ in range(n):
a,b,c=map(int, input().split())
if b<=d:
graph[a].append([b,c])
dp=[]
for i in range(d+1):
dp.append(i)
for i in range(d+1):
# 지름길(dp[i])과 고속도로(dp[i-1]+1) 비교
dp[i] = min(dp[i], dp[i - 1] + 1)
if graph[i]:
for des,dis in graph[i]:
#지름길이 더 짧을 경우 갱신
dp[des]=min(dp[des], dis+dp[i])
print(dp[d])
시간 복잡도
O(고속도로 길이)
'문제풀이 > DP' 카테고리의 다른 글
[파이썬] [DP] 백준 15988 1, 2, 3 더하기 3 (0) | 2023.04.07 |
---|---|
[파이썬] [DP] 백준 11052 카드 구매하기 (0) | 2023.04.05 |
[파이썬] [DP] 백준 15989 1, 2, 3 더하기 4 (0) | 2023.02.27 |
[파이썬] [DP] 백준 23083 꿀벌 승연이 (0) | 2023.02.06 |
[파이썬] [DP] 백준 27210 신을 모시는 사당 (0) | 2023.01.14 |