当前位置:网站首页>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;
}
};
边栏推荐
- The second round of mesa
- leetcode. 349. intersection of two arrays
- Wikipedia API User Guide
- This of phaser3 add. sprite
- Copy (copy) constructors and assignment overloaded operators=
- RSA encryption colloquial explanation
- Leetcode find duplicates
- Simple operation of MySQL database
- pycharm add configutions
- MySQL related summary
猜你喜欢
项目实训(十七)---个人工作总结
Rasa dialogue robot helpdesk (III)
RSA encryption colloquial explanation
在国企做软件测试工程师是一种什么样的体验:每天过的像打仗一样
The storage structure of a tree can adopt the parent representation, that is, the parent pointer array representation. Try to give the corresponding class definition. Each tree node contains two membe
Database query user mailbox
How to print infinite symbol in WPS
Lecture on Compilation Principles
5G工业网关在煤矿行业的应用优势
C language implementation of the classic eight queens problem
随机推荐
Alexnet实现Caltech101数据集图像分类(pytorch实现)
【斯坦福计网CS144项目】Lab1: StreamReassembler
Golang inline mechanism & go development test
Polymorphism and virtual function
ES6 deconstruction assignment
[projet cs144 de Stanford Computing Network] lab1: Stream reassembler
Several categories of software testing are clear at a glance
Sliding window summary of TCP connections
Stack and queue practice (C language): Demon King's language
pycharm add configutions
Mysql database listening -canal
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
Leetcode question brushing 06 bit operation
Introduction to convolutional neural network
Tkinter library installation
Crypto JS reports uglifyjs error
Getting started with phaser 3
Work and life
np.concatenate中axis的理解
Go JWT learning summary