From 7a785fbb8a9b83f56c20ee39dbe065fa02cc5a84 Mon Sep 17 00:00:00 2001 From: Ukj0ng <90972240+Ukj0ng@users.noreply.github.com> Date: Wed, 29 Oct 2025 13:05:07 +0900 Subject: [PATCH] =?UTF-8?q?[20251029]=20BOJ=20/=20G2=20/=20=EC=88=98?= =?UTF-8?q?=EC=97=B4=EA=B3=BC=20=EA=B0=9C=EA=B5=AC=EB=A6=AC=20/=20?= =?UTF-8?q?=ED=95=9C=EC=A2=85=EC=9A=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4 \352\260\234\352\265\254\353\246\254.md" | 91 +++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 "Ukj0ng/202510/29 BOJ G2 \354\210\230\354\227\264\352\263\274 \352\260\234\352\265\254\353\246\254.md" diff --git "a/Ukj0ng/202510/29 BOJ G2 \354\210\230\354\227\264\352\263\274 \352\260\234\352\265\254\353\246\254.md" "b/Ukj0ng/202510/29 BOJ G2 \354\210\230\354\227\264\352\263\274 \352\260\234\352\265\254\353\246\254.md" new file mode 100644 index 00000000..a1b0f61e --- /dev/null +++ "b/Ukj0ng/202510/29 BOJ G2 \354\210\230\354\227\264\352\263\274 \352\260\234\352\265\254\353\246\254.md" @@ -0,0 +1,91 @@ +``` +import java.io.*; +import java.util.*; + +public class Main { + private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); + private static final long INF = Long.MAX_VALUE/2; + private static List[] graph; + private static long[] dist; + private static int[] a, b; + private static int N; + public static void main(String[] args) throws IOException { + init(); + dijkstra(); + for (int i = 1; i <= N; i++) { + bw.write(dist[i] + " "); + } + + bw.flush(); + bw.close(); + br.close(); + } + + private static void init() throws IOException { + N = Integer.parseInt(br.readLine()); + + a = new int[N+1]; + b = new int[N+1]; + dist = new long[N+1]; + graph = new List[N+1]; + + for (int i = 1; i <= N; i++) { + graph[i] = new ArrayList<>(); + } + + StringTokenizer st = new StringTokenizer(br.readLine()); + for (int i = 1; i <= N; i++) { + a[i] = Integer.parseInt(st.nextToken()); + if (1 <= i-a[i] && i-a[i] <= N) { + graph[i-a[i]].add(i); + } + + if (1 <= i+a[i] && i+a[i] <= N) { + graph[i+a[i]].add(i); + } + } + + st = new StringTokenizer(br.readLine()); + for (int i = 1; i <= N; i++) { + b[i] = Integer.parseInt(st.nextToken()); + } + } + + private static void dijkstra() { + PriorityQueue pq = new PriorityQueue<>((o1, o2) -> Long.compare(o1.cost, o2.cost)); + Arrays.fill(dist, INF); + for (int i = 1; i <= N; i++) { + if (i-a[i] < 1 || i+a[i] > N) { + dist[i] = b[i]; + pq.add(new Node(i, b[i])); + } + } + + while (!pq.isEmpty()) { + Node current = pq.poll(); + + if (current.cost > dist[current.dest]) continue; + + for (int nDest : graph[current.dest]) { + long nCost = current.cost + b[nDest]; + + if (nCost < dist[nDest]) { + dist[nDest] = nCost; + pq.add(new Node(nDest, dist[nDest])); + } + } + } + } + + static class Node { + int dest; + long cost; + + public Node(int dest, long cost) { + this.dest = dest; + this.cost = cost; + } + } +} +```