From 99db2bb4eb9dda04711558ac22940082c8cce5a1 Mon Sep 17 00:00:00 2001 From: Jonghwan Lee <123362165+0224LJH@users.noreply.github.com> Date: Mon, 8 Sep 2025 13:47:30 +0900 Subject: [PATCH] =?UTF-8?q?[20250908]=20BOJ=20/=20G5=20/=20=EC=B5=9C?= =?UTF-8?q?=EC=86=8C=20=EB=B9=84=EC=9A=A9=20=EA=B5=AC=ED=95=98=EA=B8=B0=20?= =?UTF-8?q?/=20=EC=9D=B4=EC=A2=85=ED=99=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...1 \352\265\254\355\225\230\352\270\260.md" | 95 +++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 "0224LJH/202509/08 BOJ \354\265\234\354\206\214\353\271\204\354\232\251 \352\265\254\355\225\230\352\270\260.md" diff --git "a/0224LJH/202509/08 BOJ \354\265\234\354\206\214\353\271\204\354\232\251 \352\265\254\355\225\230\352\270\260.md" "b/0224LJH/202509/08 BOJ \354\265\234\354\206\214\353\271\204\354\232\251 \352\265\254\355\225\230\352\270\260.md" new file mode 100644 index 00000000..9e664e66 --- /dev/null +++ "b/0224LJH/202509/08 BOJ \354\265\234\354\206\214\353\271\204\354\232\251 \352\265\254\355\225\230\352\270\260.md" @@ -0,0 +1,95 @@ +```java +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.*; + +public class Main { + static int cityCnt, busCnt,start,goal; + static int[] distance; + static boolean[] visited; + static Node[] nodes; + static PriorityQueue pq = new PriorityQueue<>(); + + static class Node implements Comparable{ + int num; + Map costs = new HashMap<>(); + + public Node(int num) { + this.num = num; + } + + @Override + public int compareTo(Node o) { + return Integer.compare(distance[this.num], distance[o.num]); + } + } + + + public static void main(String[] args) throws NumberFormatException, IOException { + init(); + process(); + print(); + } + + public static void init() throws NumberFormatException, IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + cityCnt = Integer.parseInt(br.readLine()); + busCnt = Integer.parseInt(br.readLine()); + nodes = new Node[cityCnt+1]; + distance = new int[cityCnt+1]; + visited = new boolean[cityCnt+1]; + for (int i = 0; i <= cityCnt; i++) { + nodes[i] = new Node(i); + } + + for (int i = 0; i < busCnt; i++) { + StringTokenizer st = new StringTokenizer (br.readLine()); + int from = Integer.parseInt(st.nextToken()); + int to = Integer.parseInt(st.nextToken()); + int cost = Integer.parseInt(st.nextToken()); + + if (nodes[from].costs.containsKey(to)) { + int smaller = Math.min(cost, nodes[from].costs.get(to)); + nodes[from].costs.replace(to, smaller); + } else { + nodes[from].costs.put(to, cost); + } + + } + + StringTokenizer st = new StringTokenizer(br.readLine()); + start = Integer.parseInt(st.nextToken()); + goal = Integer.parseInt(st.nextToken()); + + Arrays.fill(distance, Integer.MAX_VALUE/2); + distance[start] = 0; + } + + public static void process() { + pq.add(nodes[start]); + + while(!pq.isEmpty()) { + Node n = pq.poll(); + if (visited[n.num]) continue; + visited[n.num] = true; + + for (int to: n.costs.keySet()) { + if(visited[to]) continue; + int cost = n.costs.get(to); + int nDistance = distance[n.num] + cost; + if (distance[to] > nDistance) { + distance[to] = nDistance; + pq.add(nodes[to]); + } + } + + } + + } + + public static void print() { + System.out.println(distance[goal]); + } +} +```