-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdijkstra.cpp
54 lines (42 loc) · 1.33 KB
/
dijkstra.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//
// Created by sunnysab on 24-9-30.
//
#include "graph.h"
#include "dijkstra.h"
#include "file.h"
Dijkstra::ContinuousDijkstra(const Graph& g, Vertex source)
: graph(const_cast<Graph&>(g)), source(source), distance(source) {
pq.emplace(0, source);
distance.set(source, 0);
}
/// Throw exception if there is no path between source and target
EdgeWeight Dijkstra::get(const Vertex target) {
if (source == target)
return 0;
if (const auto cached = this->distance[target]; cached != InfWeight)
return cached;
while (!pq.empty()) {
auto [d, v] = pq.top();
pq.pop();
if (d > distance[v]) continue;
auto return_flag = false;
if (v == target) return_flag = true;
for (const auto& [adj_v, w] : graph.get_adjacent_vertices(v)) {
if (auto old_d = distance[adj_v], new_d = distance[v] + w; new_d < old_d) {
distance.set(adj_v, new_d);
pq.emplace(new_d, adj_v);
}
}
if (return_flag) {
return d;
}
}
throw std::runtime_error("no path found");
}
int _main() {
auto graph = load_graph("USA-road-t.NY-stripped-1000.gr");
auto dijkstra = Dijkstra(graph, 810);
auto result = dijkstra.get(1237);
std::cout << "result = " << result << std::endl;
return 0;
}