当前位置:网站首页>leetcode743. 网络延迟时间(中等, dijkstra)
leetcode743. 网络延迟时间(中等, dijkstra)
2022-06-13 01:29:00 【重you小垃】



思路:单源最短路径->dijkstra 模板
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); //目标,距离
for (auto& a : times) {
bian[a[0]].push_back({
a[1], a[2]});
}
priority_queue<PII, vector<PII>, greater<PII>> pq; //距离->最近目的地
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;
}
};
边栏推荐
- 工作与生活
- On February 26, 2022, the latest news of national oil price adjustment today
- leetcode. 151. flip the words in the string
- ES6 deconstruction assignment
- Golang inline mechanism & go development test
- Set and array conversion, list, array
- Reinstall opencv and step on the pit.
- Memory learning book reference
- [learn FPGA programming from scratch -22]: Advanced chapter - Architecture - Design and modeling of FPGA internal hardware circuit
- Mysql database listening -canal
猜你喜欢

【斯坦福計網CS144項目】Lab1: StreamReassembler

Install pycharm process

Create a simple game interface using pyGame

MySQL performance optimization

Mathematical knowledge arrangement: extremum & maximum, stagnation point, Lagrange multiplier

September 3, 2021 visual notes

Leetcode 05 tree

Stmarl: a spatio temporal multi agentreinforcement learning approach for cooperative traffic

HashSet underlying source code

Introduction to convolutional neural network
随机推荐
Thread code learning notes
MySQL index
H5 open the app. If the app is not downloaded, jump to the download page. If the app has been downloaded, wake up the app
Docker install MySQL
【斯坦福计网CS144项目】Lab1: StreamReassembler
Leetcode-11- container with the most water (medium)
Leetcode find duplicates
leetcode 206. Reverse linked list
C language implementation of the classic eight queens problem
Mysql database listening -canal
Tangent and tangent plane
Five classic articles worth reading
Stack and queue practice (C language): Demon King's language
Leetcode-78- subset (medium)
DFS and BFS notes (II): depth first search (implemented in C language)
np. Understanding of axis in concatenate
Rasa dialogue robot helpdesk (III)
Traversal of binary tree - first order traversal, middle order traversal, and second order traversal
How to print infinite symbol in WPS
Leetcode-15- sum of three numbers (medium)