#include #include #include #include #include // Shortest cycle detection of UNDIRECTED SIMPLE graphs // Verified: template struct ShortestCycleOfUndirectedWeighted { int V, E; std::vector>> to; // (nxt, weight) const T INF; ShortestCycleOfUndirectedWeighted() = default; ShortestCycleOfUndirectedWeighted(int V) : V(V), E(0), to(V), INF(std::numeric_limits::max() / 2) {} void add_edge(int s, int t, T len) { assert(0 <= s and s < V); assert(0 <= t and t < V); assert(len >= 0); to[s].emplace_back(t, len); to[t].emplace_back(s, len); E++; } std::vector dist; std::vector prev; // Find minimum length simple cycle which passes vertex `v` // - Complexity: O(E log V) // - return: (LEN, (a, b)) // - LEN: length of the shortest cycles if exists, INF ( = numeric_limits::max() / 2 ) otherwise. // - the cycle consists of vertices [v, ..., prev[prev[a]], prev[a], a, b, prev[b], prev[prev[b]], ..., v] std::pair> Solve(int v) { assert(0 <= v and v < V); dist.assign(V, INF), dist[v] = 0; prev.assign(V, -1); using P = std::pair>; std::priority_queue, std::greater

> pq; std::vector, T>> add_edge; pq.emplace(0, std::make_pair(v, -1)); while (!pq.empty()) { const int now = pq.top().second.first, prv = pq.top().second.second; pq.pop(); for (const auto &nxt : to[now]) if (nxt.first != prv) { if (dist[nxt.first] == INF) { dist[nxt.first] = dist[now] + nxt.second; prev[nxt.first] = now; pq.emplace(dist[nxt.first], std::make_pair(nxt.first, now)); } else { add_edge.emplace_back(std::make_pair(now, nxt.first), nxt.second); } } } T minimum_cycle = INF; int s = -1, t = -1; for (auto edge : add_edge) { int a = edge.first.first, b = edge.first.second; T L = dist[a] + dist[b] + edge.second; if (L < minimum_cycle) minimum_cycle = L, s = a, t = b; } return std::make_pair(minimum_cycle, std::make_pair(s, t)); } }; #include using namespace std; using lint = long long; struct fast_ios { fast_ios(){ cin.tie(nullptr), ios::sync_with_stdio(false), cout << fixed << setprecision(20); }; } fast_ios_; #define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i bool chmin(T &m, const T q) { if (m > q) {m = q; return true;} else return false; } template struct ShortestPath { int V, E; int INVALID = -1; std::vector>> to; ShortestPath() = default; ShortestPath(int V) : V(V), E(0), to(V) {} void add_edge(int s, int t, T len) { assert(0 <= s and s < V); assert(0 <= t and t < V); to[s].emplace_back(t, len); E++; } std::vector dist; std::vector prev; // Dijkstra algorithm // Complexity: O(E log E) void Dijkstra(int s) { assert(0 <= s and s < V); dist.assign(V, std::numeric_limits::max()); dist[s] = 0; prev.assign(V, INVALID); using P = std::pair; std::priority_queue, std::greater

> pq; pq.emplace(0, s); while (!pq.empty()) { T d; int v; std::tie(d, v) = pq.top(); pq.pop(); if (dist[v] < d) continue; for (auto nx : to[v]) { T dnx = d + nx.second; if (dist[nx.first] > dnx) { dist[nx.first] = dnx, prev[nx.first] = v; pq.emplace(dnx, nx.first); } } } } }; int main() { int T, N, M; cin >> T >> N >> M; lint ret = 1e18; if (T == 1) { vector>> to(N); while (M--) { int u, v, w; cin >> u >> v >> w; u--, v--; to[u].emplace_back(v, w); } REP(s, N) { ShortestPath graph(N + 1); REP(i, N) for (auto [j, w] : to[i]) { graph.add_edge(i, j, w); if (j == s) graph.add_edge(i, N, w); } graph.Dijkstra(s); chmin(ret, graph.dist[N]); } } else { ShortestCycleOfUndirectedWeighted graph(N); while (M--) { int u, v, w; cin >> u >> v >> w; u--, v--; graph.add_edge(u, v, w); } REP(i, N) chmin(ret, graph.Solve(i).first); } cout << (ret < 1e18 ? ret : -1) << '\n'; }