Algorithm/Baekjoon

[Algorithm/Baekjoon] ์ง€๋ฆ„๊ธธ - 1446 (S1/JAVA)

dpdms2148 2023. 9. 12. 00:01
728x90

๐Ÿ“‘๋ฌธ์ œ๋งํฌ

 

1446๋ฒˆ: ์ง€๋ฆ„๊ธธ

์ฒซ์งธ ์ค„์— ์ง€๋ฆ„๊ธธ์˜ ๊ฐœ์ˆ˜ N๊ณผ ๊ณ ์†๋„๋กœ์˜ ๊ธธ์ด D๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. N์€ 12 ์ดํ•˜์ธ ์–‘์˜ ์ •์ˆ˜์ด๊ณ , D๋Š” 10,000๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜์ด๋‹ค. ๋‹ค์Œ N๊ฐœ์˜ ์ค„์— ์ง€๋ฆ„๊ธธ์˜ ์‹œ์ž‘ ์œ„์น˜, ๋„์ฐฉ ์œ„์น˜, ์ง€๋ฆ„๊ธธ์˜ ๊ธธ์ด

www.acmicpc.net

๐Ÿ’ป์ฝ”๋“œ

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class Main {
    public static class Shortcut {
        int start;
        int end;
        int distance;

        public Shortcut(int start, int end, int distance) {
            this.start = start;
            this.end = end;
            this.distance = distance;
        }
    }

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());

        int N = Integer.parseInt(st.nextToken()); // ์ง€๋ฆ„๊ธธ์˜ ๊ฐฏ์ˆ˜
        int D = Integer.parseInt(st.nextToken()); // ๊ณ ์†๋„๋กœ์˜ ๊ธธ์ด

        int[] minDistance = new int[D + 1];
        Arrays.fill(minDistance, Integer.MAX_VALUE);

        ArrayList<Shortcut> shortcut = new ArrayList<>();
        for (int i = 0; i < N; i++) {
            st = new StringTokenizer(br.readLine());
            int start = Integer.parseInt(st.nextToken());
            int end = Integer.parseInt(st.nextToken());
            int distance = Integer.parseInt(st.nextToken());

            //๋„์ฐฉ ์œ„์น˜๊ฐ€ ๊ณ ์†๋„๋กœ์˜ ๊ธธ์ด๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜, ์ง€๋ฆ„๊ธธ์˜ ๊ธธ์ด๊ฐ€ ๋„๋กœ์˜ ๊ธธ์ด๋ณด๋‹ค ๊ธด ๊ฒฝ์šฐ
            if (end > D || end - start <= distance) continue;

            // ์‹œ์ž‘ ์œ„์น˜, ๋„์ฐฉ ์œ„์น˜, ์ง€๋ฆ„๊ธธ์˜ ๊ธธ์ด
            shortcut.add(new Shortcut(start, end, distance));
        }
        Collections.sort(shortcut, (o1, o2) -> {
            if (o1.start == o2.start) {//์‹œ์ž‘ ์ง€์ ์ด ๊ฐ™์œผ๋ฉด ์ข…๋ฃŒ ์ง€์ ์„ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌ
                return Integer.compare(o1.end, o2.end);
            }
            return Integer.compare(o1.start, o2.start);
        });

        int position = 0;
        int index = 0;
        minDistance[0] = 0;
        while (position < D) {
            if (index < shortcut.size()) { // ์ง€๋ฆ„๊ธธ์ด ์žˆ๋Š” ๊ฒฝ์šฐ
                Shortcut s = shortcut.get(index);
                if (position == s.start) { // ํ˜„์žฌ ์œ„์น˜์—์„œ ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ์ง€๋ฆ„๊ธธ์ด ์žˆ๋Š” ๊ฒฝ์šฐ
                    minDistance[s.end] = Math.min(minDistance[position] + s.distance, minDistance[s.end]);
                    index++;
                } else {
                    minDistance[position + 1] = Math.min(minDistance[position + 1], minDistance[position] + 1);
                    position++;
                }
            } else { // ์ง€๋ฆ„๊ธธ์ด ์—†๋Š” ๊ฒฝ์šฐ
                minDistance[position + 1] = Math.min(minDistance[position + 1], minDistance[position] + 1);
                position++;
            }
        }

        System.out.println(minDistance[D]);
    }
}
728x90