-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1005.cpp
68 lines (62 loc) · 1.26 KB
/
1005.cpp
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
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
int t, n, k, w;
int constructT[1001] = {0, };
int conditions[1001] = {0, };
int answer[1001] = {0, };
vector<int>nextBuild[1001];
void input();
int solving();
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> t;
while(t--){
input();
cout << solving() << '\n';
}
}
void input(){
for(int i = 0; i <= n; i++){
nextBuild[i].clear();
conditions[i] = 0;
answer[i] = 0;
}
cin >> n >> k;
for(int i = 1; i <= n; i++){
cin >> constructT[i];
}
int tmp1, tmp2;
for(int i = 0; i < k; i++){
cin >> tmp1 >> tmp2;
nextBuild[tmp1].push_back(tmp2);
conditions[tmp2]++;
}
cin >> w;
}
int solving(){
queue<int>q;
for(int i = 1; i <= n; i++){
if(conditions[i] == 0){
q.push(i);
answer[i] = constructT[i];
}
}
while(!q.empty()){
int nowBuilding = q.front();
q.pop();
if(nowBuilding == w)return answer[nowBuilding];
for(int i = 0; i < nextBuild[nowBuilding].size(); i++){
int nextBuilding = nextBuild[nowBuilding][i];
conditions[nextBuilding]--;
answer[nextBuilding] = max(answer[nextBuilding], answer[nowBuilding]+constructT[nextBuilding]);
if(conditions[nextBuilding] == 0){
q.push(nextBuilding);
}
}
}
return answer[w];
}