-
Notifications
You must be signed in to change notification settings - Fork 3
/
2050.cpp
36 lines (34 loc) · 1.07 KB
/
2050.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
class Solution {
public:
int minimumTime(int n, vector<vector<int>>& relations, vector<int>& time) {
vector<int> inDegrees(n, 0);
vector<vector<int>> graph(n);
for (auto& relation : relations) {
inDegrees[relation[1] - 1]++;
graph[relation[0] - 1].push_back(relation[1] - 1);
}
vector<int> preTime(n, -1);
int maxTime = 0;
queue<int> q;
for (int i = 0; i < n; ++i) {
if (inDegrees[i] == 0) {
q.push(i);
preTime[i] = time[i];
maxTime = max(maxTime, time[i]);
}
}
while (!q.empty()) {
int node = q.front();
q.pop();
for (auto& child : graph[node]) {
inDegrees[child]--;
preTime[child] = max(preTime[child], preTime[node] + time[child]);
maxTime = max(maxTime, preTime[child]);
if (inDegrees[child] == 0) {
q.push(child);
}
}
}
return maxTime;
}
};