-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleetcode5354.cpp
45 lines (38 loc) · 1.31 KB
/
leetcode5354.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
class Solution {
public:
int numOfMinutes(int n, int headID, vector<int>& manager, vector<int>& informTime) {
vector<bool> visited(manager.size(),false);
vector<int> Cost(manager.size(),0);
for(int i = 0 ; i < manager.size() ; i++){
if(manager[i] != -1)
visited[manager[i]] = true;
}
queue<int> Q;
for(int i = 0 ; i < visited.size() ; i++){
if(visited[i] == false){
//cout << i << endl;
Q.push(i);
}
}
if(Q.empty()) return 0;
while(!Q.empty()){
int loop = Q.size();
set<int> Insert;
while(loop--){
int now = Q.front();
Q.pop();
if(now == headID)
break;
if(Cost[now] +informTime[manager[now]] > Cost[manager[now]]){
Cost[manager[now]] = Cost[now] +informTime[manager[now]];
//cout <<Cost[manager[now]] <<endl;
}
if(Insert.find(manager[now]) == Insert.end()){
Q.push(manager[now]);
Insert.insert(manager[now]);
}
}
}
return Cost[headID];
}
};