Notice
Recent Posts
Recent Comments
Today
Total
01-25 13:57
Link
๊ด€๋ฆฌ ๋ฉ”๋‰ด

Partially Committed

[๋ฐฑ์ค€ 1516] ๊ฒŒ์ž„ ๊ฐœ๋ฐœํ•˜๊ธฐ (JAVA) ๋ณธ๋ฌธ

๐Ÿ”ฅ Algorithm || ๋ฌธ์ œํ’€์ด/PS

[๋ฐฑ์ค€ 1516] ๊ฒŒ์ž„ ๊ฐœ๋ฐœํ•˜๊ธฐ (JAVA)

WonderJay 2023. 1. 17. 13:40
728x90
๋ฐ˜์‘ํ˜•
SMALL

๋ฌธ์ œ

https://www.acmicpc.net/problem/1516

 

1516๋ฒˆ: ๊ฒŒ์ž„ ๊ฐœ๋ฐœ

์ฒซ์งธ ์ค„์— ๊ฑด๋ฌผ์˜ ์ข…๋ฅ˜ ์ˆ˜ N(1 ≤ N ≤ 500)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‹ค์Œ N๊ฐœ์˜ ์ค„์—๋Š” ๊ฐ ๊ฑด๋ฌผ์„ ์ง“๋Š”๋ฐ ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„๊ณผ ๊ทธ ๊ฑด๋ฌผ์„ ์ง“๊ธฐ ์œ„ํ•ด ๋จผ์ € ์ง€์–ด์ ธ์•ผ ํ•˜๋Š” ๊ฑด๋ฌผ๋“ค์˜ ๋ฒˆํ˜ธ๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๊ฑด๋ฌผ์˜ ๋ฒˆํ˜ธ๋Š” 1๋ถ€

www.acmicpc.net

์ˆŒ ํšŒ์‚ฌ์—์„œ ์ด๋ฒˆ์— ์ƒˆ๋กœ์šด ์ „๋žต ์‹œ๋ฎฌ๋ ˆ์ด์…˜ ๊ฒŒ์ž„ ์„ธ์ค€ ํฌ๋ž˜ํ”„ํŠธ๋ฅผ ๊ฐœ๋ฐœํ•˜๊ธฐ๋กœ ํ•˜์˜€๋‹ค. ํ•ต์‹ฌ์ ์ธ ๋ถ€๋ถ„์€ ๊ฐœ๋ฐœ์ด ๋๋‚œ ์ƒํƒœ๊ณ , ์ข…์กฑ๋ณ„ ๊ท ํ˜•๊ณผ ์ „์ฒด ๊ฒŒ์ž„ ์‹œ๊ฐ„ ๋“ฑ์„ ์กฐ์ ˆํ•˜๋Š” ๋ถ€๋ถ„๋งŒ ๋‚จ์•„ ์žˆ์—ˆ๋‹ค.

๊ฒŒ์ž„ ํ”Œ๋ ˆ์ด์— ๋“ค์–ด๊ฐ€๋Š” ์‹œ๊ฐ„์€ ์ƒํ™ฉ์— ๋”ฐ๋ผ ๋‹ค๋ฅผ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์—, ๋ชจ๋“  ๊ฑด๋ฌผ์„ ์ง“๋Š”๋ฐ ๊ฑธ๋ฆฌ๋Š” ์ตœ์†Œ์˜ ์‹œ๊ฐ„์„ ์ด์šฉํ•˜์—ฌ ๊ทผ์‚ฌํ•˜๊ธฐ๋กœ ํ•˜์˜€๋‹ค. ๋ฌผ๋ก , ์–ด๋–ค ๊ฑด๋ฌผ์„ ์ง“๊ธฐ ์œ„ํ•ด์„œ ๋‹ค๋ฅธ ๊ฑด๋ฌผ์„ ๋จผ์ € ์ง€์–ด์•ผ ํ•  ์ˆ˜๋„ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ฌธ์ œ๊ฐ€ ๋‹จ์ˆœํ•˜์ง€๋งŒ์€ ์•Š์„ ์ˆ˜๋„ ์žˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค๋ฉด ์Šคํƒ€ํฌ๋ž˜ํ”„ํŠธ์—์„œ ๋ฒ™์ปค๋ฅผ ์ง“๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋ฐฐ๋Ÿญ์„ ๋จผ์ € ์ง€์–ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ๋ฐฐ๋Ÿญ์„ ๋จผ์ € ์ง€์€ ๋’ค ๋ฒ™์ปค๋ฅผ ์ง€์–ด์•ผ ํ•œ๋‹ค. ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๊ฑด๋ฌผ์„ ๋™์‹œ์— ์ง€์„ ์ˆ˜ ์žˆ๋‹ค.

