๊ธฐ๋ก๋ฐฉ

BOJ_18352 : ํŠน์ • ๊ฑฐ๋ฆฌ์˜ ๋„์‹œ ์ฐพ๊ธฐ ๋ณธ๋ฌธ

CodingTest/Java

BOJ_18352 : ํŠน์ • ๊ฑฐ๋ฆฌ์˜ ๋„์‹œ ์ฐพ๊ธฐ

Soom_1n 2024. 3. 27. 16:08

๐Ÿ‘‰ ๋ฌธ์ œ๋งํฌ

 

18352๋ฒˆ: ํŠน์ • ๊ฑฐ๋ฆฌ์˜ ๋„์‹œ ์ฐพ๊ธฐ

์ฒซ์งธ ์ค„์— ๋„์‹œ์˜ ๊ฐœ์ˆ˜ N, ๋„๋กœ์˜ ๊ฐœ์ˆ˜ M, ๊ฑฐ๋ฆฌ ์ •๋ณด K, ์ถœ๋ฐœ ๋„์‹œ์˜ ๋ฒˆํ˜ธ X๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. (2 ≤ N ≤ 300,000, 1 ≤ M ≤ 1,000,000, 1 ≤ K ≤ 300,000, 1 ≤ X ≤ N) ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ M๊ฐœ์˜ ์ค„์— ๊ฑธ์ณ์„œ ๋‘ ๊ฐœ

www.acmicpc.net


๐Ÿ”ธ ๋ฌธ์ œ ๋ถ„์„ ๐Ÿ”ธ

  • N๊ฐœ์˜ ๋„์‹œ์™€ ๋„์‹œ ์‚ฌ์ด๋ฅผ ์—ฐ๊ฒฐํ•˜๋Š” M๊ฐœ์˜ ๋‹จ๋ฐฉํ–ฅ ๋„๋กœ๊ฐ€ ์กด์žฌํ•œ๋‹ค.
    • ๋„๋กœ ํ•˜๋‚˜ ๋‹น 1์˜ ๊ฑฐ๋ฆฌ๋ฅผ ๊ฐ–๋Š”๋‹ค.
  • X๋ฒˆ ๋„์‹œ์—์„œ ์‹œ์ž‘ํ•ด ์ตœ๋‹จ๊ฑฐ๋ฆฌ๊ฐ€ ์ •ํ™•ํžˆ X ๊ฑฐ๋ฆฌ๋งŒํผ ๋–จ์–ด์ง„ ๋„์‹œ์˜ ๋ชฉ๋ก์„ ์ถœ๋ ฅํ•œ๋‹ค.

๐Ÿ”ธ ๋ฌธ์ œ ํ’€์ด ๐Ÿ”ธ

  • ํ•˜๋‚˜์˜ ๋…ธ๋“œ์—์„œ ๋‹ค๋ฅธ ๋ชจ๋“  ๋…ธ๋“œ๋“ค์˜ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•ด์•ผ ํ•˜๋ฏ€๋กœ ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•œ๋‹ค.
    • N์€30๋งŒ๊ฐœ์ธ๋ฐ, ์šฐ์„ ์ˆœ์œ„ ํ๋ฅผ ํ™œ์šฉํ•ด O(NlogN)์œผ๋กœ ํ•ด๊ฒฐํ•˜๋ฉด, ์•ฝ 16์–ต(1.6์ดˆ)์œผ๋กœ ์ œํ•œ์‹œ๊ฐ„ 2์ดˆ์— ์ถฉ๋ถ„ํ•˜๋‹ค.
  • ๊ฐ„์„ ์ด 100๋งŒ๊ฐœ ์ด๋ฉด์„œ ๊ฐ„์„  ๋‹น 1์˜ ๊ฐ€์ค‘์น˜๋ฅผ ๊ฐ€์ง€๋ฏ€๋กœ intํ˜•์œผ๋กœ ์ถฉ๋ถ„ํ•˜๋‹ค.

๐Ÿ”ธ ์ฝ”๋“œ ๐Ÿ”ธ

