From ce970b4d7e15d9729744f0e4adfa948d794ad0ce Mon Sep 17 00:00:00 2001 From: zinnnn37 Date: Sun, 4 Jan 2026 23:21:39 +0900 Subject: [PATCH] =?UTF-8?q?[20260104]=20BOJ=20/=20G5=20/=20=EA=B0=84?= =?UTF-8?q?=EC=84=A0=20=EC=9D=B4=EC=96=B4=EA=B0=80=EA=B8=B0=202=20/=20?= =?UTF-8?q?=EA=B9=80=EB=AF=BC=EC=A7=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\354\226\264\352\260\200\352\270\260 2.md" | 97 +++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 "zinnnn37/202601/4 BOJ G4 \352\260\204\354\204\240 \354\235\264\354\226\264\352\260\200\352\270\260 2.md" diff --git "a/zinnnn37/202601/4 BOJ G4 \352\260\204\354\204\240 \354\235\264\354\226\264\352\260\200\352\270\260 2.md" "b/zinnnn37/202601/4 BOJ G4 \352\260\204\354\204\240 \354\235\264\354\226\264\352\260\200\352\270\260 2.md" new file mode 100644 index 00000000..f8d6f231 --- /dev/null +++ "b/zinnnn37/202601/4 BOJ G4 \352\260\204\354\204\240 \354\235\264\354\226\264\352\260\200\352\270\260 2.md" @@ -0,0 +1,97 @@ +```java +import java.io.*; +import java.util.*; + +public class BJ_14284_간선_이어가기_2 { + + private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); + private static StringTokenizer st; + + private static int N, M, S, T; + private static int[] dist; + private static List[] graph; + private static Queue q; + + private static class Node implements Comparable { + int to; + int weight; + + public Node(int to, int weight) { + this.to = to; + this.weight = weight; + } + + @Override + public int compareTo(Node o) { + return Integer.compare(this.weight, o.weight); + } + + } + + public static void main(String[] args) throws IOException { + init(); + sol(); + + bw.flush(); + bw.close(); + br.close(); + } + + private static void init() throws IOException { + st = new StringTokenizer(br.readLine()); + N = Integer.parseInt(st.nextToken()); + M = Integer.parseInt(st.nextToken()); + + graph = new List[N + 1]; + for (int i = 1; i <= N; i++) { + graph[i] = new ArrayList<>(); + } + + for (int i = 0; i < M; i++) { + st = new StringTokenizer(br.readLine()); + + int a = Integer.parseInt(st.nextToken()); + int b = Integer.parseInt(st.nextToken()); + int c = Integer.parseInt(st.nextToken()); + + graph[a].add(new Node(b, c)); + graph[b].add(new Node(a, c)); + } + + st = new StringTokenizer(br.readLine()); + S = Integer.parseInt(st.nextToken()); + T = Integer.parseInt(st.nextToken()); + + dist = new int[N + 1]; + Arrays.fill(dist, 987654321); + q = new PriorityQueue<>(); + } + + private static void sol() throws IOException { + q.offer(new Node(S, 0)); + dist[S] = 0; + + while (!q.isEmpty()) { + Node cur = q.poll(); + + if (dist[cur.to] < cur.weight) continue; + + if (cur.to == T) { + bw.write(cur.weight + ""); + return; + } + + for (Node next : graph[cur.to]) { + int newDist = dist[cur.to] + next.weight; + + if (newDist < dist[next.to]) { + dist[next.to] = newDist; + q.offer(new Node(next.to, newDist)); + } + } + } + } + +} +```