문제 링크입니다: https://www.acmicpc.net/problem/1516
1516번: 게임 개발
첫째 줄에 건물의 종류 수 N(1 ≤ N ≤ 500)이 주어진다. 다음 N개의 줄에는 각 건물을 짓는데 걸리는 시간과 그 건물을 짓기 위해 먼저 지어져야 하는 건물들의 번호가 주어진다. 건물의 번호는 1부터 N까지로 하고, 각 줄은 -1로 끝난다고 하자. 각 건물을 짓는데 걸리는 시간은 100,000보다 작거나 같은 자연수이다.
www.acmicpc.net
간단한 위상정렬 문제였습니다.
핵심은 max(result[nextNode], result[node] + duration[node]); 이 부분이였습니다.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
#include <queue> | |
using namespace std; | |
const int MAX = 500 + 1; | |
int N; | |
int inDegree[MAX]; | |
int duration[MAX]; | |
int result[MAX]; | |
vector<int> graph[MAX]; | |
int main(void) | |
{ | |
ios_base::sync_with_stdio(0); | |
cin.tie(0); | |
cin >> N; | |
for (int i = 1; i <= N; i++) | |
{ | |
int time; | |
cin >> time; | |
duration[i] = time; | |
while (1) | |
{ | |
int node; | |
cin >> node; | |
if (node == -1) | |
{ | |
break; | |
} | |
inDegree[i]++; | |
graph[node].push_back(i); | |
} | |
} | |
queue<int> q; | |
for (int i = 1; i <= N; i++) | |
{ | |
if (inDegree[i] == 0) | |
{ | |
q.push(i); | |
} | |
} | |
while (!q.empty()) | |
{ | |
int node = q.front(); | |
q.pop(); | |
for (int i = 0; i < graph[node].size(); i++) | |
{ | |
int nextNode = graph[node][i]; | |
result[nextNode] = max(result[nextNode], result[node] + duration[node]); | |
if (--inDegree[nextNode] == 0) | |
{ | |
q.push(nextNode); | |
} | |
} | |
} | |
for (int i = 1; i <= N; i++) | |
{ | |
cout << result[i] + duration[i] << "\n"; | |
} | |
return 0; | |
} |


개발환경:Visual Studio 2017
지적, 조언, 질문 환영입니다! 댓글 남겨주세요~
반응형
'알고리즘 > BOJ' 카테고리의 다른 글
백준 5635번 생일 (2) | 2019.10.02 |
---|---|
백준 6118번 숨바꼭질 (0) | 2019.09.29 |
백준 1766번 문제집 (0) | 2019.09.29 |
백준 16674번 2018년을 되돌아보며 (0) | 2019.09.25 |
백준 2842번 집배원 한상덕 (0) | 2019.09.21 |