SOL)
BFS
마찬가지로 indgree를 이용하는 문제
[-] Collapse
#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;
vector<vector<int> > adj;
queue<int> q;
int indgree[1001], construct[1001], Time[1001];
int END;
int search(){
while (!q.empty()){
int size = q.size();
for (int i = 0; i < size; i++){
int here = q.front();
q.pop();
Time[here] += construct[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 Time[END];
}
int main(){
int t; scanf("%d", &t);
while (t--){
memset(Time, 0, sizeof(Time));
memset(indgree, 0, sizeof(indgree));
memset(construct, 0, sizeof(construct));
int V, E; scanf("%d%d", &V, &E);
adj = vector<vector<int> >(V+1);
for (int i = 1; i <= V; i++)
scanf("%d", &construct[i]);
for (int i = 0; i < E; i++){
int u, v; scanf("%d%d", &u, &v);
adj[u].push_back(v);
indgree[v]++;
}
for (int i = 1; i <= V; i++){
if (indgree[i] == 0)q.push(i);
}
scanf("%d", &END);
printf("%d\n", search());
}
}
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;
vector<vector<int> > adj;
queue<int> q;
int indgree[1001], construct[1001], Time[1001];
int END;
int search(){
while (!q.empty()){
int size = q.size();
for (int i = 0; i < size; i++){
int here = q.front();
q.pop();
Time[here] += construct[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 Time[END];
}
int main(){
int t; scanf("%d", &t);
while (t--){
memset(Time, 0, sizeof(Time));
memset(indgree, 0, sizeof(indgree));
memset(construct, 0, sizeof(construct));
int V, E; scanf("%d%d", &V, &E);
adj = vector<vector<int> >(V+1);
for (int i = 1; i <= V; i++)
scanf("%d", &construct[i]);
for (int i = 0; i < E; i++){
int u, v; scanf("%d%d", &u, &v);
adj[u].push_back(v);
indgree[v]++;
}
for (int i = 1; i <= V; i++){
if (indgree[i] == 0)q.push(i);
}
scanf("%d", &END);
printf("%d\n", search());
}
}
댓글 없음:
댓글 쓰기