From 36f555d216b6bab414d1681ef5804b854a86aa02 Mon Sep 17 00:00:00 2001 From: lkhyun <102892446+lkhyun@users.noreply.github.com> Date: Thu, 6 Feb 2025 17:40:26 +0900 Subject: [PATCH] =?UTF-8?q?[20250206]=20BOJ=20/=20=EC=8B=A4=EB=B2=841=20/?= =?UTF-8?q?=20=EC=A7=80=EB=A6=84=EA=B8=B8=20/=20=EC=9D=B4=EA=B0=95?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...1 \354\247\200\353\246\204\352\270\270.md" | 82 +++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 "lkhyun/202502/06 BOJ \354\213\244\353\262\2041 \354\247\200\353\246\204\352\270\270.md" diff --git "a/lkhyun/202502/06 BOJ \354\213\244\353\262\2041 \354\247\200\353\246\204\352\270\270.md" "b/lkhyun/202502/06 BOJ \354\213\244\353\262\2041 \354\247\200\353\246\204\352\270\270.md" new file mode 100644 index 00000000..b2d9f320 --- /dev/null +++ "b/lkhyun/202502/06 BOJ \354\213\244\353\262\2041 \354\247\200\353\246\204\352\270\270.md" @@ -0,0 +1,82 @@ +```java +import java.util.*; +import java.io.*; + +class Node implements Comparable { + int vertex, weight; + + Node(int vertex, int weight) { + this.vertex = vertex; + this.weight = weight; + } + + @Override + public int compareTo(Node n) { + return this.weight - n.weight; + } +} + +public class Main { + final static int INF = Integer.MAX_VALUE; + static List[] graph; + static int[] dist; + + public static void dijkstra(int start, int D) { + PriorityQueue minheap = new PriorityQueue<>(); + Arrays.fill(dist, INF); // 거리 초기화 + dist[start] = 0; + minheap.add(new Node(start, 0)); + + while (!minheap.isEmpty()) { + Node current = minheap.poll(); + + if (current.weight > dist[current.vertex]) continue; + + for (Node next : graph[current.vertex]) { + int newWeight = dist[current.vertex] + next.weight; + if (dist[next.vertex] > newWeight) { + dist[next.vertex] = newWeight; + minheap.add(new Node(next.vertex, newWeight)); + } + } + } + } + + public static void main(String[] args) throws Exception { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); + StringTokenizer st = new StringTokenizer(br.readLine()); + int N = Integer.parseInt(st.nextToken()); + int D = Integer.parseInt(st.nextToken()); + + graph = new ArrayList[D + 1]; + dist = new int[D + 1]; + for (int i = 0; i <= D; i++) { + graph[i] = 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 weight = Integer.parseInt(st.nextToken()); + + if (end <= D && (end - start) > weight) { // 일방통행이니까 목적지보다 더 가거나 굳이 탈 이유가 없는 지름길 제외 + graph[start].add(new Node(end, weight)); + } + } + for (int i = 0; i < D; i++) { //지름길 안타는 경우우 + graph[i].add(new Node(i + 1, 1)); + } + + // 다익스트라 실행 + dijkstra(0, D); + + // 최단 거리 출력 + bw.write(dist[D] + "\n"); + bw.flush(); + bw.close(); + } +} + +```