-
Notifications
You must be signed in to change notification settings - Fork 0
/
acm_craft.cpp
58 lines (52 loc) · 1.23 KB
/
acm_craft.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
// https://www.acmicpc.net/problem/1005
#include <bits/stdc++.h>
using namespace std;
int main(void)
{
ios::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin>>t;
while (t--) {
int n, k;
cin>>n>>k;
vector<int> d(n+1);
vector<int> dp(n+1);
for (int i = 1; i<=n; ++i) {
cin>>d[i];
dp[i]=d[i];
}
vector<vector<int>> adj_list(n+1);
vector<int> indegree(n+1);
for (int i = 1; i<=k; ++i) {
int u, v;
cin>>u>>v;
adj_list[u].push_back(v);
++indegree[v];
}
int w;
cin>>w;
queue<int> q;
for (int i = 1; i<=n; ++i) {
if (!indegree[i]) {
q.push(i);
}
}
while (!q.empty()) {
int cur = q.front();
q.pop();
if (cur==w) {
break;
}
for (int next : adj_list[cur]) {
dp[next]=max(dp[next],dp[cur]+d[next]);
--indegree[next];
if (!indegree[next]) {
q.push(next);
}
}
}
cout << dp[w] << '\n';
}
return 0;
}