import java.io.*;
import java.util.ArrayList;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.StringTokenizer;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        StringTokenizer st = new StringTokenizer(br.readLine());
        StringBuilder sb = new StringBuilder();

        int N = Integer.parseInt(st.nextToken());
        int M = Integer.parseInt(st.nextToken());
        int K = Integer.parseInt(st.nextToken());
        int X = Integer.parseInt(st.nextToken());

        int[] city = new int[N + 1];
        ArrayList<Integer>[] adjList = new ArrayList[N + 1];
        for (int i = 0; i <= N; i++) {
            city[i] = Integer.MAX_VALUE;
            adjList[i] = new ArrayList<>();
        }

        for (int i = 0; i < M; i++) {
            st = new StringTokenizer(br.readLine());
            adjList[Integer.parseInt(st.nextToken())].add(Integer.parseInt(st.nextToken()));
        }

        Queue<Edge> pq = new PriorityQueue<>();
        pq.add(new Edge(X, X, 0));

        while (!pq.isEmpty()) {
            Edge edge = pq.poll();
            if (city[edge.end] > edge.cost) {
                city[edge.end] = edge.cost;

                for (int i : adjList[edge.end]) {
                    pq.add(new Edge(edge.end, i, edge.cost + 1));
                }
            }
        }

        for (int i = 1; i <= N; i++) {
            if (city[i] == K) {
                sb.append(i).append('\n');
            }
        }
        if (sb.length() == 0) {
            sb.append(-1);
        }

        bw.write(sb.toString().trim());
        bw.flush();
    }

    private static class Edge implements Comparable<Edge> {
        int start, end, cost;

        public Edge(int start, int end, int cost) {
            this.start = start;
            this.end = end;
            this.cost = cost;
        }

        @Override
        public int compareTo(Edge o) {
            return this.cost - o.cost;
        }
    }
}

๐Ÿ”ธ ์ฝ”๋“œ ํ•ด์„ ๐Ÿ”ธ

  • ๊ฐ„์„  ์ €์žฅ์„ ์œ„ํ•œ Edge ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์–ด, ์‹œ์ž‘๊ณผ ๋ ๋…ธ๋“œ ๋ฒˆํ˜ธ์™€ ๊ฐ€์ค‘์น˜๋ฅผ ์ €์žฅํ•ด ์‚ฌ์šฉํ–ˆ๋‹ค.
  • ๊ฐ„์„  ์ •๋ณด๋Š” ArrayList๋ฐฐ์—ด adjList์— ์ €์žฅํ•ด ์‚ฌ์šฉํ–ˆ๋‹ค.
  • X์—์„œ X๋กœ ๊ฐ€๋Š” ๊ฑฐ๋ฆฌ๋Š” 0์ด๋ผ๊ณ  ํ–ˆ์œผ๋ฏ€๋กœ, ์šฐ์„ ์ˆœ์œ„ ํ์— ์‹œ์ž‘ ๊ฐ’์œผ๋กœ ๋„ฃ๊ณ  ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‹คํ–‰ํ•œ๋‹ค.
    • ์ €์žฅ๋œ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๋ณด๋‹ค ์ž‘์€ ๊ฐ’์ด ๋‚˜์˜ค๋ฉด, ๊ฐ’์„ ๊ฐฑ์‹ ํ•˜๊ณ  ๋‹ค์‹œ ์—ฐ๊ฒฐ๋œ ๊ฐ„์„  ์ •๋ณด๋ฅผ ํ์— ๋„ฃ์–ด ํ™•์ธํ•œ๋‹ค.
  • ๋ชจ๋“  ๊ฑฐ๋ฆฌ๋ฅผ ๊ณ„์‚ฐ ํ›„, ๊ฐ’์ด K์ธ ๋…ธ๋“œ ๋ฒˆํ˜ธ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

๐Ÿ”ธ end ๐Ÿ”ธ

  •  ๋นŒ๋“œ ํ–ˆ์„ ๋•Œ๋Š” ์ž˜ ๋๋Š”๋ฐ, ์ œ์ถœํ•ด๋ณด๋‹ˆ StringBuilder ๋ถ€๋ถ„์— ์ปดํŒŒ์ผ ์—๋Ÿฌ๊ฐ€ ๋‚ฌ๋‹ค.
    • ๊ธฐ์กด ์ฝ”๋“œ๋Š” sb.isEmpty()์ด๋ฉด sb.append(-1)์„ ๋„ฃ๋Š” ๋ฐฉ์‹์œผ๋กœ, ๊ฑฐ๋ฆฌ๊ฐ€ K์ธ ๋„์‹œ๊ฐ€ ์—†์œผ๋ฉด -1์„ ๋„ฃ๊ณ ์ž ํ–ˆ๋‹ค.
    • ํ•˜์ง€๋งŒ isEmpty()์ดํ›„์— append๋ฅผ ์‚ฌ์šฉํ•˜๋Š”๊ฒŒ ๋ฐฑ์ค€ ์ปดํŒŒ์ผ๋Ÿฌ์—์„œ๋Š” ์—๋Ÿฌ๊ฐ€ ๋‚˜๋Š”๋“ฏ ํ•˜๋‹ค.
    • sb.length()๊ฐ€ 0์ธ์ง€ ํ™•์ธํ•˜๋Š” ์ฝ”๋“œ๋กœ ๋ณ€๊ฒฝํ–ˆ๋‹ค.
728x90