ํŽธ์˜์ƒ ์ž์›์€ ๋ฌดํ•œํžˆ ๋งŽ์ด ๊ฐ€์ง€๊ณ  ์žˆ๊ณ , ๊ฑด๋ฌผ์„ ์ง“๋Š” ๋ช…๋ น์„ ๋‚ด๋ฆฌ๊ธฐ๊นŒ์ง€๋Š” ์‹œ๊ฐ„์ด ๊ฑธ๋ฆฌ์ง€ ์•Š๋Š”๋‹ค๊ณ  ๊ฐ€์ •ํ•˜์ž.

์ž…๋ ฅ

์ฒซ์งธ ์ค„์— ๊ฑด๋ฌผ์˜ ์ข…๋ฅ˜ ์ˆ˜ N(1 ≤ N ≤ 500)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‹ค์Œ N๊ฐœ์˜ ์ค„์—๋Š” ๊ฐ ๊ฑด๋ฌผ์„ ์ง“๋Š”๋ฐ ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„๊ณผ ๊ทธ ๊ฑด๋ฌผ์„ ์ง“๊ธฐ ์œ„ํ•ด ๋จผ์ € ์ง€์–ด์ ธ์•ผ ํ•˜๋Š” ๊ฑด๋ฌผ๋“ค์˜ ๋ฒˆํ˜ธ๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๊ฑด๋ฌผ์˜ ๋ฒˆํ˜ธ๋Š” 1๋ถ€ํ„ฐ N๊นŒ์ง€๋กœ ํ•˜๊ณ , ๊ฐ ์ค„์€ -1๋กœ ๋๋‚œ๋‹ค๊ณ  ํ•˜์ž. ๊ฐ ๊ฑด๋ฌผ์„ ์ง“๋Š”๋ฐ ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„์€ 100,000๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜์ด๋‹ค. ๋ชจ๋“  ๊ฑด๋ฌผ์„ ์ง“๋Š” ๊ฒƒ์ด ๊ฐ€๋Šฅํ•œ ์ž…๋ ฅ๋งŒ ์ฃผ์–ด์ง„๋‹ค.

์ถœ๋ ฅ

N๊ฐœ์˜ ๊ฐ ๊ฑด๋ฌผ์ด ์™„์„ฑ๋˜๊ธฐ๊นŒ์ง€ ๊ฑธ๋ฆฌ๋Š” ์ตœ์†Œ ์‹œ๊ฐ„์„ ์ถœ๋ ฅํ•œ๋‹ค.

์˜ˆ์ œ ์ž…๋ ฅ 1

5
10 -1
10 1 -1
4 1 -1
4 3 1 -1
3 3 -1

์˜ˆ์ œ ์ถœ๋ ฅ 1

10
20
14
18
17

  ๊ฐ๊ฐ์˜ ๊ฑด๋ฌผ์€ ์ง€์–ด์ง€๋Š” ๋ฐ์— ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„ my_time[i] ๊ณผ ํ…ŒํฌํŠธ๋ฆฌ(์„ ํ–‰ํ•ด์„œ ์ง€์–ด์ ธ์•ผ ํ•˜๋Š” ๊ฑด๋ฌผ)๊ฐ€ ์žˆ๋‹ค.

๊ทธ๋Ÿฌํ•œ ์ƒํ™ฉ์—์„œ, ๊ฐ๊ฐ์˜ ๊ฑด๋ฌผ์ด ์ง€์–ด์งˆ ์ˆ˜ ์žˆ๋Š” ์ตœ์†Œ์˜ ์‹œ๊ฐ„์„ ์ถœ๋ ฅํ•˜๋ฉด ๋œ๋‹ค.

 

  ์ฆ‰ ๊ฐ๊ฐ์˜ ๊ฑด๋ฌผ์„ Node ๋กœ ํ‘œํ˜„ํ•˜๊ณ  ํ…ŒํฌํŠธ๋ฆฌ๋ฅผ Edge ๋กœ ํ‘œํ˜„ํ•˜๋ฉด direct graph ์˜ ํ˜•ํƒœ๋ฅผ ๊ฐ€์ง€๋ฉฐ, ์‚ฌ์ดํด์ด ์กด์žฌํ•˜์ง€ ์•Š๋Š”๋‹ค. ์ด๋Ÿฌํ•œ ์ƒํ™ฉ์—์„œ ๊ฐ์ž์˜ ํ…ŒํฌํŠธ๋ฆฌ๋Œ€๋กœ ๊ฑด๋ฌผ์„ ๊ฑด์„คํ•˜์˜€์„ ๋•Œ, ์ตœ์†Œ์˜ ๊ฑด์„ค ์‹œ๊ฐ„์„ ์•Œ์•„๋‚ด๊ธฐ ์œ„ํ•ด์„œ๋Š” ์œ„์ƒ์ •๋ ฌ์„ ์ด์šฉํ•œ๋‹ค.

 

