-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path0743.cpp
More file actions
26 lines (23 loc) · 765 Bytes
/
Copy path0743.cpp
File metadata and controls
26 lines (23 loc) · 765 Bytes
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
class Solution {
typedef pair<int, int> edge;
public:
int networkDelayTime(vector<vector<int>> ×, int n, int k) {
vector<vector<edge>> adj(n + 1, vector<edge>());
for (auto &p : times)
adj[p[0]].push_back({p[2], p[1]});
priority_queue<edge, vector<edge>, greater<edge>> st;
unordered_set<int> us;
int time = 0;
st.push({0, k});
while (!st.empty()) {
auto [t, root] = st.top();
st.pop();
if (us.count(root)) continue;
time = t;
us.insert(root);
for (auto &[time, dest] : adj[root])
if (!us.count(dest)) st.push({t + time, dest});
}
return us.size() == n ? time : -1;
}
};