Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- DFS
- COSPROJAVA1급
- 다익스트라
- 자바PS
- GatherTown
- 게더타운시작
- QUICKSTARTGUIDE
- dp
- COSPRO
- 01BFS
- 시뮬레이션
- PS
- 다이나믹프로그래밍
- deque
- 재귀함수
- 백준
- YBMCOS
- 완전탐색
- 네트워크플로우
- 구현
- java
- 백준코딩테스트
- 세그먼트트리
- 취득후기
- 엘라스틱서치
- spring
- 우선순위큐
- 이젠 골드구현도 어렵네..
- BFS
- 알고리즘
Archives
- Today
- Total
공부공간
BOJ - 2056 ) 작업 본문
https://www.acmicpc.net/problem/2056
작업의 우선순위 중, 최소시간을 구하기 위해서 위상정렬을 통하여
차수에 대한 누적합을 구해줍니다. 정렬이 끝난 후, Sum의 값중 가장 큰 값이 정답입니다.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.StringTokenizer;
public class 작업 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int N = Integer.parseInt(st.nextToken());
int cost[] = new int[N+1];
int degree[] = new int[N+1];
int sum[] = new int[N+1];
ArrayList<Integer> node[] = new ArrayList[N+1];
for(int i=0;i<N;i++) {
node[i+1] = new ArrayList<>();
st = new StringTokenizer(br.readLine());
cost[i+1] = Integer.parseInt(st.nextToken());
int next = Integer.parseInt(st.nextToken());
if(next>=1) {
for(int j=0;j<next;j++) {
int connect = Integer.parseInt(st.nextToken());
degree[i+1]++;
node[connect].add(i+1);
}
}
}
// int[] 첫번째는 번호 뒤에는 누적합
ArrayDeque<int []> dq = new ArrayDeque<>();
for(int i=1;i<N+1;i++) {
sum[i]=cost[i];
if(degree[i]==0) {
dq.add(new int[] {i,cost[i]});
}
}
while(!dq.isEmpty()) {
int now[] = dq.poll();
int size = node[now[0]].size();
for(int i=0;i<size;i++) {
int next = node[now[0]].get(i);
int next_cost = sum[now[0]] + cost[next];
if(sum[next] < next_cost) {
sum[next] = next_cost;
}
if( (--degree[next]) ==0) {
dq.add(new int[] {next,next_cost});
}
}
}
int answer = -1;
for(int i=1;i<N+1;i++) {if(sum[i] > answer) {answer =sum[i];}}
System.out.println(answer);
}
}
'알고리즘 > 위상정렬' 카테고리의 다른 글
BOJ - 1766 ) 문제집 (1) | 2020.11.08 |
---|---|
BOJ - 1005 ) ACM Craft (0) | 2020.05.11 |
BOJ - 2623 ) 음악프로그램 (2) | 2020.03.24 |
BOJ - 2252 ) 줄 세우기 (0) | 2020.03.23 |
Comments