From 0cffed484aeb623ca0f04453a5b87be54c288a7e Mon Sep 17 00:00:00 2001 From: Jonghwan Lee <123362165+0224LJH@users.noreply.github.com> Date: Fri, 5 Dec 2025 23:54:14 +0900 Subject: [PATCH] =?UTF-8?q?[20251205]=20BOJ=20/=20G5=20/=20=EB=B0=B1?= =?UTF-8?q?=EB=8F=84=EC=96=B4=20/=20=EC=9D=B4=EC=A2=85=ED=99=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...J \353\260\261\353\217\204\354\226\264.md" | 103 ++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 "0224LJH/202512/05 BOJ \353\260\261\353\217\204\354\226\264.md" diff --git "a/0224LJH/202512/05 BOJ \353\260\261\353\217\204\354\226\264.md" "b/0224LJH/202512/05 BOJ \353\260\261\353\217\204\354\226\264.md" new file mode 100644 index 00000000..a5d75017 --- /dev/null +++ "b/0224LJH/202512/05 BOJ \353\260\261\353\217\204\354\226\264.md" @@ -0,0 +1,103 @@ +```java + +import java.util.StringTokenizer; +import java.util.*; +import java.io.*; + +public class Main { + + static StringBuilder sb = new StringBuilder(); + static HashSet set = new HashSet<>(); + static int nodeCnt,edgeCnt; + static long ans = -1; + static long[] dis; + static Node[] nodes; + static boolean[] canGo,visited; + + static class Node implements Comparable{ + int num; + Map to; + + public Node (int num) { + this.num = num; + to = new HashMap<>(); + } + + public int compareTo(Node n) { + return Long.compare(dis[this.num], dis[n.num]); + } + } + + + public static void main(String[] args) throws IOException { + init(); + process(); + print(); + + } + + private static void init() throws IOException{ + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(br.readLine()); + nodeCnt = Integer.parseInt(st.nextToken()); + edgeCnt = Integer.parseInt(st.nextToken()); + + nodes = new Node[nodeCnt]; + visited = new boolean[nodeCnt]; + canGo = new boolean[nodeCnt]; + dis = new long[nodeCnt]; + Arrays.fill(dis, Long.MAX_VALUE); + dis[0] = 0; + + for (int i = 0; i < nodeCnt; i++) { + nodes[i] = new Node(i); + } + + st = new StringTokenizer(br.readLine()); + for (int i = 0; i < nodeCnt; i++) { + canGo[i] = (Integer.parseInt(st.nextToken()) == 1); + } + + for (int i = 0; i < edgeCnt; i++) { + st = new StringTokenizer(br.readLine()); + int from = Integer.parseInt(st.nextToken()); + int to = Integer.parseInt(st.nextToken()); + int cost = Integer.parseInt(st.nextToken()); + + nodes[from].to.put(to,cost); + nodes[to].to.put(from,cost); + } + + } + + private static void process() throws IOException { + PriorityQueue pq = new PriorityQueue<>(); + pq.add(nodes[0]); + + while(!pq.isEmpty()) { + Node n = pq.poll(); + if (visited[n.num]) continue; + visited[n.num] = true; + + for (int to: n.to.keySet()) { + if (to != nodeCnt-1 && canGo[to]) { + continue; + } + int cost = n.to.get(to); + if (dis[to] > dis[n.num] + cost) { + dis[to] = dis[n.num] + cost; + pq.add(nodes[to]); + } + } + } + + if (dis[nodeCnt-1] != Long.MAX_VALUE) ans = dis[nodeCnt-1]; + + } + + private static void print() { + System.out.println(ans); + } + +} +```