From ebfcc135e9c29a42ecbaf024edc36111ba65504d Mon Sep 17 00:00:00 2001 From: zinnnn37 Date: Fri, 2 Jan 2026 22:25:10 +0900 Subject: [PATCH] =?UTF-8?q?[20260102]=20BOJ=20/=20G5=20/=20=EB=B0=B1?= =?UTF-8?q?=EB=8F=84=EC=96=B4=20/=20=EA=B9=80=EB=AF=BC=EC=A7=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...5 \353\260\261\353\217\204\354\226\264.md" | 108 ++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 "zinnnn37/202601/2 BOJ G5 \353\260\261\353\217\204\354\226\264.md" diff --git "a/zinnnn37/202601/2 BOJ G5 \353\260\261\353\217\204\354\226\264.md" "b/zinnnn37/202601/2 BOJ G5 \353\260\261\353\217\204\354\226\264.md" new file mode 100644 index 00000000..f942c491 --- /dev/null +++ "b/zinnnn37/202601/2 BOJ G5 \353\260\261\353\217\204\354\226\264.md" @@ -0,0 +1,108 @@ +```java +import java.io.*; +import java.util.*; + +public class BJ_17396_백도어 { + + private static final int FALSE = 0; + private static final int TRUE = 1; + private static final long INF = 100_000L * 1000_000 + 1; + + private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); + private static StringTokenizer st; + + private static int N, M; + private static int[] isVisible; + private static long[] dist; + private static List[] graph; + private static Queue pq; + + private static class Node implements Comparable { + int to; + long weight; + + public Node(int to, long weight) { + this.to = to; + this.weight = weight; + } + + @Override + public int compareTo(Node o) { + return Long.compare(this.weight, o.weight); + } + + } + + public static void main(String[] args) throws IOException { + init(); + sol(); + + bw.flush(); + bw.close(); + br.close(); + } + + private static void init() throws IOException { + st = new StringTokenizer(br.readLine()); + N = Integer.parseInt(st.nextToken()); + M = Integer.parseInt(st.nextToken()); + + graph = new List[N]; + for (int i = 0; i < N; i++) { + graph[i] = new ArrayList<>(); + } + + isVisible = new int[N]; + st = new StringTokenizer(br.readLine()); + for (int i = 0; i < N; i++) { + isVisible[i] = Integer.parseInt(st.nextToken()); + } + + dist = new long[N]; + Arrays.fill(dist, INF); + + for (int i = 0; i < M; i++) { + st = new StringTokenizer(br.readLine()); + + int a = Integer.parseInt(st.nextToken()); + int b = Integer.parseInt(st.nextToken()); + int c = Integer.parseInt(st.nextToken()); + + graph[a].add(new Node(b, c)); + graph[b].add(new Node(a, c)); + } + + pq = new PriorityQueue<>(); + } + + private static void sol() throws IOException { + pq.offer(new Node(0, 0)); + dist[0] = 0; + + while (!pq.isEmpty()) { + Node cur = pq.poll(); + + if (cur.to == N - 1) { + bw.write(dist[cur.to] + ""); + return; + } + + if (dist[cur.to] < cur.weight) continue; + + for (Node next : graph[cur.to]) { + if (next.to != N - 1 && isVisible[next.to] == TRUE) continue; + + long newDist = dist[cur.to] + next.weight; + + if (newDist < dist[next.to]) { + dist[next.to] = newDist; + pq.offer(new Node(next.to, newDist)); + } + } + } + bw.write("-1"); + } + +} +```