백준 1516 게임개발
유형
코드
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.StringTokenizer;
public class BOJ1516 {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer stz;
static int N;
static List<List<Integer>> arr;
static Queue<Integer> q;
static int[] indegree;
static int[] time;
static int[] dp;
public static void main(String[] args) throws NumberFormatException, IOException {
N = Integer.parseInt(br.readLine());
arr = new ArrayList<List<Integer>>();
for(int i = 0; i <= N; i++) {
arr.add(new ArrayList<Integer>());
}
indegree = new int[N+1];
time = new int[N+1];
dp = new int[N+1];
int next;
for(int i = 1; i <= N; i++) {
stz = new StringTokenizer(br.readLine());
time[i] = Integer.parseInt(stz.nextToken());
while(true) {
next = Integer.parseInt(stz.nextToken());
if(next== -1) break;
indegree[i]++;
arr.get(next).add(i);
}
}
findMinimumTime();
}
static void findMinimumTime() {
StringBuilder sb = new StringBuilder();
q = new LinkedList<Integer>();
for(int i = 1; i <= N ; i++) {
if(indegree[i] == 0) {
q.offer(i);
dp[i] = time[i];
}
}
while(!q.isEmpty()) {
int cur = q.poll();
for(Integer next : arr.get(cur)) {
indegree[next]--;
if(indegree[next] == 0) q.offer(next);
dp[next] = Math.max(dp[next], dp[cur] + time[next]);
}
}
for(int i = 1 ; i <= N; i++) {
sb.append(dp[i]+"\n");
}
System.out.println(sb);
}
}