#line 1 "graph/test/shortest_cycle.test.cpp" #define PROBLEM "https://yukicoder.me/problems/no/1320" #line 2 "graph/shortest_cycle.hpp" #include #include #include #include #include #include #include #include // Shortest cycle detection of graphs // Verified: https://yukicoder.me/submissions/594507 template struct shortest_cycle { int V; std::vector>> to; // (nxt, edge_idx, weight) std::vector> 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 dist; std::vector prv; std::pair> Solve(const int &v) { assert(0 <= v and v < V); dist.assign(V, T()); prv.assign(V, -1); std::vector prve(V, -1); std::vector orig(V, -1); auto reached = [&](int i) { return i == v or prv.at(i) != -1; }; std::priority_queue, std::vector>, std::greater<>> pq; pq.emplace(dist.at(v), v); 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 is_edge_used(edges.size()); for (int eid : prve) { if (eid >= 0) is_edge_used.at(eid) = true; } std::optional 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 != v) continue; } else { if (orig.at(a) == orig.at(b) and (a != v or b != v)) 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 retrieve_loop(const std::pair &ab) const { if (ab.first < 0 or ab.second < 0) return {}; std::vector 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; } }; #line 4 "graph/test/shortest_cycle.test.cpp" #include #line 7 "graph/test/shortest_cycle.test.cpp" using namespace std; int main() { int T, N, M; cin >> T >> N >> M; const long long INF = 1LL << 60; long long ret = INF; if (T == 1) { // Directed graph shortest_cycle graph(N); while (M--) { int u, v, w; cin >> u >> v >> w; u--, v--; graph.add_edge(u, v, w); } for (int s = 0; s < N; s++) { auto len = graph.Solve(s).first; if (len >= 0) ret = min(ret, len); } } else { // Undirected graph shortest_cycle graph(N); while (M--) { int u, v, w; cin >> u >> v >> w; u--, v--; graph.add_bi_edge(u, v, w); } for (int i = 0; i < N; i++) { auto len = graph.Solve(i).first; if (len >= 0) ret = min(ret, len); } } cout << (ret < INF ? ret : -1) << '\n'; }