From c3e39eaee7ef7a9e099d663fbaf266c31c2a0b63 Mon Sep 17 00:00:00 2001 From: lkhyun <102892446+lkhyun@users.noreply.github.com> Date: Tue, 23 Sep 2025 19:36:22 +0900 Subject: [PATCH] =?UTF-8?q?[20250923]=20BOJ=20/=20G3=20/=20=EB=91=90=20?= =?UTF-8?q?=EB=8B=A8=EA=B3=84=20=EC=B5=9C=EB=8B=A8=20=EA=B2=BD=EB=A1=9C=20?= =?UTF-8?q?2=20/=20=EC=9D=B4=EA=B0=95=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...353\213\250 \352\262\275\353\241\234 2.md" | 95 +++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 "lkhyun/202509/23 BOJ G3 \353\221\220 \353\213\250\352\263\204 \354\265\234\353\213\250 \352\262\275\353\241\234 2.md" diff --git "a/lkhyun/202509/23 BOJ G3 \353\221\220 \353\213\250\352\263\204 \354\265\234\353\213\250 \352\262\275\353\241\234 2.md" "b/lkhyun/202509/23 BOJ G3 \353\221\220 \353\213\250\352\263\204 \354\265\234\353\213\250 \352\262\275\353\241\234 2.md" new file mode 100644 index 00000000..a8e6a77a --- /dev/null +++ "b/lkhyun/202509/23 BOJ G3 \353\221\220 \353\213\250\352\263\204 \354\265\234\353\213\250 \352\262\275\353\241\234 2.md" @@ -0,0 +1,95 @@ +```java +import java.util.*; +import java.io.*; + +public class Main{ + static class Edge{ + int to; + long weight; + Edge(int to, long weight){ + this.to = to; + this.weight = weight; + } + } + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); + static StringTokenizer st; + static StringBuilder sb = new StringBuilder(); + static int N,M; + static List[] adjEdges; + static int X,Z; + static int P; + static long[] distStart; + static long[] distEnd; + + public static void main(String[] args) throws Exception { + st = new StringTokenizer(br.readLine()); + N = Integer.parseInt(st.nextToken()); + M = Integer.parseInt(st.nextToken()); + + adjEdges = new List[N+1]; + for (int i = 1; i <= N; i++) { + adjEdges[i] = new ArrayList<>(); + } + + distStart = new long[N+1]; + distEnd = new long[N+1]; + + for (int i = 0; i < M; i++) { + st = new StringTokenizer(br.readLine()); + int from = Integer.parseInt(st.nextToken()); + int to = Integer.parseInt(st.nextToken()); + long weight = Long.parseLong(st.nextToken()); + + adjEdges[from].add(new Edge(to, weight)); + adjEdges[to].add(new Edge(from, weight)); + } + + st = new StringTokenizer(br.readLine()); + X = Integer.parseInt(st.nextToken()); + Z = Integer.parseInt(st.nextToken()); + + P = Integer.parseInt(br.readLine()); + List middleVertex = new ArrayList<>(P); + + st = new StringTokenizer(br.readLine()); + for (int i = 0; i < P; i++) { + middleVertex.add(Integer.parseInt(st.nextToken())); + } + + dijkstra(X, distStart); + dijkstra(Z, distEnd); + + long min = Long.MAX_VALUE; + for (int Y : middleVertex) { + if(distStart[Y] == Long.MAX_VALUE || distEnd[Y] == Long.MAX_VALUE) continue; // Integer -> Long + min = Math.min(min, distStart[Y] + distEnd[Y]); + } + if(min == Long.MAX_VALUE) bw.write("-1"); + else bw.write(min + ""); + bw.close(); + } + + public static void dijkstra(int start, long[] dist){ + PriorityQueue pq = new PriorityQueue<>((a,b) -> Long.compare(a.weight,b.weight)); // Integer -> Long + Arrays.fill(dist, Long.MAX_VALUE); + + pq.offer(new Edge(start, 0)); + dist[start] = 0; + + while(!pq.isEmpty()){ + Edge cur = pq.poll(); + + if(dist[cur.to] < cur.weight) continue; + + for (Edge next : adjEdges[cur.to]) { + long newDistance = cur.weight + next.weight; + if(newDistance < dist[next.to]){ + dist[next.to] = newDistance; + pq.offer(new Edge(next.to, newDistance)); + } + } + } + } +} +```