-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboj1005.cpp
56 lines (46 loc) · 1022 Bytes
/
boj1005.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 <iostream>
#include <algorithm>
using namespace std;
inline void Quick_IO() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
}
int dp[1001];
int times[1001];
vector<int> parents[1001];
int calc(int target) {
int &ret = dp[target];
if (ret != -1) {
return ret;
}
int result = times[target];
int maxValue = 0;
for (auto parent:parents[target]) {
maxValue = max(maxValue, calc(parent));
}
return ret = result + maxValue;
}
int main() {
Quick_IO();
int T;
cin >> T;
while (T--) {
for (auto &parent : parents) {
parent.clear();
}
int N, K, W;
cin >> N >> K;
fill(dp + 1, dp + N + 1, -1);
for (int i = 1; i <= N; ++i) {
cin >> times[i];
}
for (int i = 0, x, y; i < K; ++i) {
cin >> x >> y;
parents[y].push_back(x);
}
cin >> W;
cout << calc(W) << '\n';
}
return 0;
}