-
Notifications
You must be signed in to change notification settings - Fork 0
/
dijkstra_algorithm.cpp
176 lines (152 loc) · 5 KB
/
dijkstra_algorithm.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include <bits/stdc++.h>
struct Edge {
size_t to;
size_t weight;
Edge(size_t to, size_t weight) : to(to), weight(weight) {}
bool operator==(const Edge& other) const {
return to == other.to && weight == other.weight;
}
};
enum class EdgeType { DIRECTED, UNDIRECTED };
class Graph {
public:
Graph(size_t number_of_vertices, EdgeType type = EdgeType::UNDIRECTED)
: adjacency_list_(number_of_vertices), edge_type_(type) {}
void addEdge(size_t u, size_t v, size_t weight) {
adjacency_list_[u].emplace_back(v, weight);
if (edge_type_ == EdgeType::UNDIRECTED) {
adjacency_list_[v].emplace_back(u, weight);
}
}
const std::vector<Edge>& getOutgoingEdges(size_t u) const {
return adjacency_list_[u];
}
size_t number_of_vertices() const { return adjacency_list_.size(); }
private:
EdgeType edge_type_;
std::vector<std::vector<Edge>> adjacency_list_;
};
class Dijkstra {
public:
Dijkstra(size_t number_of_vertices, EdgeType type = EdgeType::UNDIRECTED)
: graph_(number_of_vertices, type),
edge_type_(type),
first_edge_added_(false),
has_same_weights_(true) {}
void AddEdge(size_t u, size_t v, size_t weight) {
if (!first_edge_added_) {
initial_weight_ = weight;
first_edge_added_ = true;
} else if (has_same_weights_ && weight != initial_weight_) {
has_same_weights_ = false;
}
graph_.addEdge(u, v, weight);
}
std::vector<size_t> CalcShortestPath(size_t start) {
if (has_same_weights_) {
return DoDijkstra<std::queue<std::pair<size_t, size_t>>>(
start, std::nullopt, std::nullopt);
} else {
return DoDijkstra<std::priority_queue<
std::pair<size_t, size_t>, std::vector<std::pair<size_t, size_t>>,
std::greater<>>>(start, std::nullopt, std::nullopt);
}
}
size_t CalcShortestPath(
size_t start, size_t target,
std::optional<std::pair<size_t, size_t>> edge_deleted) {
if (has_same_weights_) {
return DoDijkstra<std::queue<std::pair<size_t, size_t>>>(
start, target, edge_deleted)[target];
} else {
return DoDijkstra<std::priority_queue<
std::pair<size_t, size_t>, std::vector<std::pair<size_t, size_t>>,
std::greater<>>>(start, target, edge_deleted)[target];
}
}
private:
Graph graph_;
EdgeType edge_type_;
bool has_same_weights_;
size_t initial_weight_;
bool first_edge_added_;
template <typename Container>
std::vector<size_t> DoDijkstra(
size_t start, std::optional<size_t> target,
std::optional<std::pair<size_t, size_t>> edge_deleted) {
int n = graph_.number_of_vertices();
std::vector<size_t> distance(n, std::numeric_limits<size_t>::max());
Container q;
q.emplace(0, start);
distance[start] = 0;
while (q.size()) {
size_t cur_dis, cur_node;
if constexpr (std::is_same_v<Container,
std::queue<std::pair<size_t, size_t>>>) {
std::tie(cur_dis, cur_node) = q.front();
} else if constexpr (std::is_same_v<
Container,
std::priority_queue<
std::pair<size_t, size_t>,
std::vector<std::pair<size_t, size_t>>,
std::greater<>>>) {
std::tie(cur_dis, cur_node) = q.top();
} else {
throw std::invalid_argument("Template parameter not supported");
}
q.pop();
if (cur_dis > distance[cur_node]) {
continue;
}
for (const auto& edge : graph_.getOutgoingEdges(cur_node)) {
if (edge_deleted == std::make_pair(cur_node, edge.to) ||
edge_deleted == std::make_pair(edge.to, cur_node)) {
continue;
}
size_t new_dis = cur_dis + edge.weight;
if (new_dis < distance[edge.to]) {
distance[edge.to] = new_dis;
q.emplace(new_dis, edge.to);
if (edge.to == target) {
return distance;
}
}
}
}
return distance;
}
};
using namespace std;
class Solution {
public:
int findShortestCycle(int n, vector<vector<int>>& edges) {
Dijkstra dijkstra(n);
for (auto& edge : edges) {
dijkstra.AddEdge(edge[0], edge[1], 1);
}
size_t ans = std::numeric_limits<size_t>::max();
for (auto& edge : edges) {
auto min_dis = dijkstra.CalcShortestPath(edge[0], edge[1],
make_pair(edge[0], edge[1]));
if (min_dis != numeric_limits<size_t>::max()) {
ans = min(ans, min_dis + 1);
}
}
return ans == std::numeric_limits<size_t>::max() ? -1 : ans;
}
};
int main() {
{
int n = 7;
vector<vector<int>> edges = {{0, 1}, {1, 2}, {2, 0}, {3, 4},
{4, 5}, {5, 6}, {6, 3}};
Solution sol;
cout << sol.findShortestCycle(n, edges) << endl;
}
{
int n = 4;
vector<vector<int>> edges = {{0, 1}, {0, 2}};
Solution sol;
cout << sol.findShortestCycle(n, edges) << endl;
}
}