-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path743.cpp
More file actions
38 lines (33 loc) · 1.2 KB
/
743.cpp
File metadata and controls
38 lines (33 loc) · 1.2 KB
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
class Solution {
public:
typedef pair<int, int> Node; // distance, id
int networkDelayTime(vector<vector<int>>& times, int n, int k) {
vector<vector<pair<int, int>>> graph(n + 1); // node-> nodes (id, distance)
vector<int> distances(n + 1, INT_MAX);
vector<bool> visited(n + 1, false);
for (auto time : times) {
graph[time[0]].push_back(make_pair(time[1], time[2]));
}
priority_queue<Node, vector<Node>, greater<Node>> pq;
pq.push(make_pair(0, k));
distances[k] = 0;
while (!pq.empty()) {
auto [dist, node] = pq.top();
pq.pop();
visited[node] = true;
for (auto [neighbor, w] : graph[node]) {
int _w = w + dist;
if (_w < distances[neighbor]) {
distances[neighbor] = _w;
if (!visited[neighbor]) pq.push(make_pair(_w, neighbor));
}
}
}
int res = -1;
for (int i = 1; i <= n; i++) {
if (distances[i] == INT_MAX) return -1;
else res = max(res, distances[i]);
}
return res;
}
};