-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1005.cpp
More file actions
63 lines (61 loc) · 1.26 KB
/
1005.cpp
File metadata and controls
63 lines (61 loc) · 1.26 KB
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
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int max(const int &a, const int &b)
{
if (a >= b)
return a;
else
return b;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int t;
cin >> t;
while (t--)
{
int n, k;
queue<int> q;
cin >> n >> k;
vector<int> adj[n + 1];
vector<int> idx(n + 1);
vector<int> T(n + 1);
vector<int> ans(n + 1, 0);
for (int i = 1; i <= n; i++)
{
int a;
cin >> a;
T[i] = ans[i] = a;
}
for (int i = 0; i < k; i++)
{
int u, v;
cin >> u >> v;
adj[u].push_back(v);
idx[v]++;
}
for (int i = 1; i <= n; i++)
if (idx[i] == 0)
q.push(i);
while (!q.empty())
{
int x = q.front();
q.pop();
for (int y : adj[x])
{
if (ans[y] < ans[x] + T[y])
ans[y] = ans[x] + T[y];
idx[y]--;
if (idx[y] == 0)
q.push(y);
}
}
int w;
cin >> w;
cout << ans[w] << "\n";
}
return 0;
}