-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path1005-acm_craft.cpp
More file actions
69 lines (57 loc) · 1.17 KB
/
Copy path1005-acm_craft.cpp
File metadata and controls
69 lines (57 loc) · 1.17 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include<bits/stdc++.h>
using namespace std;
int N,K,D;
int Delay[1001];
vector<int> way[1001];
int inDegree[1001];
int result[1001];
int topologySort(){
queue<int> q;
for(int i=1;i<=N;i++){
if(inDegree[i]==0){
q.push(i);
result[i]=Delay[i];
}
}
for(int i=1;i<=N;i++){
int f=q.front();
q.pop();
if(f==D){
return result[D];
}
for(int j=0;j<way[f].size();j++){
int y=way[f][j];
inDegree[y]--;
result[y]=max(result[y],result[f]+Delay[y]);
if(inDegree[y]==0){
q.push(y);
}
}
}
return 0;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int T;
int X,Y;
cin>>T;
while(T--){
cin>>N>>K;
for(int i=1;i<=N;i++){
cin>>Delay[i];
inDegree[i]=0;
result[i]=0;
way[i].clear();
}
for(int i=1;i<=K;i++){
cin>>X>>Y;
inDegree[Y]++;
way[X].push_back(Y);
}
cin>>D;
cout<<topologySort()<<'\n';
}
return 0;
}