This repository has been archived on 2022-06-22. You can view files and clone it, but cannot push or open issues or pull requests.
usaco-guide/content/5_Gold/SP.mdx
Benjamin Qi b9fbac192b + Gold
2020-07-03 16:29:32 -04:00

111 lines
No EOL
3.9 KiB
Text
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
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 Cowmbat", "971", "Hard", false, ["APSP", "DP"], ""),
],
}
};
<problems-list problems={metadata.problems.sample} />
## Tutorial
Use *Dijkstra's Algorithm*.
<resources>
<resource source="CPH" title="13.2 - Dijkstra" starred>code</resource>
<resource source="cp-algo" title="Dijkstra (Dense Graphs)" url="graph/dijkstra_dense.html"></resource>
<resource source="cp-algo" title="Dijkstra (Sparse Graphs)" url="graph/dijkstra_sparse.html"></resource>
<resource source="CPC" title="8 - Graphs 2" url="08_graphs_2"></resource>
</resources>
## Implementation
### Part 1: $O(N^2)$
The USACO training pages present a $O(N^2)$ version, although this is rarely used nowadays. (but describe anyways?)
### Part 2: $O(M\log N)$
<resources>
<resource source="Benq" title="Dijkstra" url="https://github.com/bqi343/USACO/blob/master/Implementations/content/graphs%20(12)/Basics/Dijkstra%20(7.3).h"> </resource>
</resources>
(comments in code?)
<optional-content title="Faster Dijkstra">
Can be done in $O(M+N\log N)$ with [Fibonacci heap](https://en.wikipedia.org/wiki/Fibonacci_heap).
</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.
### Tutorial
<resources>
<resource source="CPH" title="13.3 - Floyd-Warshall" starred>example calculation, code</resource>
<resource source="PAPS" title="12.3.3"></resource>
<resource source="cp-algo" title="Floyd-Warshall" url="graph/all-pair-shortest-path-floyd-warshall.html"></resource>
</resources>
<optional-content title="Incorrect Floyd-Warshall">
[Paper](https://arxiv.org/pdf/1904.01210.pdf)
> A common mistake in implementing the FloydWarshall 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 FloydWarshall 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.
</optional-content>
### Problems
<problems-list problems={metadata.problems.apsp} />
(more?)