惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

C
Cyber Attacks, Cyber Crime and Cyber Security
Cisco Talos Blog
Cisco Talos Blog
Scott Helme
Scott Helme
The Last Watchdog
The Last Watchdog
G
GRAHAM CLULEY
T
Tenable Blog
PCI Perspectives
PCI Perspectives
Simon Willison's Weblog
Simon Willison's Weblog
N
News and Events Feed by Topic
Know Your Adversary
Know Your Adversary
S
Schneier on Security
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
P
Privacy International News Feed
C
CERT Recently Published Vulnerability Notes
NISL@THU
NISL@THU
SecWiki News
SecWiki News
S
Securelist
D
Docker
阮一峰的网络日志
阮一峰的网络日志
人人都是产品经理
人人都是产品经理
T
Tailwind CSS Blog
T
Troy Hunt's Blog
The Register - Security
The Register - Security
K
Kaspersky official blog
Blog — PlanetScale
Blog — PlanetScale
云风的 BLOG
云风的 BLOG
Hacker News: Ask HN
Hacker News: Ask HN
S
Secure Thoughts
Stack Overflow Blog
Stack Overflow Blog
T
Threat Research - Cisco Blogs
博客园 - 司徒正美
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
F
Fortinet All Blogs
T
Threatpost
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
小众软件
小众软件
WordPress大学
WordPress大学
Security Archives - TechRepublic
Security Archives - TechRepublic
博客园 - 聂微东
Attack and Defense Labs
Attack and Defense Labs
B
Blog RSS Feed
Project Zero
Project Zero
Y
Y Combinator Blog
T
The Blog of Author Tim Ferriss
博客园 - 【当耐特】
V
V2EX
Help Net Security
Help Net Security
P
Proofpoint News Feed
A
Arctic Wolf

博客园 - 北叶青藤

2096. Step-By-Step Directions From a Binary Tree Node to Another Find path from root to a target node in Binary Tree 1188. Design Bounded Blocking Queue 1115. Print FooBar Alternately 1114. Print in Order 1242. Web Crawler Multithreaded Python Multi-threading bot ip Log Rate Limiter Same Word of HTML Labels Most Frequent Call Chain remove prefix in a words list 1102. Path With Maximum Minimum Value Property Booking Optimizer minimum number 755. Pour Water Keyword Tagging in Reviews with Overlapping Matches Retryer Function Implementation 1125. Smallest Sufficient Team Print the terrain Split stay Task scheduling problem 滑雪问题 845. Longest Mountain in Array 723. Candy Crush 1539. Kth Missing Positive Number 1650. Lowest Common Ancestor of a Binary Tree III 424. Longest Repeating Character Replacement 843. Guess the Word 551. Student Attendance Record I
When Dijkstra Algorithm Should be Use?
北叶青藤 · 2026-07-21 · via 博客园 - 北叶青藤

A good way to think about Dijkstra is:

Dijkstra works when the first time you remove a node from the priority queue, you already know its optimal value.

This is called the greedy property.

If a node's value can still improve later by taking a different path, Dijkstra is not applicable.


Cases where Dijkstra works

1. Shortest Path (Classic)

Edges represent distances.

Suppose we're finding the shortest path from A.

Initially 

We pop B first because 2 < 5.

Could there later be another path to B shorter than 2?

No.

Any other path must go through C, whose distance is already ≥5.

So

Impossible to improve.

This is exactly why Dijkstra works.

Requirement:

  • edge weights ≥ 0

2. Network Latency

Total latency

Again,

  • weights are nonnegative
  • costs only increase

Perfect for Dijkstra.


3. GPS Navigation

Road lengths

Distance always accumulates positively.

Works.


4. Cheapest Flight (without discounts)

Edge

Cost

Again additive positive cost.

Works.


5. Maximum Bottleneck Path (modified Dijkstra)

Suppose bandwidths

Path capacity is

So

We maximize the minimum.

A modified Dijkstra works because

The capacity never increases after extending a path.

The greedy property still holds.


Cases where Dijkstra does NOT work

1. Negative Edges

Initially

Dijkstra pops

But later

Much better.

Too late.

Greedy fails.

Bellman-Ford is needed.


2. Currency Exchange (your problem)

Products 

vs

Notice

looked optimal initially

but later became

Dijkstra finalized B too early.


3. Longest Path

Suppose 

Longest path

vs

Again

looked finished

but wasn't.


4. Maximum Product

Exactly your interview problem.

Products may increase dramatically later.

Greedy property breaks.


5. Paths with Rewards

Imagine

Objective

Reaching a node cheaply isn't necessarily best if another path collects much more reward.

No greedy property.


A useful rule of thumb

Suppose your path value is

Ask:

Can extending a worse path ever make it better than a currently better path?

If the answer is No, Dijkstra usually works.

If Yes, Dijkstra usually fails.


Works

Sum

with

Cannot decrease.

Works.


Minimum

Cannot increase.

Works.


Maximum

Cannot decrease in the relevant direction.

Works.


Doesn't work

Product

because

A worse partial product

can become

while the better one

becomes

Ordering changes.


Sum with negative edges

A worse partial sum can become better.

Ordering changes.


Interview heuristic

When solving a graph problem, ask these questions:

  1. Is the objective additive?
    • distance += edge
    • cost += edge
    • time += edge
    • → Think Dijkstra.
  2. Are all edge "increments" non-negative?
    • If not, Dijkstra is unsafe.
  3. Can the ranking of two partial paths flip after extending them?

For example, suppose two paths reach different nodes with current values:

If after one more edge you can get:

then the ordering has flipped. Once this can happen, the greedy assumption behind Dijkstra no longer holds, and you should be suspicious of using it.

This "can the ordering flip?" test is one of the quickest ways to judge whether Dijkstra is appropriate in an interview.