[BOJ] 1005. ACM Craft (๐Ÿฅ‡, DP/์œ„์ƒ ์ •๋ ฌ)

lemythe423ยท2024๋…„ 1์›” 29์ผ
0

BOJ ๋ฌธ์ œํ’€์ด

๋ชฉ๋ก ๋ณด๊ธฐ
104/133
post-thumbnail

๐Ÿ”—

ํ’€์ด

1 -> 2, 3 -> 4 ์™€ 5 -> 4 2๊ฐ€์ง€ ๊ฒฝ๋กœ๊ฐ€ ์กด์žฌํ•  ์ˆ˜ ์žˆ๋‹ค.

์ด๋•Œ ๋งค ๋…ธ๋“œ๋งˆ๋‹ค ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„๋“ค์„ ๊ฐฑ์‹ ํ•ด์•ผ ํ•œ๋‹ค. 2, 3์˜ ๊ฒฝ์šฐ 1์ด๋ผ๋Š” ๊ฐ™์€ ์ถœ๋ฐœ์ง€๋ฅผ ๊ฐ–๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— 30๊ณผ 40์˜ ์‹œ๊ฐ„์ด ์ตœ์†Œ๋กœ ๊ฑธ๋ฆฌ๊ฒŒ ๋œ๋‹ค.

์ด์ œ 4๋Š” 2, 3, 5 ์ค‘ ํ•˜๋‚˜์—์„œ ๋„๋‹ฌํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋˜๋Š”๋ฐ, ์ด๋•Œ ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„์˜ ์ตœ์†Œ๊ฐ’์„ ๊ตฌํ•˜๋ผ๊ณ  ํ•˜์ง€๋งŒ ์‹ค์ œ๋กœ๋Š” 2, 3, 5๊ฐ€ ๋ชจ๋‘ ๊ฑด์„ค๋˜์–ด์•ผ 4๊ฐ€ ๊ฑด์„ค๋  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ๊ฒฐ๊ตญ 2, 3, 5๊ฐ€ ๋ชจ๋‘ ๊ฑด์„ค๋  ์ˆ˜ ์žˆ๋Š” ์‹œ๊ฐ„ ์ฆ‰ 2, 3, 5 ์ค‘ ๊ฑด์„ค ์‹œ๊ฐ„์˜ ์ตœ๋Œ€๊ฐ’์„ ๊ตฌํ•ด์•ผ ํ•œ๋‹ค. ๊ทธ ์ตœ๋Œ€๊ฐ’์— 4์— ๊ฑด์„ค๋  ์‹œ๊ฐ„์„ ๋”ํ•˜๋ฉด ๋œ๋‹ค.

๊ฒฐ๊ตญ ๋ชฉ์  ๋…ธ๋“œ์— ๋„๋‹ฌํ•  ์ˆ˜ ์žˆ๋Š” ์„ ํ–‰ ๋…ธ๋“œ๋“ค ๊ฐ€์šด๋ฐ ์ตœ๋Œ€๊ฐ’์„ ๊ตฌํ•˜๊ณ  ๊ทธ ๊ฐ’๊ณผ ๋ชฉ์  ๋…ธ๋“œ์˜ ๊ฑด์„ค ์‹œ๊ฐ„์„ ๋”ํ•ด์„œ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค.

import java.io.*;
import java.util.*;

public class Main {
    static BufferedReader br;
    static int solution() throws IOException {
        StringTokenizer st = new StringTokenizer(br.readLine());
        int N = Integer.parseInt(st.nextToken());
        int K = Integer.parseInt(st.nextToken());

        int[] builtTime = new int[N+1];
        int[] connectCount = new int[N+1];
        List<List<Integer>> builtOrder = new ArrayList<>();
        for (int i = 0; i <= N; i++) {
            builtOrder.add(new ArrayList<>());
        }

        st = new StringTokenizer(br.readLine());
        for (int i = 1; i <= N; i++) {
            builtTime[i] = Integer.parseInt(st.nextToken());
        }
        
        int X;
        int Y;
        for (int i = 0; i < K; i++) {
            st = new StringTokenizer(br.readLine());
            X = Integer.parseInt(st.nextToken());
            Y = Integer.parseInt(st.nextToken());
            connectCount[Y]++;
            builtOrder.get(X).add(Y);
        }

        int W = Integer.parseInt(br.readLine());
        
        Queue<Integer> queue = new LinkedList<>();
        for (int i = 1; i <= N; i++) {
            if (connectCount[i] == 0) {
                queue.add(i);
            }
        }

        int[] answer = Arrays.copyOf(builtTime, N+1);
        while (!queue.isEmpty()) {
            Queue<Integer> nextQueue = new LinkedList<>();
            while (!queue.isEmpty()) {
                int w = queue.poll();
                int timeW = answer[w];

                for (int nextW : builtOrder.get(w)) {
                    connectCount[nextW]--;
                    answer[nextW] = Math.max(answer[nextW], builtTime[nextW] + timeW);
                    if (connectCount[nextW] == 0) {
                        nextQueue.add(nextW);
                    }
                }
            }
            queue = nextQueue;
        }
        return answer[W];
    }
    public static void main(String[] args) throws IOException {
        br = new BufferedReader(new InputStreamReader(System.in));
        int T = Integer.parseInt(br.readLine());

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < T; i++) {
            sb.append(solution() + "\n");
        }
        sb.replace(sb.length()-1, sb.length(), "");
        System.out.println(sb);
    }
}
profile
์•„๋ฌด๋ง์ด๋‚˜ํ•˜๊ธฐ

0๊ฐœ์˜ ๋Œ“๊ธ€