网络延迟时间

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;

}

};

相关推荐
bob628562 小时前
leetcode刷题-2026-3-38
算法·leetcode
山川行2 小时前
Python快速闯关专栏的总结
java·开发语言·笔记·python·算法·visual studio code·visual studio
Irissgwe2 小时前
网络基础概念
linux·网络·网络基础概念
IdahoFalls2 小时前
QT-Windows Kits-版本问题:【“_mm_loadu_si64”: 找不到标识符】解决方案[NEW]
开发语言·c++·windows·qt·算法·visual studio
会编程的土豆2 小时前
【leetcode hot 100】 二叉树2
算法·leetcode·职场和发展
承渊政道2 小时前
【优选算法】(实战掌握分治思想的使用方法)
数据结构·c++·笔记·vscode·学习·算法·leetcode
我在人间贩卖青春2 小时前
Qt 网络编程
网络·qt
adam_life2 小时前
A*算法——# P1379 八数码难题
算法·优先队列·a星算法·最优启发式搜索·哈希标记·启发式函数·已走步数+预估距离
Yungoal2 小时前
C++基础项目结构
数据结构·c++·算法