PrevNext
Somewhat Frequent
 0/15

Shortest Paths with Non-Negative Edge Weights

Authors: Benjamin Qi, Andi Qu

Introduces Bellman-Ford, Floyd-Warshall, Dijkstra.

Nearly all Gold shortest path problems involve Dijkstra. However, it's a good idea to learn Bellman-Ford and Floyd-Warshall first since they're simpler.

Bellman-Ford

Resources
CPHup to but not including "Negative Cycles"

Floyd-Warshall

Focus Problem – read through this problem before continuing!

Tutorial

Resources
CPHexample calculation, code
cp-algocode, why it works
PAPScode, why it works
CP2
Optional: Incorrect Floyd-Warshall

Paper

A common mistake in implementing the Floyd–Warshall algorithm is to misorder the triply nested loops (The correct order is KIJ). The incorrect IJK and IKJ algorithms do not give correct solutions for some instance. However, we can prove that if these are repeated three times, we obtain the correct solutions.

It would be emphasized that these fixes (repeating incorrect algorithms three times) have the same time complexity as the correct Floyd–Warshall algorithm up to constant factors. Therefore, our results suggest that, if one is confused by the order of the triply nested loops, one can repeat the procedure three times just to be safe.

Problems

Used as the first step of the following:

StatusSourceProblem NameDifficultyTagsSolution
GoldHard
Show Tags

APSP, DP

External Sol

Dijkstra

Focus Problem – read through this problem before continuing!

O(N2)O(N^2)

The USACO training pages present a O(N2)O(N^2) implementation, although this is rarely used nowadays.

Resources
cp-algo

O(MlogN)O(M\log N)

O(MlogN)O(M\log N) Implementation

Resources
Benq

C++

Recall from the second prerequisite module that we can use greater<> to make the top element of a priority queue the least instead of the greatest. Alternatively, you can negate distances before placing them into the priority queue, but that's more confusing.

1#include <bits/stdc++.h>
2using namespace std;
3
4using ll = long long;
5#define pb push_back
6
7vector<pair<int, int>> graph[100000];
8// Adjacency list of (neighbour, edge weight)
9ll dist[100000];
10int N;

Java

1import java.util.*;
2import java.io.*;
3
4public class test {
5 static class Pair<K, V> {
6 public K a;
7 public V b;
8 public Pair(K a, V b) {
9 this.a = a;
10 this.b = b;

Warning!

It's important to include continue. This ensures that when all edge weights are non-negative, we will never go through the adjacency list of any vertex more than once. Removing it will cause TLE on the last test of the above problem!

This section is not complete.

Feel free to file a request to complete this using the "Contact Us" button.

(explanation of hack case)

Optional: Faster Dijkstra

Can be done in O(M+NlogN)O(M+N\log N) with Fibonacci heap. In practice though, this is rarely faster, since the Fibonacci heap has a bad constant factor.

Problems

StatusSourceProblem NameDifficultyTagsSolution
CSESEasy
Show Tags

SP

GoldEasy
Show Tags

SP

External Sol
GoldEasy
Show Tags

SP

External Sol
GoldEasy
Show Tags

SP

External Sol
GoldNormal
Show Tags

SP

External Sol
CSESNormal
Show Tags

SP

KattisNormal
Show Tags

SP

External Sol
CSESNormal
Show Tags

SP

IOIHard
Show Tags

SP

External Sol
JOIHard
Show Tags

SP, DP

APIOHard
Show Tags

SP, Geometry

Balkan OIHard
Show Tags

SP

Module Progress:

Give Us Feedback on Shortest Paths with Non-Negative Edge Weights!

PrevNext