๊ธฐ๋ก๋ฐฉ

BOJ_1948 : ์ž„๊ณ„๊ฒฝ๋กœ ๋ณธ๋ฌธ

CodingTest/Java

BOJ_1948 : ์ž„๊ณ„๊ฒฝ๋กœ

Soom_1n 2024. 9. 12. 16:48

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


 


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

  • N๊ฐœ์˜ ๋„์‹œ ์‚ฌ์ด M๊ฐœ์˜ ๋„๋กœ๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๊ฐ ๋„๋กœ๋ฅผ ๊ฑด๋„ˆ๋Š” ์‹œ๊ฐ„์ด ์žˆ๊ณ , ์ถœ๋ฐœ์ง€์™€ ๋„์ฐฉ์ง€๊ฐ€ ์ฃผ์–ด์ง„๋‹ค.
  • ๋„๋กœ๋Š” ์ถœ๋ฐœ์ง€๋ถ€ํ„ฐ ๋„์ฐฉ์ง€๊นŒ์ง€ ๋‹จ๋ฐฉํ–ฅ ๋น„์ˆœํ™˜ ๊ทธ๋ž˜ํ”„์ด๋‹ค.
  • ๋งŽ์€ ๊ฒฝ๋กœ ์ค‘์— ๊ฐ€์žฅ ๋Šฆ๊ฒŒ ๋„์ฐฉํ•˜๋Š” ๊ฒฝ๋กœ์—์„œ ์ง€๋‚˜๋Š” ๋„๋กœ์˜ ์ˆ˜ ์ดํ•ฉ์„ ์ถœ๋ ฅํ•œ๋‹ค.

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

  • ์ถœ๋ฐœ์ง€๋ถ€ํ„ฐ ๋„์ฐฉ์ง€๊นŒ์ง€ ๋‹จ๋ฐฉํ–ฅ ๋น„์ˆœํ™˜ ๊ทธ๋ž˜ํ”„์ด๋ฏ€๋กœ, ์œ„์ƒ ์ •๋ ฌ(Topology Sort)์„ ํ†ตํ•ด ์ตœ๋Œ€๊ฐ’์„ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ๊ฑด๋„ˆ์˜จ ๊ฒฝ๋กœ์˜ ์ˆ˜๋ฅผ ์นด์šดํŠธ ํ•ด์•ผํ•˜๋ฏ€๋กœ ์—ญ๋ฐฉํ–ฅ ํƒ์ƒ‰์ด ํ•„์š”ํ•˜๋‹ค.
    • 1๋ถ„๋„ ์‰ฌ์ง€ ์•Š๊ณ  ๋‹ฌ๋ฆฐ ๊ฒฝ๋กœ์ด๋ฏ€๋กœ ํ˜„์žฌ ์‹œ๊ฐ„๊ณผ ๋„๋กœ์˜ ์†Œ์š” ์‹œ๊ฐ„์˜ ์ฐจ์ด๊ฐ€ ๋งž์•„ ๋–จ์–ด์ ธ์•ผ ํ•œ๋‹ค.
    • ๊ฒฝ๋กœ๋ฅผ ์ค‘๋ณต๋˜๊ฒŒ ์ฒดํฌํ•˜์ง€ ์•Š๊ธฐ ์œ„ํ•ด์„œ ๋„์‹œ๋ฅผ ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ ํ•œ๋‹ค.

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

