-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path743+Network Delay Time_SPFA.cpp
More file actions
43 lines (33 loc) · 1.05 KB
/
Copy path743+Network Delay Time_SPFA.cpp
File metadata and controls
43 lines (33 loc) · 1.05 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
39
40
41
42
43
class Solution {
public:
int networkDelayTime(vector<vector<int>>& times, int n, int k) {
const int inf = INT_MAX / 2;
vector<vector<pair<int, int>>> g(n + 1);
for (auto &t : times) {
g[t[0]].emplace_back(t[1], t[2]);
}
// 记录最短路径
vector<int> dist(n + 1, inf);
dist[k] = 0;
queue<int> q;
q.emplace(k);
unordered_set<int> s;
s.emplace(k);
while (q.size()) {
int cur = q.front(); q.pop();
s.erase(cur);
for (auto &e : g[cur]) {
int x = e.first, d = dist[cur] + e.second;
if (dist[x] > d) {
dist[x] = d;
if (s.count(x) == 0) { // x不在集合中
q.emplace(x);
s.emplace(x);
}
}
}
}
int res = *max_element(++dist.begin(), dist.end());
return res == inf ? -1 : res;
}
};