当前位置:网站首页>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;
}
};
边栏推荐
- Summary of various installation methods of Lab View
- np.concatenate中axis的理解
- [Stanford Jiwang cs144 project] lab1: streamreassembler
- Stack stack LIFO
- Method of cleaning C disk
- [projet cs144 de Stanford Computing Network] lab1: Stream reassembler
- Wildcard usage of go standard library FMT
- Use koa to mock data and set cross domain issues
- [learn FPGA programming from scratch -22]: Advanced chapter - Architecture - Design and modeling of FPGA internal hardware circuit
- Leetcode question brushing 04 string
猜你喜欢

Rasa对话机器人之HelpDesk (三)

leetcode 142. Circular linked list II

MySQL related summary

关于tkinter.Canvas 不显示图片的问题

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

Temporary objects and compilation optimization

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

Startup, connection and stop of MySQL service

Rasa dialogue robot helpdesk (III)

Project training (XVII) -- personal work summary
随机推荐
Record the VMware installation process of VMware Tools and some problems encountered
Leetcode 01 array
ES6 deconstruction assignment
4K sea bottom and water surface fabrication method and ocean bump displacement texture Download
Downloading wiki corpus and aligning with multilingual wikis
MySQL - use field alias after where
Simple operation of MySQL database
Answer to matrix theory of Nanjing University of Aeronautics and Astronautics
[wsl2] restrict wsl2 accessible hardware resources (cpu/ memory)
[learn FPGA programming from scratch -21]: Advanced - Architecture - VerilogHDL coding specification
How to solve the problem of database?
A summary of global variables and typedef
Memory learning book reference
Unity jsonutility failed to serialize list
Three paradigms of database
Calculate sentence confusion ppl using Bert and gpt-2
Web Application & applet application deployment
Wangdao machine test - Chapter 6 - maximum common divisor GCD and minimum common multiple LCM
MySQL download and installation
[andoid][step pit]cts 11_ Testbootclasspathandsystemserverclasspath at the beginning of R3_ Analysis of nonduplicateclasses fail