SOL)
BFS
ACM Craft와 마찬가지로 indgree를 생각해주면서 풀면 정답
[-] Collapse
#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;
vector<vector<int> > adj;
queue<int> q;
int indgree[10001], cost[10001], Time[10001];
int search(){
int ans = 0;
while (!q.empty()){
int size = q.size();
for (int i = 0; i < size; i++){
int here = q.front();
q.pop();
Time[here] += cost[here];
ans = max(ans, Time[here]);
for (int j = 0; j < adj[here].size(); j++){
int next = adj[here][j];
Time[next] = max(Time[next], Time[here]);
if (--indgree[next] > 0) continue;
q.push(next);
}
}
}
return ans;
}
int main(){
int n; scanf("%d", &n);
adj = vector<vector<int> >(n + 1);
for (int i = 1; i <= n; i++){
int E; scanf("%d%d", &cost[i], &E);
for (int j = 0; j < E; j++){
int vertex; scanf("%d", &vertex);
adj[vertex].push_back(i);
indgree[i]++;
}
}
for (int i = 1; i <= n; i++){
if (indgree[i] == 0) q.push(i);
}
printf("%d", search());
}
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;
vector<vector<int> > adj;
queue<int> q;
int indgree[10001], cost[10001], Time[10001];
int search(){
int ans = 0;
while (!q.empty()){
int size = q.size();
for (int i = 0; i < size; i++){
int here = q.front();
q.pop();
Time[here] += cost[here];
ans = max(ans, Time[here]);
for (int j = 0; j < adj[here].size(); j++){
int next = adj[here][j];
Time[next] = max(Time[next], Time[here]);
if (--indgree[next] > 0) continue;
q.push(next);
}
}
}
return ans;
}
int main(){
int n; scanf("%d", &n);
adj = vector<vector<int> >(n + 1);
for (int i = 1; i <= n; i++){
int E; scanf("%d%d", &cost[i], &E);
for (int j = 0; j < E; j++){
int vertex; scanf("%d", &vertex);
adj[vertex].push_back(i);
indgree[i]++;
}
}
for (int i = 1; i <= n; i++){
if (indgree[i] == 0) q.push(i);
}
printf("%d", search());
}
댓글 없음:
댓글 쓰기