From 1d5b269c51371280f1ba3ca8291b82c1b4f1269e Mon Sep 17 00:00:00 2001 From: oncsr Date: Tue, 16 Sep 2025 21:11:51 +0900 Subject: [PATCH] =?UTF-8?q?[20250916]=20BOJ=20/=20D5=20/=20JOI=20=EA=B5=AD?= =?UTF-8?q?=EA=B0=80=EC=9D=98=20=ED=96=89=EC=82=AC=20/=20=EA=B6=8C?= =?UTF-8?q?=ED=98=81=EC=A4=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0\354\235\230 \355\226\211\354\202\254.md" | 186 ++++++++++++++++++ 1 file changed, 186 insertions(+) create mode 100644 "khj20006/202509/16 BOJ D5 JOI \352\265\255\352\260\200\354\235\230 \355\226\211\354\202\254.md" diff --git "a/khj20006/202509/16 BOJ D5 JOI \352\265\255\352\260\200\354\235\230 \355\226\211\354\202\254.md" "b/khj20006/202509/16 BOJ D5 JOI \352\265\255\352\260\200\354\235\230 \355\226\211\354\202\254.md" new file mode 100644 index 00000000..8e3353f0 --- /dev/null +++ "b/khj20006/202509/16 BOJ D5 JOI \352\265\255\352\260\200\354\235\230 \355\226\211\354\202\254.md" @@ -0,0 +1,186 @@ +```java +import java.io.*; +import java.util.*; + +class IOController { + BufferedReader br; + BufferedWriter bw; + StringTokenizer st; + + public IOController() { + br = new BufferedReader(new InputStreamReader(System.in)); + bw = new BufferedWriter(new OutputStreamWriter(System.out)); + st = new StringTokenizer(""); + } + + String nextLine() throws Exception { + String line = br.readLine(); + st = new StringTokenizer(line); + return line; + } + + String nextToken() throws Exception { + while (!st.hasMoreTokens()) + nextLine(); + return st.nextToken(); + } + + int nextInt() throws Exception { + return Integer.parseInt(nextToken()); + } + + long nextLong() throws Exception { + return Long.parseLong(nextToken()); + } + + double nextDouble() throws Exception { + return Double.parseDouble(nextToken()); + } + + void close() throws Exception { + bw.flush(); + bw.close(); + } + + void write(String content) throws Exception { + bw.write(content); + } + +} + +public class Main { + + static IOController io; + + // + + static final int INF = (int)1e9 + 7; + + static int N, M, K, Q; + static List[] graph; + static List[] tree; + static List edges; + static int[][] min, par; + static int[] dep, root, dist; + + public static int f(int x) { return x == root[x] ? x : (root[x] = f(root[x])); } + + public static void main(String[] args) throws Exception { + + io = new IOController(); + + N = io.nextInt(); + M = io.nextInt(); + K = io.nextInt(); + Q = io.nextInt(); + graph = new List[N+1]; + tree = new List[N+1]; + root = new int[N+1]; + for(int i=1;i<=N;i++) { + graph[i] = new ArrayList<>(); + tree[i] = new ArrayList<>(); + root[i] = i; + } + + edges = new ArrayList<>(); + for(int i=1;i<=M;i++) { + int a = io.nextInt(); + int b = io.nextInt(); + int c = io.nextInt(); + graph[a].add(new int[]{b,c}); + graph[b].add(new int[]{a,c}); + edges.add(new int[]{a,b,0}); + } + + int[] starts = new int[K]; + for(int i=0;i b[2]-a[2]); + + for(int[] edge : edges) { + int a = edge[0], b = edge[1], c = edge[2]; + int x = f(a), y = f(b); + if(x == y) continue; + tree[a].add(b); + tree[b].add(a); + root[x] = y; + } + + par = new int[N+1][17]; + min = new int[N+1][17]; + dep = new int[N+1]; + dfs(1,0,0); + + for(int k=1;k<17;k++) for(int i=1;i<=N;i++) { + par[i][k] = par[par[i][k-1]][k-1]; + min[i][k] = Math.min(min[i][k-1], min[par[i][k-1]][k-1]); + } + + while(Q-->0) { + int a = io.nextInt(); + int b = io.nextInt(); + int ans = Integer.MAX_VALUE; + + int diff = Math.abs(dep[a] - dep[b]); + for(int k=0;k<17;k++) if((diff & (1< dep[b]) { + ans = Math.min(ans, min[a][k]); + a = par[a][k]; + } + else { + ans = Math.min(ans, min[b][k]); + b = par[b][k]; + } + } + + for(int k=16;k>=0;k--) if(par[a][k] != par[b][k]) { + ans = Math.min(ans, Math.min(min[a][k], min[b][k])); + a = par[a][k]; + b = par[b][k]; + } + + if(a != b) { + ans = Math.min(ans, Math.min(min[a][0], min[b][0])); + a = par[a][0]; + } + ans = Math.min(ans, dist[a]); + + io.write(ans + "\n"); + } + + io.close(); + + } + + public static void dijkstra(int[] starts) { + dist = new int[N+1]; + Arrays.fill(dist, INF); + PriorityQueue pq = new PriorityQueue<>((a,b) -> a[0]-b[0]); + for(int i=0;i dist[n]) continue; + for(int[] e:graph[n]) if(dist[e[0]] > d + e[1]) { + dist[e[0]] = d + e[1]; + pq.offer(new int[]{dist[e[0]], e[0]}); + } + } + } + + public static void dfs(int n, int p, int d) { + dep[n] = d; + par[n][0] = p; + min[n][0] = dist[n]; + for(int i:tree[n]) if(i != p) dfs(i, n, d+1); + } + +} +```