class Solution {
public:
int networkDelayTime(vector<vector<int>>& times, int n, int k) {
vector<vector<pair<int, int>>> graph(n);
for (auto& t : times) {
int u = t[0] - 1, v = t[1] - 1;
graph[u].emplace_back(v, t[2]);
}
priority_queue<pair<int, int>, vector<pair<int, int>>,
greater<pair<int, int>>>
pq;
pq.push({0, k-1});
vector<int> dist(n, INT_MAX - 10000);
vector<bool> st(n, false);
dist[k-1]=0;
while (pq.size()) {
auto e = pq.top();
pq.pop();
int w = e.first, u = e.second;
if (st[u])
continue;
st[u] = true;
for (auto& o : graph[u]) {
int v = o.first, ww = o.second;
if (!st[v] && dist[v] > dist[u] + ww) {
dist[v] = dist[u] + ww;
pq.push({dist[v], v});
}
}
}
int res = 0;
for (int i = 0; i < n; ++i) {
if (dist[i] == INT_MAX - 10000)
return -1;
res = max(res, dist[i]);
}
return res;
}
};