-
Notifications
You must be signed in to change notification settings - Fork 1
/
boj.1005.cc
85 lines (73 loc) · 1.54 KB
/
boj.1005.cc
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> pll;
typedef pair<ull, ull> pull;
typedef const ll cll;
typedef queue<ll> qll;
typedef queue<pll> qpll;
typedef priority_queue<ll> pqll;
typedef priority_queue<pll> pqpll;
typedef vector<ll> vll;
cll N_MAX = 1e3, K_MAX = 1e5;
ll n, k, w, D[N_MAX + 1] = {}, dp[N_MAX + 1] = {}, degree[N_MAX + 1] = {};
ll solve(void)
{
cin >> n >> k;
for (ll i = 1; i <= n; ++i)
{
cin >> D[i];
}
memset(dp, -1, sizeof(ll) * (n + 1));
memset(degree, 0, sizeof(ll) * (n + 1));
vector<vll> childs(n + 1), parents(n + 1);
for (ll x, y, i = 0; i < k; ++i)
{
cin >> x >> y;
++degree[y];
childs[x].emplace_back(y);
parents[y].emplace_back(x);
}
cin >> w;
ll result = 0;
qll q;
for (ll i = 1; i <= n; ++i)
{
if (!degree[i])
{
q.push(i);
dp[i] = D[i];
}
}
while (!q.empty())
{
ll tgt = q.front();
q.pop();
for (auto &c : childs[tgt])
{
if (!--degree[c])
{
for (auto &p : parents[c])
{
dp[c] = max(dp[c], dp[p] + D[c]);
}
q.push(c);
}
}
}
return dp[w];
}
int main(void)
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
ll t;
cin >> t;
while (t--)
{
cout << solve() << "\n";
}
return 0;
}