-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1005_acm.cpp
More file actions
66 lines (48 loc) · 1.21 KB
/
1005_acm.cpp
File metadata and controls
66 lines (48 loc) · 1.21 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
#include<bits/stdc++.h>
#include<memory.h>
#define IOS ios_base::sync_with_stdio(0)
#define CI cin.tie(0)
using namespace std;
int cost[1001] = {0};
int dist[1001] = {0};
int ind[1001] = {0};
int main(){
IOS;
CI;
int T;
cin>>T;
while(T--){
int n, m;
int fin;
queue<int> q;
vector<int> v[1001];
memset(ind,0,sizeof(ind));
memset(cost,0,sizeof(cost));
memset(dist,0,sizeof(dist));
cin>>n>>m;
for(int i=1;i<=n;i++){
cin>>cost[i];
}
for(int i=1;i<=m;i++){
int from, to;
cin>>from>>to;
v[from].push_back(to);
ind[to]++;
}
cin>>fin;
for(int i=1;i<=n;i++){
if(ind[i]==0) q.push(i);
}
while(ind[fin]>0){
int from = q.front();
q.pop();
for(int next:v[from]){
dist[next] = max(dist[next],dist[from]+cost[from]);
ind[next]--;
if(ind[next]==0) q.push(next);
}
}
cout<<dist[fin]+cost[fin]<<'\n';
}
return 0;
}