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; + } + } +} +```