-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
/
Copy pathSolution.cpp
35 lines (35 loc) · 944 Bytes
/
Solution.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
class Solution {
public:
int minimumTime(int n, vector<vector<int>>& relations, vector<int>& time) {
vector<vector<int>> g(n);
vector<int> indeg(n);
for (auto& e : relations) {
int a = e[0] - 1, b = e[1] - 1;
g[a].push_back(b);
++indeg[b];
}
queue<int> q;
vector<int> f(n);
int ans = 0;
for (int i = 0; i < n; ++i) {
int v = indeg[i], t = time[i];
if (v == 0) {
q.push(i);
f[i] = t;
ans = max(ans, t);
}
}
while (!q.empty()) {
int i = q.front();
q.pop();
for (int j : g[i]) {
if (--indeg[j] == 0) {
q.push(j);
}
f[j] = max(f[j], f[i] + time[j]);
ans = max(ans, f[j]);
}
}
return ans;
}
};