-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy path743-network-delay-time.cpp
39 lines (37 loc) · 1.15 KB
/
743-network-delay-time.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
#include <unordered_map>
#include <queue>
class Solution {
public:
int networkDelayTime(vector<vector<int>>& times, int N, int K) {
unordered_map<int, vector<vector<int>>> graph;
for (auto& t : times) {
auto it = graph.find(t[0]);
if (it != graph.end()) {
(it->second).push_back({t[1], t[2]});
} else {
graph[t[0]] = {{t[1], t[2]}};
}
}
int numVisited = 0;
vector<int> delay(N + 1, -1);
delay[K] = 0;
queue<int> q;
q.emplace(K);
while (!q.empty()) {
int curr = q.front();
q.pop();
for (auto& n : graph[curr]) {
if (delay[n[0]] == -1 || delay[n[0]] > delay[curr] + n[1]) {
delay[n[0]] = delay[curr] + n[1];
q.emplace(n[0]);
}
}
}
int maxDelay = INT_MIN;
for (int i = 1; i <= N; i++) {
if (delay[i] == -1) return -1;
if (delay[i] > maxDelay) maxDelay = delay[i];
}
return maxDelay;
}
};