-
Notifications
You must be signed in to change notification settings - Fork 0
/
1005.cpp
56 lines (47 loc) · 890 Bytes
/
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
#include <cstdio>
#include <vector>
#include <queue>
using namespace std;
int time[1001], in[1001];
int t, n, k, a, b, w;
int main() {
scanf("%d", &t);
while (t--) {
scanf("%d %d", &n, &k);
for (int i = 1; i <= n; ++i) {
scanf("%d", &time[i]);
in[i] = 0;
}
vector<vector<int>> vec(n + 1);
for (int i = 0; i < k; ++i) {
scanf("%d %d", &a, &b);
vec[a].push_back(b);
in[b]++;
}
vector<int> ans(n + 1);
queue<int> q;
for (int i = 1; i <= n; ++i) {
if (!in[i]) {
q.push(i);
ans[i] = time[i];
}
}
scanf("%d", &w);
while (!q.empty()) {
int m = 0;
int cur = q.front();
q.pop();
if (cur == w)
break;
for (auto next : vec[cur]) {
in[next]--;
if (ans[next] < ans[cur] + time[next])
ans[next] = ans[cur] + time[next];
if (!in[next])
q.push(next);
}
}
printf("%d\n", ans[w]);
}
return 0;
}