当前位置:网站首页>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;
}
};
边栏推荐
- leetcode 142. Circular linked list II
- Stack stack LIFO
- [learn FPGA programming from scratch -21]: Advanced - Architecture - VerilogHDL coding specification
- Understanding of the detach() function of pytorch
- Leetcode-17- letter combination of phone number (medium)
- leetode. 242. valid Letter heteronyms
- Auto commit attribute of MySQL
- Add default right-click menu
- 5G工业网关在煤矿行业的应用优势
- pycharm add configutions
猜你喜欢

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

5G工业网关在煤矿行业的应用优势

Deadlock problem summary

How to solve the problems when using TV focusable to package APK in uni app

Alexnet implements image classification of caltech101 dataset (pytorch Implementation)

Several categories of software testing are clear at a glance
![[leetcode] valid phone number Bash](/img/f8/cecb74f9d8f7c589e62e3a9a874f82.jpg)
[leetcode] valid phone number Bash

Alexnet实现Caltech101数据集图像分类(pytorch实现)

4K sea bottom and water surface fabrication method and ocean bump displacement texture Download

How to print infinite symbol in WPS
随机推荐
Golang learning essay
Auto commit attribute of MySQL
軟件測試的幾種分類,一看就明了
V-inline-date, similar to Ctrip, flying pig, time selection with price
#pragma comment(lib,“urlmon.lib“)
Leetcode-9-palindromes (simple)
ng-tv-focusable
A problem discovery and attempted solution to the strange stop of server script
Leetcode question brushing 04 string
This of phaser3 add. sprite
How many times does the constructor execute?
Several categories of software testing are clear at a glance
Camera model_
Docker install MySQL
5G工业网关在煤矿行业的应用优势
Downloading wiki corpus and aligning with multilingual wikis
Loss calculation in pytorch
np.concatenate中axis的理解
Transaction characteristics and isolation levels
Go JWT learning summary