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

推荐订阅源

Security Latest
Security Latest
Recent Commits to openclaw:main
Recent Commits to openclaw:main
T
Threatpost
NISL@THU
NISL@THU
A
Arctic Wolf
C
CXSECURITY Database RSS Feed - CXSecurity.com
T
Tenable Blog
O
OpenAI News
Know Your Adversary
Know Your Adversary
Google Online Security Blog
Google Online Security Blog
Cloudbric
Cloudbric
PCI Perspectives
PCI Perspectives
爱范儿
爱范儿
GbyAI
GbyAI
U
Unit 42
IT之家
IT之家
Cyberwarzone
Cyberwarzone
T
The Exploit Database - CXSecurity.com
罗磊的独立博客
Last Week in AI
Last Week in AI
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Hacker News - Newest:
Hacker News - Newest: "LLM"
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
AWS News Blog
AWS News Blog
Schneier on Security
Schneier on Security
L
LINUX DO - 最新话题
Latest news
Latest news
Hacker News: Ask HN
Hacker News: Ask HN
W
WeLiveSecurity
TaoSecurity Blog
TaoSecurity Blog
Attack and Defense Labs
Attack and Defense Labs
Scott Helme
Scott Helme
Security Archives - TechRepublic
Security Archives - TechRepublic
S
Securelist
Help Net Security
Help Net Security
C
Cybersecurity and Infrastructure Security Agency CISA
V
V2EX
S
Security @ Cisco Blogs
月光博客
月光博客
P
Proofpoint News Feed
B
Blog RSS Feed
The GitHub Blog
The GitHub Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Hugging Face - Blog
Hugging Face - Blog
L
LangChain Blog
博客园 - 叶小钗
C
Check Point Blog
腾讯CDC
The Cloudflare Blog
Simon Willison's Weblog
Simon Willison's Weblog

博客园 - 北叶青藤

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.