-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathshortest_cycle.hpp
119 lines (101 loc) · 3.67 KB
/
shortest_cycle.hpp
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
#pragma once
#include <algorithm>
#include <cassert>
#include <limits>
#include <optional>
#include <queue>
#include <tuple>
#include <utility>
#include <vector>
// Shortest cycle detection of graphs
// Verified:
// - https://yukicoder.me/submissions/594507
// - ABC308Ex https://atcoder.jp/contests/abc308/submissions/43580864
template <bool DIRECTED, typename T> struct shortest_cycle {
int V;
std::vector<std::vector<std::tuple<int, int, T>>> to; // (nxt, edge_idx, weight)
std::vector<std::tuple<int, int, T>> edges;
shortest_cycle(int V = 0) : V(V), to(V) {}
void add_edge(int s, int t, T weight) {
static_assert(DIRECTED);
assert(0 <= s and s < V);
assert(0 <= t and t < V);
assert(weight >= 0);
to.at(s).emplace_back(t, (int)edges.size(), weight);
edges.emplace_back(s, t, weight);
}
void add_bi_edge(int s, int t, T weight) {
static_assert(!DIRECTED);
assert(0 <= s and s < V);
assert(0 <= t and t < V);
assert(weight >= 0);
to.at(s).emplace_back(t, (int)edges.size(), weight);
to.at(t).emplace_back(s, (int)edges.size(), weight);
edges.emplace_back(s, t, weight);
}
std::vector<T> dist;
std::vector<int> prv;
std::pair<T, std::pair<int, int>> Solve(const int &r) {
assert(0 <= r and r < V);
dist.assign(V, T());
prv.assign(V, -1);
std::vector<int> prve(V, -1);
std::vector<int> orig(V, -1);
auto reached = [&](int i) { return i == r or prv.at(i) != -1; };
std::priority_queue<std::pair<T, int>, std::vector<std::pair<T, int>>, std::greater<>> pq;
pq.emplace(dist.at(r), r);
while (!pq.empty()) {
const auto [d_, now] = pq.top();
pq.pop();
if (d_ > dist.at(now)) continue;
for (const auto &[nxt, eid, w] : to[now]) {
if (reached(nxt) and dist.at(nxt) <= dist.at(now) + w) continue;
dist.at(nxt) = dist.at(now) + w;
orig.at(nxt) = orig.at(now) < 0 ? nxt : orig.at(now);
prv.at(nxt) = now;
prve.at(nxt) = eid;
pq.emplace(dist.at(nxt), nxt);
}
}
std::vector<bool> is_edge_used(edges.size());
for (int eid : prve) {
if (eid >= 0) is_edge_used.at(eid) = true;
}
std::optional<T> minimum_cycle = std::nullopt;
int s = -1, t = -1;
for (int eid = 0; eid < (int)edges.size(); ++eid) {
if (is_edge_used.at(eid)) continue;
auto [a, b, w] = edges.at(eid);
if (!reached(a) or !reached(b)) continue;
if constexpr (DIRECTED) {
if (b != r) continue;
} else {
if (orig.at(a) == orig.at(b) and (a != r or b != r)) continue;
}
if (T L = dist.at(a) + dist.at(b) + w;
!minimum_cycle.has_value() or L < minimum_cycle.value()) {
minimum_cycle = L;
s = a, t = b;
}
}
return std::make_pair(minimum_cycle.value_or(T(-1)), std::make_pair(s, t));
}
std::vector<int> retrieve_loop(const std::pair<int, int> &ab) const {
if (ab.first < 0 or ab.second < 0) return {};
std::vector<int> ret;
bool initial = true;
for (int cur : {ab.first, ab.second}) {
while (cur >= 0) {
ret.push_back(cur);
cur = prv.at(cur);
}
if (initial) {
std::reverse(ret.begin(), ret.end());
initial = false;
} else {
ret.pop_back();
}
}
return ret;
}
};