๊ตฌํ˜„ ๊ณผ์ •

1. ArrayList<ArrayList<Integer>> graph ์— ๋ฐ์ดํ„ฐ ์ €์žฅ

2. ํ˜„์žฌ ์ž๊ธฐ ์ž์‹ ์˜ ๊ฑด์„ค ์‹œ๊ฐ„์„ my_times ๋ฐฐ์—ด์— ์ €์žฅ

3. indegree ๋ฐฐ์—ด ๊ณ„์‚ฐ

4. indegree ๊ฐ€ 0 ์ธ ๋…ธ๋“œ๋ฅผ Queue ์— offer

5. Queue ๊ฐ€ ๋นŒ ๋•Œ ๊นŒ์ง€ ์•„๋ž˜๋ฅผ ๋ฐ˜๋ณต

5.1 now = queue.poll()

5.2 now ์— ๋Œ€ํ•˜์—ฌ ์—ฐ๊ฒฐ ์„ฑ๋ถ„์„ ํƒ์ƒ‰ ( ์—ฐ๊ฒฐ๋œ ๋…ธ๋“œ๋ฅผ cur ์ด๋ผ๊ณ  ๊ฐ€์ •ํ•ด๋ณด์ž.)

      ๊ทธ๋Ÿฌ๋ฉด, minimum_time[cur] /*cur ์ด ์™„์„ฑ๋˜๊ธฐ๊นŒ์ง€ ๊ฑธ๋ฆฌ๋Š” ์ตœ์†Œ ์‹œ๊ฐ„*/  = Max(minimum_time[cur], minimum_time[now] + my_time[now]) ์ด๋‹ค.

5.3 indegree[cur] --

5.4 indegree[cur] == 0 ์ด๋ฉด queue ์— ๋„ฃ๋Š”๋‹ค.

6. queue ๊ฐ€ ๋น„๋ฉด, minimum_time (์™„์„ฑ์— ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„)์˜ ์›์†Œ์— ๊ฐ๊ฐ my_time (์„ ํ–‰ ๊ฑด๋ฌผ ๊ณ ๋ คํ•˜์ง€ ์•Š๊ณ  ์ž๊ธฐ ์ž์‹ ์„ ๊ฑด์„คํ•˜๋Š” ๋ฐ ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„)์˜ ์›์†Œ๋ฅผ ๋”ํ•ด์„œ ์ถœ๋ ฅํ•œ๋‹ค.

 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.LinkedList;
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));
        StringTokenizer stk = new StringTokenizer(br.readLine());

        int n = Integer.parseInt(stk.nextToken());

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

        int[] my_time = new int[n + 1];
        int[] minimum_time = new int[n + 1];
        int[] indegree = new int[n + 1];
        for (int i = 1; i <= n; i++) {
            stk = new StringTokenizer(br.readLine());
            my_time[i] = Integer.parseInt(stk.nextToken());
            while (true) {
                int t = Integer.parseInt(stk.nextToken());
                if (t == -1)
                    break;
                else {
                    graph.get(t).add(i);
                    indegree[i]++;
                }
            }
        }
        Queue<Integer> q = new LinkedList<>();
        for (int i = 1; i <= n; i++) {
            if (indegree[i] == 0) {
                q.offer(i);
            }
        }
        while (!q.isEmpty()) {
            int now = q.poll();
            for (int cur : graph.get(now)) {
                // ์—ฐ๊ฒฐ ์„ฑ๋ถ„ ํƒ์ƒ‰
                minimum_time[cur] = Math.max(minimum_time[cur], minimum_time[now] + my_time[now]);
                indegree[cur]--;
                if (indegree[cur] == 0)
                    q.add(cur);
            }
        }
        for (int i = 1; i <= n; i++) {
            minimum_time[i] += my_time[i];
            System.out.println(minimum_time[i]);
        }
    }
}

728x90
๋ฐ˜์‘ํ˜•
LIST
Comments