-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathACM_Craft.cpp
More file actions
100 lines (83 loc) · 1.61 KB
/
ACM_Craft.cpp
File metadata and controls
100 lines (83 loc) · 1.61 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
struct Node
{
int num;
int endTime;
};
bool operator>(const Node a, const Node b)
{
return a.endTime > b.endTime;
}
int n, k;
int target;
vector<int> buildTime;
vector<vector<int>> edges;
vector<int> indegree;
vector<int> answer;
void input()
{
cin >> n >> k;
buildTime.assign(n+1, 0);
edges.assign(n+1, vector<int>());
indegree.assign(n+1, 0);
for (int i=1; i<n+1; ++i)
{
cin >> buildTime[i];
}
for (int i=0; i<k; ++i)
{
int from, to;
cin >> from >> to;
++indegree[to];
edges[from].push_back(to);
}
cin >> target;
}
void topologicalSort()
{
priority_queue<Node, vector<Node>, greater<Node> > pq;
for (int i=1; i<n+1; ++i)
{
if (indegree[i] == 0)
pq.push({ i, buildTime[i] });
}
while(!pq.empty())
{
int cur = pq.top().num;
int curTime = pq.top().endTime;
pq.pop();
if (cur == target)
{
answer.push_back(curTime);
break;
}
for (int next : edges[cur])
{
--indegree[next];
if (indegree[next] != 0)
continue;
int nextTime = curTime + buildTime[next];
pq.push({next, nextTime});
}
}
}
int main()
{
cin.tie(0);
ios_base::sync_with_stdio(false);
int testcase;
cin >> testcase;
while (testcase--)
{
input();
topologicalSort();
}
for (auto a : answer)
{
cout << a << "\n";
}
return 0;
}