From ffcc2a6e52b33919900df52e6f2e7ca93c24465c Mon Sep 17 00:00:00 2001 From: lkhyun <102892446+lkhyun@users.noreply.github.com> Date: Fri, 26 Sep 2025 17:39:46 +0900 Subject: [PATCH] =?UTF-8?q?[20250926]=20BOJ=20/=20G4=20/=20=EC=9E=91?= =?UTF-8?q?=EC=97=85=20/=20=EC=9D=B4=EA=B0=95=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../26 BOJ G4 \354\236\221\354\227\205.md" | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 "lkhyun/202509/26 BOJ G4 \354\236\221\354\227\205.md" diff --git "a/lkhyun/202509/26 BOJ G4 \354\236\221\354\227\205.md" "b/lkhyun/202509/26 BOJ G4 \354\236\221\354\227\205.md" new file mode 100644 index 00000000..f5993bb4 --- /dev/null +++ "b/lkhyun/202509/26 BOJ G4 \354\236\221\354\227\205.md" @@ -0,0 +1,64 @@ +```java +import java.util.*; +import java.io.*; + +public class Main{ + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); + static StringTokenizer st; + static StringBuilder sb = new StringBuilder(); + static int N; + static List[] prevTask; + static int[] prevCnt; + static int[] cost; + static int ans = 0; + + public static void main(String[] args) throws Exception { + N = Integer.parseInt(br.readLine()); + prevTask = new List[N+1]; + for (int i = 1; i <= N; i++) { + prevTask[i] = new ArrayList<>(); + } + prevCnt = new int[N+1]; + cost = new int[N+1]; + + for (int i = 1; i <= N; i++) { + st = new StringTokenizer(br.readLine()); + cost[i] = Integer.parseInt(st.nextToken()); + int tmp = Integer.parseInt(st.nextToken()); + for (int j = 0; j < tmp; j++) { + prevTask[Integer.parseInt(st.nextToken())].add(i); + prevCnt[i]++; + } + } + solve(); + bw.write(ans+""); + bw.close(); + } + public static void solve(){ + PriorityQueue q = new PriorityQueue<>((a,b) -> Integer.compare(a[1],b[1])); + boolean[] visited = new boolean[N+1]; + for (int i = 1; i <= N; i++) { + if(prevCnt[i] == 0){ + q.offer(new int[]{i,cost[i]}); + visited[i] = true; + } + } + + while(!q.isEmpty()){ + int[] cur = q.poll(); + ans = Math.max(ans,cur[1]); + + for (int next : prevTask[cur[0]]) { + if(!visited[next]){ + prevCnt[next]--; + if(prevCnt[next] == 0){ + q.offer(new int[]{next,cur[1]+cost[next]}); + visited[next] = true; + } + } + } + } + } +} +```