网络延迟时间

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;

}

};

相关推荐
放羊郎2 小时前
基于ORB-SLAM2算法的优化工作
人工智能·算法·计算机视觉
mask哥2 小时前
力扣算法java实现汇总整理(上)
java·算法·leetcode
如果'\'真能转义说3 小时前
OOXML 文档格式剖析:哈希、ZIP结构与识别
xml·算法·c#·哈希算法
苍煜3 小时前
Docker容器网络详解+端口映射原理(系列第二篇:实战核心)
网络·docker·容器
梦梦代码精4 小时前
BuildingAI 上部署自定义工作流智能体:5 个实用技巧
大数据·人工智能·算法·开源软件
初願致夕霞4 小时前
基于系统调用的Linux网络编程——UDP与TCP
linux·网络·c++·tcp/ip·udp
Zephyr_05 小时前
Leedcode算法题
java·算法
流年如夢5 小时前
栈和列队(LeetCode)
数据结构·算法·leetcode·链表·职场和发展
数智化精益手记局6 小时前
什么是设备维护管理?设备维护管理包含哪些内容?
大数据·网络·人工智能·安全·信息可视化