当前位置:网站首页>leetcode743. Network latency (medium, Dijkstra)
leetcode743. Network latency (medium, Dijkstra)
2022-06-13 01:35:00 【Heavy garbage】



Ideas : Single source shortest path ->dijkstra Templates
class Solution {
public:
using PII = pair<int, int>;
int networkDelayTime(vector<vector<int>>& times, int n, int k) {
vector<int> vis(n + 1), dis(n + 1, INT_MAX);
vector<vector<PII>> bian(n + 1); // The goal is , distance
for (auto& a : times) {
bian[a[0]].push_back({
a[1], a[2]});
}
priority_queue<PII, vector<PII>, greater<PII>> pq; // distance -> Nearest destination
pq.push({
0, k});
int cnt = 0;
dis[k] = 0;
while (!pq.empty()) {
auto [_dis, pos] = pq.top();
pq.pop();
if (vis[pos]) continue;
vis[pos] = true;
cnt++;
for (auto& tmp : bian[pos]) {
if (_dis + tmp.second < dis[tmp.first]) {
dis[tmp.first] = _dis + tmp.second;
pq.push({
dis[tmp.first], tmp.first});
}
}
}
int ans = 0;
for (int i = 1; i <= n; ++i) {
ans = max(ans, dis[i]);
}
return (cnt == n) ? ans : -1;
}
};
边栏推荐
猜你喜欢

Leetcode question brushing 02 linked list operation

Camera model_

leetcode743. 网络延迟时间(中等, dijkstra)

DFS and BFS notes (II): depth first search (implemented in C language)

如何通过受众群体定位解决实际问题?
![[leetcode] valid phone number Bash](/img/f8/cecb74f9d8f7c589e62e3a9a874f82.jpg)
[leetcode] valid phone number Bash

Work and life

Leetcode question brushing 04 string

Use koa to mock data and set cross domain issues

【MathType】利用MathType输出LaTex样式的公式
随机推荐
Alexnet implements image classification of caltech101 dataset (pytorch Implementation)
Understanding of the detach() function of pytorch
Thread code learning notes
关于#数据库#的问题,如何解决?
Network communication tcp/ip
MySQL ---- where后使用字段别名
Detailed explanation of Joseph problem
[MathType] use MathType to output latex style formula
D template instance does not match declaration
C language implementation of the classic eight queens problem
Exercise 5.14 input n strings, arrange them in alphabetical order and output them.
About the proposed signature file migration to industry standard format pkcs12
How to solve the problem of database?
leetcode. 541. reverse string II
csdn涨薪技术之Jmeter接口测试数据库断言的实现与设计
QT color extraction
Polymorphism and virtual function
np.concatenate中axis的理解
项目实训(十七)---个人工作总结
How does Apple add QQ email?