forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0743-network-delay-time.cpp
63 lines (52 loc) · 2.07 KB
/
0743-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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
Signal sent from node k to network of n nodes, return time for all nodes to receive it
Ex. times = [[2,1,1],[2,3,1],[3,4,1]], n = 4, k = 2 -> 2
u,v,w -> u = source node, v = target node, w = signal travel time
Shortest path from node k to every other node, Dijkstra's to find fastest path
Time: O(V + E log V)
Space: O(V + E)
*/
class Solution {
public:
int networkDelayTime(vector<vector<int>>& times, int n, int k) {
vector<pair<int, int>> adj[n + 1];
for (int i = 0; i < times.size(); i++) {
int source = times[i][0];
int dest = times[i][1];
int time = times[i][2];
adj[source].push_back({time, dest});
}
vector<int> signalReceiveTime(n + 1, INT_MAX);
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
pq.push({0, k});
// time for start node is 0
signalReceiveTime[k] = 0;
while (!pq.empty()) {
int currNodeTime = pq.top().first;
int currNode = pq.top().second;
pq.pop();
if (currNodeTime > signalReceiveTime[currNode]) {
continue;
}
// send signal to adjacent nodes
for (int i = 0; i < adj[currNode].size(); i++) {
pair<int, int> edge = adj[currNode][i];
int time = edge.first;
int neighborNode = edge.second;
// fastest signal time for neighborNode so far
if (signalReceiveTime[neighborNode] > currNodeTime + time) {
signalReceiveTime[neighborNode] = currNodeTime + time;
pq.push({signalReceiveTime[neighborNode], neighborNode});
}
}
}
int result = INT_MIN;
for (int i = 1; i <= n; i++) {
result = max(result, signalReceiveTime[i]);
}
if (result == INT_MAX) {
return -1;
}
return result;
}
};