From bfe1d720debaa6e9c3c8d680d41577ba669706af Mon Sep 17 00:00:00 2001 From: suyeun84 <81475092+suyeun84@users.noreply.github.com> Date: Fri, 1 Aug 2025 17:52:17 +0900 Subject: [PATCH] =?UTF-8?q?[20250801]=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=EC=88=98=EC=97=B0?= 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" | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 "suyeun84/202508/01 BOJ G5 \352\260\204\354\204\240 \354\235\264\354\226\264\352\260\200\352\270\260 2.md" diff --git "a/suyeun84/202508/01 BOJ G5 \352\260\204\354\204\240 \354\235\264\354\226\264\352\260\200\352\270\260 2.md" "b/suyeun84/202508/01 BOJ G5 \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..bcc04dac --- /dev/null +++ "b/suyeun84/202508/01 BOJ G5 \352\260\204\354\204\240 \354\235\264\354\226\264\352\260\200\352\270\260 2.md" @@ -0,0 +1,67 @@ +```java +import java.util.*; +import java.io.*; + +public class boj14284 { + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + static StringTokenizer st; + static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());} + static int nextInt() {return Integer.parseInt(st.nextToken());} + + static int dist[]; + static ArrayList> graph = new ArrayList<>(); + static int n, m; + + public static void main(String[] args) throws Exception { + nextLine(); + n = nextInt(); + m = nextInt(); + dist = new int[n+1]; + for (int i = 0; i < n+1; i++) graph.add(new ArrayList<>()); + + int a, b, c; + for (int i = 0; i < m; i++) { + nextLine(); + a = nextInt(); + b = nextInt(); + c = nextInt(); + graph.get(a).add(new Node(b, c)); + graph.get(b).add(new Node(a, c)); + } + nextLine(); + int s = nextInt(); + int t = nextInt(); + System.out.println(dijkstra(s, t)); + } + + static int dijkstra(int s, int t) { + Arrays.fill(dist, Integer.MAX_VALUE); + PriorityQueue pq = new PriorityQueue<>( + (o1, o2) -> o1.v - o2.v + ); + pq.add(new Node(s, 0)); + + dist[s] = 0; + + while (!pq.isEmpty()) { + Node cur = pq.poll(); + + for (Node next : graph.get(cur.e)) { + if (dist[next.e] > dist[cur.e] + next.v) { + dist[next.e] = dist[cur.e] + next.v; + pq.add(new Node(next.e, dist[next.e])); + } + } + } + return dist[t]; + } + + static class Node { + int e, v; + public Node(int e, int v) { + this.e = e; + this.v = v; + } + } +} +```