|
| 1 | +package study.week09; |
| 2 | + |
| 3 | +import java.io.BufferedReader; |
| 4 | +import java.io.IOException; |
| 5 | +import java.io.InputStreamReader; |
| 6 | +import java.util.ArrayList; |
| 7 | +import java.util.List; |
| 8 | + |
| 9 | +// 지름길 |
| 10 | +// dp |
| 11 | + |
| 12 | +/** |
| 13 | + * 1부터 D까지 순회하면서 현재 i값에 대하여 지름길의 end 가 같을 때, 해당 지름길을 이용하는 것이 작다면 업데이트 |
| 14 | + * 지름길 리스트를 end기준으로 오름차순 정렬을 하는데, i는 1부터 D까지 증가하므로 end 값이 작은 것부터 봐야 다른 지름길까지 순회 할 필요가 없기 때문 |
| 15 | + */ |
| 16 | +public class BOJ_1446 { |
| 17 | + |
| 18 | + static class ShortCut { |
| 19 | + |
| 20 | + int start; |
| 21 | + int end; |
| 22 | + int len; |
| 23 | + |
| 24 | + public ShortCut(int start, int end, int len) { |
| 25 | + this.start = start; |
| 26 | + this.end = end; |
| 27 | + this.len = len; |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + public static void main(String[] args) throws IOException { |
| 32 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 33 | + |
| 34 | + String[] s = br.readLine().split(" "); |
| 35 | + int N = Integer.parseInt(s[0]); |
| 36 | + int D = Integer.parseInt(s[1]); |
| 37 | + |
| 38 | + List<ShortCut> shortCuts= new ArrayList<>(); |
| 39 | + |
| 40 | + for (int i = 0; i < N; i++) { |
| 41 | + s = br.readLine().split(" "); |
| 42 | + int start = Integer.parseInt(s[0]); |
| 43 | + int end = Integer.parseInt(s[1]); |
| 44 | + int len = Integer.parseInt(s[2]); |
| 45 | + |
| 46 | + shortCuts.add(new ShortCut(start, end, len)); |
| 47 | + } |
| 48 | + |
| 49 | + shortCuts.sort((s1,s2) -> { |
| 50 | + if(s1.end == s2.end){ |
| 51 | + return s2.len - s1.len; |
| 52 | + } |
| 53 | + |
| 54 | + return s1.end - s2.end; |
| 55 | + }); |
| 56 | + |
| 57 | + int [] dp = new int[D+1]; |
| 58 | + |
| 59 | + |
| 60 | + for(int i=1; i<=D; i++){ |
| 61 | + dp[i] = dp[i-1] + 1; |
| 62 | + |
| 63 | + for(int j=0; j<N; j++){ |
| 64 | + ShortCut shortCut = shortCuts.get(j); |
| 65 | + if(shortCut.end > i){ |
| 66 | + break; |
| 67 | + } |
| 68 | + |
| 69 | + if(shortCut.end == i){ |
| 70 | + if(dp[i] > dp[shortCut.start] + shortCut.len){ |
| 71 | + dp[i] = dp[shortCut.start] + shortCut.len; |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + System.out.println(dp[D]); |
| 78 | + |
| 79 | + } |
| 80 | + |
| 81 | +} |
0 commit comments