import java.io.*;
import java.util.ArrayDeque;
import java.util.ArrayList;
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));
        StringBuilder sb = new StringBuilder();
        StringTokenizer st;

        int n = Integer.parseInt(br.readLine());
        int m = Integer.parseInt(br.readLine());
        int[] indegree = new int[n + 1];

        ArrayList<ArrayList<Edge>> adjList = new ArrayList<>();
        ArrayList<ArrayList<Edge>> adjList_reverse = new ArrayList<>();

        for (int i = 0; i <= n; i++) {
            adjList.add(new ArrayList<>());
            adjList_reverse.add(new ArrayList<>());
        }

        for (int i = 0; i < m; i++) {
            st = new StringTokenizer(br.readLine());
            int out = Integer.parseInt(st.nextToken());
            int in = Integer.parseInt(st.nextToken());
            int time = Integer.parseInt(st.nextToken());
            adjList.get(out).add(new Edge(in, time));
            adjList_reverse.get(in).add(new Edge(out, time));
            indegree[in]++;
        }

        st = new StringTokenizer(br.readLine());
        int start = Integer.parseInt(st.nextToken());
        int end = Integer.parseInt(st.nextToken());


        // ์œ„์ƒ ์ •๋ ฌ
        int[] critical_path = new int[n + 1]; // ๊ฑธ๋ฆฐ ์‹œ๊ฐ„์˜ ์ตœ๋Œ“๊ฐ’
        Queue<Integer> que = new ArrayDeque<>();
        que.offer(start);

        while (!que.isEmpty()) {
            int now = que.poll();

            for (Edge next : adjList.get(now)) {
                critical_path[next.v] = Math.max(critical_path[next.v], critical_path[now] + next.time);
                if (--indegree[next.v] == 0) {
                    que.offer(next.v);
                }
            }
        }

        // ์—ญ๋ฐฉํ–ฅ ์œ„์ƒ ์ •๋ ฌ
        boolean[] visited = new boolean[n + 1];
        que.offer(end);
        int cnt = 0;

        while (!que.isEmpty()) {
            int now = que.poll();
            for (Edge next : adjList_reverse.get(now)) {
                if (critical_path[now] == critical_path[next.v] + next.time) {
                    cnt++;
                    if (!visited[next.v]) {
                        que.offer(next.v);
                        visited[next.v] = true;
                    }
                }
            }
        }

        sb.append(critical_path[end]).append('\n').append(cnt);
        bw.write(sb.toString());
        bw.flush();
    }

    private static class Edge {
        int v;
        int time;

        public Edge(int v, int time) {
            this.v = v;
            this.time = time;
        }
    }
}

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

  • ์ง„์ž… ์ฐจ์ˆ˜ intํ˜• ๋ฐฐ์—ด indegree, ์ธ์ ‘ ๋ฆฌ์ŠคํŠธ adjList์™€ ์—ญ๋ฐฉํ–ฅ ์ธ์ ‘ ๋ฆฌ์ŠคํŠธ adjList_reverse๋ฅผ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค.
  • ๋จผ์ € ์œ„์ƒ ์ •๋ ฌ์„ ํ†ตํ•ด ๊ฐ ๋„์‹œ์— ๋„์ฐฉํ•˜๋Š” ์ตœ๋Œ€ ์‹œ๊ฐ„(์ž„๊ณ„ ๊ฒฝ๋กœ)๋ฅผ ๊ตฌํ•œ๋‹ค.
    • ํ˜„์žฌ ๋„์‹œ์—์„œ ๋‹ค์Œ ๋„์‹œ๋กœ ๊ฐ€๋Š” ๊ฒฝ๋กœ๊ฐ€ ๋” ํฐ ๊ฐ’์ด๋ฉด ์—…๋ฐ์ดํŠธ (DP ๋Š๋‚Œ)
    • ๋‹ค์Œ ๋„์‹œ์˜ ์ง„์ž… ์ฐจ์ˆ˜๋ฅผ -1 ํ•ด์ฃผ๊ณ , ๋งŒ์•ฝ 0์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ฒฝ๋กœ๋ฅผ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด ํ์— ๋„ฃ๋Š”๋‹ค.
    • ์ตœ์ข… ๋„์ฐฉ์ง€์˜ ์ž„๊ณ„ ๊ฒฝ๋กœ ๊ฐ’์ด ๋„์ฐฉ๊นŒ์ง€ ๊ฑธ๋ฆฌ๋Š” ์ตœ๋Œ€ ์‹œ๊ฐ„์ด๋‹ค.
  • ์—ญ๋ฐฉํ–ฅ ์œ„์ƒ ์ •๋ ฌ์„ ํ†ตํ•ด ๊ฑด๋„ˆ์˜จ ๊ฐ„์„ ์˜ ์ˆ˜๋ฅผ ์นด์šดํŠธํ•œ๋‹ค.
    • ์ˆœ๋ฐฉํ–ฅ ์œ„์ƒ ์ •๋ ฌ๊ณผ ๋‹ค๋ฅธ ์ ์€, 1๋ถ„๋„ ๊ธฐ๋‹ค๋ ค์„œ๋Š” ์•ˆ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์‹œ๊ฐ„์ด ์ •ํ™•ํžˆ ์ผ์น˜ํ•ด์•ผ ํ•œ๋‹ค.
      (๋‹ค์Œ ๋„์‹œ๋กœ ๊ฐ€๋Š” ์‹œ๊ฐ„ + ๋‹ค์Œ ๋„์‹œ ์ž„๊ณ„ ๊ฒฝ๋กœ๊ฐ’ = ํ˜„์žฌ ๋„์‹œ ์ž„๊ณ„ ๊ฒฝ๋กœ๊ฐ’)
    • ์ผ์น˜ํ•œ๋‹ค๋ฉด, ํ•ด๋‹น ๊ฒฝ๋กœ๊ฐ€ ์ง€๋‚˜์˜จ ๊ธธ์ด๋ฏ€๋กœ cnt+1
    • ์ค‘๋ณต์„ ํ”ผํ•˜๊ธฐ ์œ„ํ•ด ๋ฐฉ๋ฌธ ์ฒดํฌ๋กœ 1๋ฒˆ๋งŒ ๋‹ค์Œ ๋„์‹œ๋ฅผ ํ์— ๋„ฃ๋Š”๋‹ค.
    • ์ด ์นด์šดํŠธ ๋œ cnt ๊ฐ’์ด ์ง€๋‚˜์˜จ ๊ฒฝ๋กœ์˜ ์ดํ•ฉ์ด๋‹ค.

๐Ÿ”ธ end ๐Ÿ”ธ

  • ์œ„์ƒ ์ •๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๊ณต๋ถ€ํ•˜๋ฉฐ ์ตœ์ข…์œผ๋กœ ํ’€์–ด๋ณธ ์–ด๋ ค์šด ๋ฌธ์ œ์ธ๋ฐ...๋„ˆ๋ฌด ์–ด๋ ค์› ๋‹ค.
  • 3์‹œ๊ฐ„์€ ์žก๊ณ ์žˆ๋˜ ๊ฒƒ ๊ฐ™์€๋ฐ, ์œ„์ƒ ์ •๋ ฌ์„ ์™„๋ฒฝํžˆ ์ดํ•ดํ•˜๊ณ , ๊ทธ๋ž˜ํ”„ ๋ฌธ์ œ์—์„œ ์ข…์ข… ๋‚˜์˜ค๋Š” ์—ญ๋ฐฉํ–ฅ ํƒ์ƒ‰๊นŒ์ง€ ํ•„์š”๋กœ ํ–ˆ๋‹ค.
  • ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ๊ฐ„๋‹จํ•˜๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ๋Š”๋ฐ, ํ™œ์šฉ ์˜์—ญ์ด ๋„ˆ๋ฌด ๋‹ค์–‘ํ•ด์„œ ์•„์ง ํ•œ์ฐธ ์—ฐ์Šต์ด ํ•„์š”ํ•œ ๊ฒƒ ๊ฐ™๋‹ค.

728x90