网络延迟时间

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;

}

};

相关推荐
white-persist34 分钟前
【vulhub shiro 漏洞复现】vulhub shiro CVE-2016-4437 Shiro反序列化漏洞复现详细分析解释
运维·服务器·网络·python·算法·安全·web安全
黄俊懿1 小时前
【架构师从入门到进阶】第五章:DNS&CDN&网关优化思路——第一节:DNS优化
网络·计算机网络·架构·系统架构·cdn·dns·架构设计
FL16238631291 小时前
基于C#winform部署软前景分割DAViD算法的onnx模型实现前景分割
开发语言·算法·c#
baizhigangqw2 小时前
启发式算法WebApp实验室:从搜索策略到群体智能的能力进阶
算法·启发式算法·web app
C雨后彩虹2 小时前
最多等和不相交连续子序列
java·数据结构·算法·华为·面试
Byron Loong2 小时前
【网络】Python 怎么做TCP通讯
网络·python·tcp/ip
裕工实验室2 小时前
功率模块为什么一定要用陶瓷PCB?从结构到选材一篇讲清(附DPC / DBC / AMB选型逻辑)
网络·硬件工程·pcb工艺·材料工程
SilentSamsara3 小时前
HTTP/1.1 到 HTTP/3:每代协议解决了什么问题
网络·网络协议·tcp/ip·http·https
cpp_25013 小时前
P2347 [NOIP 1996 提高组] 砝码称重
数据结构·c++·算法·题解·洛谷·noip·背包dp
Hugh-Yu-1301233 小时前
二元一次方程组求解器c++代码
开发语言·c++·算法