-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1005.cpp
More file actions
57 lines (51 loc) · 924 Bytes
/
1005.cpp
File metadata and controls
57 lines (51 loc) · 924 Bytes
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
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t, n, k, w, a, b;
int buildt[1005], line[1005], dp[1005];
vector<int> v[1005];
queue<int> q;
cin >> t;
while (t--) {
cin >> n >> k;
for (int i = 1; i <= n; i++) {
cin >> buildt[i];
line[i] = 0;
dp[i] = 0;
}
for (int i = 0; i < k; i++) {
cin >> a >> b;
line[b]++;
v[a].push_back(b);
}
cin >> w;
for (int i = 1; i <= n; i++) {
if (line[i] == 0) {
dp[i] = buildt[i];
q.push(i);
}
}
for (int i = 0; i < n; i++) {
a = q.front();
q.pop();
for (int j = 0; j < v[a].size(); j++) {
b = v[a].at(j);
dp[b] = max(dp[b], dp[a] + buildt[b]);
if (--line[b] == 0) {
q.push(b);
}
}
}
cout << dp[w] << "\n";
for (int i = 1; i <= n; i++) {
v[i].clear();
}
while (!q.empty()) {
q.pop();
}
}
return 0;
}