From f6baf2b1d289a8fc3bed2630ee9d9b83a1db22f6 Mon Sep 17 00:00:00 2001 From: zinnnn37 Date: Sun, 7 Sep 2025 18:01:23 +0900 Subject: [PATCH] =?UTF-8?q?[20250907]=20BOJ=20/=20G2=20/=20=EC=A7=91=20?= =?UTF-8?q?=EA=B5=AC=ED=95=98=EA=B8=B0=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 --- ...1 \352\265\254\355\225\230\352\270\260.md" | 130 ++++++++++++++++++ 1 file changed, 130 insertions(+) create mode 100644 "zinnnn37/202509/7 BOJ G2 \354\247\221 \352\265\254\355\225\230\352\270\260.md" diff --git "a/zinnnn37/202509/7 BOJ G2 \354\247\221 \352\265\254\355\225\230\352\270\260.md" "b/zinnnn37/202509/7 BOJ G2 \354\247\221 \352\265\254\355\225\230\352\270\260.md" new file mode 100644 index 00000000..d6006875 --- /dev/null +++ "b/zinnnn37/202509/7 BOJ G2 \354\247\221 \352\265\254\355\225\230\352\270\260.md" @@ -0,0 +1,130 @@ +```java +import java.io.*; +import java.util.*; + +public class BJ_13911_집_구하기 { + + private static final long INF = 9876543210L; + + 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 V, E; + private static int M, x; + private static int S, y; + + private static long[] mcdonalds; + private static long[] starbucks; + private static boolean[] isMcdonalds; + private static boolean[] isStarbucks; + + private static Queue pq; + private static List[] graph; + + private static class Node implements Comparable { + int from; + int to; + long dist; + + Node(int from, int to, long dist) { + this.from = from; + this.to = to; + this.dist = dist; + } + + @Override + public int compareTo(Node o) { + return Long.compare(this.dist, o.dist); + } + } + + public static void main(String[] args) throws IOException { + init(); + sol(); + } + + private static void init() throws IOException { + st = new StringTokenizer(br.readLine()); + V = Integer.parseInt(st.nextToken()); + E = Integer.parseInt(st.nextToken()); + + graph = new List[V + 1]; + for (int i = 1; i <= V; i++) { + graph[i] = new ArrayList<>(); + } + + for (int i = 1; i <= E; i++) { + st = new StringTokenizer(br.readLine()); + + int from = Integer.parseInt(st.nextToken()); + int to = Integer.parseInt(st.nextToken()); + int weight = Integer.parseInt(st.nextToken()); + + graph[from].add(new Node(from, to, weight)); + graph[to].add(new Node(to, from, weight)); + } + + mcdonalds = new long[V + 1]; + starbucks = new long[V + 1]; + isMcdonalds = new boolean[V + 1]; + isStarbucks = new boolean[V + 1]; + + pq = new PriorityQueue<>(); + } + + private static void sol() throws IOException { + // Multisource Dijkstra + st = new StringTokenizer(br.readLine()); + M = Integer.parseInt(st.nextToken()); + x = Integer.parseInt(st.nextToken()); + dijkstra(mcdonalds, isMcdonalds, M); + + // Multisource Dijkstra + st = new StringTokenizer(br.readLine()); + S = Integer.parseInt(st.nextToken()); + y = Integer.parseInt(st.nextToken()); + dijkstra(starbucks, isStarbucks, S); + + long ans = INF; + for (int i = 1; i <= V; i++) { + if (!isMcdonalds[i] && !isStarbucks[i] && mcdonalds[i] <= x && starbucks[i] <= y) { + ans = Math.min(ans, mcdonalds[i] + starbucks[i]); + } + } + bw.write((ans == INF ? -1 : ans) + ""); + bw.flush(); + bw.close(); + br.close(); + } + + private static void dijkstra(long[] dist, boolean[] isStore, int cnt) throws IOException { + pq.clear(); + Arrays.fill(dist, INF); + + st = new StringTokenizer(br.readLine()); + for (int i = 0; i < cnt; i++) { + int start = Integer.parseInt(st.nextToken()); + isStore[start] = true; + dist[start] = 0; + pq.offer(new Node(start, start, 0)); + } + + while (!pq.isEmpty()) { + Node cur = pq.poll(); + + if (cur.dist > dist[cur.to]) continue; + + for (Node n : graph[cur.to]) { + long nDist = dist[cur.to] + n.dist; + + if (nDist < dist[n.to]) { + dist[n.to] = nDist; + pq.offer(new Node(n.from, n.to, nDist)); + } + } + } + } + +} +``` \ No newline at end of file