-
Notifications
You must be signed in to change notification settings - Fork 0
/
ACMCraft.cpp
59 lines (54 loc) · 935 Bytes
/
ACMCraft.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
59
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
int main(void)
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t, n, k, d, x, y, w;
cin >> t;
while (t--)
{
cin >> n >> k;
vector<int> build(n + 1), cnt(n + 1), mintime(n + 1);
vector<vector<int>> seq(n + 1);
queue<int> q;
for (int i = 1; i <= n; i++)
{
cin >> d;
build[i] = d;
}
while (k--)
{
cin >> x >> y;
seq[x].push_back(y);
cnt[y]++;
}
for (int i = 1; i <= n; i++)
{
if (!cnt[i])
{
mintime[i] = build[i];
q.push(i);
}
}
cin >> w;
while (!q.empty())
{
int pos = q.front();
q.pop();
for (int i = 0; i < seq[pos].size(); i++)
{
int npos = seq[pos][i];
mintime[npos] = max(mintime[npos], mintime[pos] + build[npos]);
if (--cnt[npos] == 0)
q.push(npos);
}
}
cout << mintime[w] << '\n';
}
return 0;
}