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
'Algorithm > Baekjoon' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [Algorithm/Baekjoon] ๋ถ๋ถ์์ด์ ํฉ - 1182 (S2/JAVA) (2) | 2024.09.02 |
|---|---|
| [Algorithm/Baekjoon] ์์ฐ - 2512 (S2/JAVA) (3) | 2024.03.20 |
| [Algorithm/Baekjoon] ์๋ํฐ - 1406 (S2/JAVA) (1) | 2023.08.08 |
| [Algorithm/Baekjoon] ์ฃผ์ ์ - 13305 (S3/JAVA) (1) | 2023.08.07 |
| [Algorithm/Baekjoon] ์ฃผ์ - 11501 (S2/JAVA) (0) | 2023.08.01 |