-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1005.cpp
40 lines (40 loc) · 836 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
#include <bits/stdc++.h>
using namespace std;
int main() {
int tc; cin >> tc;
while(tc--) {
int n, k; cin >> n >> k;
vector<int> graph[n + 1];
int time[n + 1];
bool isUsed[n + 1] = {0,};
int indegree[n + 1] = {0,};
int dp[n + 1] = {0,};
queue<int> q;
for(int i = 1; i <= n; i++) cin >> time[i];
while(k--) {
int a, b; cin >> a >> b;
graph[a].push_back(b);
indegree[b]++;
}
for(int i = 1; i <= n; i++) {
if(indegree[i] == 0) q.push(i);
}
while(!q.empty()) {
int cur = q.front(); q.pop();
dp[cur] += time[cur];
for(auto nxt : graph[cur]) {
if(isUsed[nxt]) {
dp[nxt] = max(dp[nxt], dp[cur]);
}
else {
dp[nxt] = dp[cur];
isUsed[nxt] = 1;
}
indegree[nxt]-=1;
if(indegree[nxt] == 0) q.push(nxt);
}
}
int w; cin >> w;
cout << dp[w] << "\n";
}
}