90 lines
No EOL
2.9 KiB
Text
90 lines
No EOL
2.9 KiB
Text
---
|
|
id: sp
|
|
title: "Shortest Paths with Non-Negative Edge Weights"
|
|
author: Benjamin Qi
|
|
prerequisites:
|
|
- Gold - Breadth First Search
|
|
description: Introduces Dijkstra's Algorithm for a single source as well as Floyd-Warshall for All-Pairs Shortest Path.
|
|
frequency: 3
|
|
---
|
|
|
|
import { Problem } from "../models";
|
|
|
|
export const metadata = {
|
|
problems: {
|
|
sample: [
|
|
new Problem("CSES", "Shortest Routes I", "1671", "Easy", false, ["SP"], "equivalent to [Kattis SSSP Non-Negative](https://open.kattis.com/problems/shortestpath1)"),
|
|
],
|
|
dijk: [
|
|
new Problem("CSES", "Flight Discount", "1195", "Easy", false, ["SP"], "one edge modified"),
|
|
new Problem("CSES", "Flight Routes", "1196", "Easy", false, ["SP"], "$k$ smallest paths"),
|
|
new Problem("CSES", "Investigation", "1202", "Easy", false, ["SP"], ""),
|
|
new Problem("Gold", "Milk Pumping", "969", "Easy", false, ["SP"], ""),
|
|
new Problem("Gold", "Visit FJ", "717", "Easy", false, ["SP"], ""),
|
|
new Problem("Gold", "Shortcut", "899", "Easy", false, ["SP"], ""),
|
|
new Problem("Gold", "Fine Dining", "861", "Easy", false, ["SP"], ""),
|
|
new Problem("Kattis", "Robot Turtles", "robotturtles", "Easy", false, ["SP"], ""),
|
|
new Problem("Kat", "Lane Switching", "https://open.kattis.com/contests/acpc17open/problems/laneswitching", "Normal", false, ["SP"], ""),
|
|
],
|
|
apspSam: [
|
|
new Problem("CSES", "Shortest Routes II", "1672", "Easy", false, ["APSP"], ""),
|
|
],
|
|
apsp: [
|
|
new Problem("Gold", "Moortal Combat", "971", "Hard", false, ["APSP", "DP"], ""),
|
|
],
|
|
}
|
|
};
|
|
|
|
## Non-Negative Edge Weights
|
|
|
|
<problems-list problems={metadata.problems.sample} />
|
|
|
|
Use *Dijkstra's Algorithm*.
|
|
|
|
### Tutorial
|
|
|
|
- CSES 13.2
|
|
- [PAPS 12.3.1](https://www.csc.kth.se/~jsannemo/slask/main.pdf)
|
|
- [cp-algo Dijkstra (Dense Graphs)](https://cp-algorithms.com/graph/dijkstra_sparse.html)
|
|
- [cp-algo Dijkstra (Sparse Graphs)](https://cp-algorithms.com/graph/dijkstra_sparse.html)
|
|
- Usually, it's this one that's applicable.
|
|
- [CPC.8](https://github.com/SuprDewd/T-414-AFLV/tree/master/08_graphs_2)
|
|
|
|
### $O(M\log N)$ implementation
|
|
|
|
The USACO training pages present a $O(N^2)$ version, although this is rarely used nowadays.
|
|
|
|
<optional-content title="Faster Dijkstra">
|
|
|
|
Can be done in $O(M+N\log N)$ with Fibonacci heap.
|
|
|
|
(link?)
|
|
|
|
</optional-content>
|
|
|
|
### Problems
|
|
|
|
<problems-list problems={metadata.problems.dijk} />
|
|
|
|
## All Pairs Shortest Path (APSP)
|
|
|
|
<problems-list problems={metadata.problems.apspSam} />
|
|
|
|
Use the *Floyd-Warshall* algorithm.
|
|
|
|
### Standard
|
|
|
|
- [CSES Shortest Routes II](https://cses.fi/problemset/task/1672)
|
|
|
|
### Tutorial
|
|
|
|
- CPH 13.3
|
|
- [PAPS 12.3.3](https://www.csc.kth.se/~jsannemo/slask/main.pdf)
|
|
- [cp-algo Floyd-Warshall](https://cp-algorithms.com/graph/all-pair-shortest-path-floyd-warshall.html)
|
|
|
|
### Problems
|
|
|
|
<problems-list problems={metadata.problems.apsp} />
|
|
|
|
- [USACO Moortal Cowmbat](http://usaco.org/index.php?page=viewproblem2&cpid=971)
|
|
- Use APSP before running DP. |