-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
/
Copy pathSolution.cpp
33 lines (33 loc) · 910 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
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> dp(n);
int ans = 0;
for (int i = 0; i < n; ++i) {
int v = indeg[i], t = time[i];
if (v == 0) {
q.push(i);
dp[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);
dp[j] = max(dp[j], dp[i] + time[j]);
ans = max(ans, dp[j]);
}
}
return ans;
}
};