반응형
https://www.acmicpc.net/problem/1516
1516번: 게임 개발
첫째 줄에 건물의 종류 수 N(1 ≤ N ≤ 500)이 주어진다. 다음 N개의 줄에는 각 건물을 짓는데 걸리는 시간과 그 건물을 짓기 위해 먼저 지어져야 하는 건물들의 번호가 주어진다. 건물의 번호는 1부
www.acmicpc.net
#include <bits/stdc++.h>
using namespace std;
int n, m;
int degree[1001];
vector<int> adj[1001];
int cost[501];
int res[501];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) {
int t;
cin >> t;
cost[i] = t;
int a;
while (1) {
cin >> a;
if (a == -1) break;
adj[a].push_back(i);
degree[i]++;
}
}
queue<int> Q;
for (int i = 1; i <= n; i++) {
if (degree[i] == 0) {
res[i] = cost[i];
Q.push(i);
}
}
while (!Q.empty()) {
int x = Q.front();
Q.pop();
for (auto nx : adj[x]) {
res[nx] = max(res[nx], res[x] + cost[nx]);
if (--degree[nx] == 0) Q.push(nx);
}
}
for (int i = 1; i <= n; i++) {
cout << res[i] << '\n';
}
return 0;
}
위상정렬 다이나믹프로그래밍 문제
반응형
'Algorithm > problem' 카테고리의 다른 글
백준 1005번 : ACM Craft - 위상정렬 dp C++ (0) | 2022.02.24 |
---|---|
백준 1766번 : 문제집 - 위상정렬 C++ 코드 (0) | 2022.02.24 |
백준 2623번 : 음악프로그램 - 위상정렬 C++ 코드 (0) | 2022.02.24 |
백준 16946번 : 벽 부수고 이동하기 4 - BFS C++ 코드 (0) | 2022.02.23 |
백준 11495번 : 격자 0 만들기 - Network Flow C++ 코드 (0) | 2022.02.23 |