From f6df1e7883fd40d2e83ba7834e3f05262300ae08 Mon Sep 17 00:00:00 2001 From: nkey Date: Sun, 15 Feb 2026 06:17:55 +0900 Subject: [PATCH] =?UTF-8?q?1446-s1=20=EC=84=B1=EA=B3=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- workbook_8708/1446-s1.py | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 workbook_8708/1446-s1.py diff --git a/workbook_8708/1446-s1.py b/workbook_8708/1446-s1.py new file mode 100644 index 0000000..f497f02 --- /dev/null +++ b/workbook_8708/1446-s1.py @@ -0,0 +1,40 @@ +# 지름길 + +import sys + +input = sys.stdin.readline + +def solution(): + n, d = map(int, input().rstrip().split()) + shortcut = {} + for _ in range(n): + start, end, cost = map(int, input().rstrip().split()) + if end > d: + continue + if not shortcut.get(end, []): + shortcut[end] = [] + shortcut[end].append((start, cost)) + + dp = [0] * (d+1) + + for i in range(1, d+1): + shortcut_list = shortcut.get(i, []) + dp[i] = min(min([dp[s]+c for s, c in shortcut_list]) if shortcut_list else float('inf'), dp[i-1]+1) + + print(dp[d]) + return + + +solution() + +""" +걸린 시간: 30분 + +시간 복잡도: 모든 지점에 대해 dp 값을 구하는데, 이때 한 지점은 연결된 지름길만큼 반복한다. +하지만 지름길 하나 당 한번만 보기 때문에 전체 시간복잡도는 O(n+d)이다. + +해설: 지름길의 도착 위치 기준으로 그냥 가는 것과 여러 지름길 중 하나를 선택하는 것과 같이 +여러 개를 비교해야했다. 그리고 비교를 하는 것이 과거의 지점에서 계산을 하는 것이기 때문에 dp를 생각했다. +현재 지점의 dp를 계산할 때 이 곳에 올 수 있는 모든 경우의 수 중 cost가 최소인 것을 구하면 된다. +따라서 지름길을 통해 오는 것과 바로 한 칸 전 위치에서 +1로 오는 경우 중 구하면 된다. +""" \ No newline at end of file