#include #ifdef LOCAL #include #else #define debug(...) void(0) #endif template std::istream& operator>>(std::istream& is, std::vector& v) { for (auto& x : v) is >> x; return is; } template std::ostream& operator<<(std::ostream& os, const std::vector& v) { auto sep = ""; for (const auto& x : v) os << std::exchange(sep, " ") << x; return os; } template bool chmin(T& x, U&& y) { return y < x and (x = std::forward(y), true); } template bool chmax(T& x, U&& y) { return x < y and (x = std::forward(y), true); } template void mkuni(std::vector& v) { std::sort(begin(v), end(v)); v.erase(unique(begin(v), end(v)), end(v)); } template int lwb(const std::vector& v, const T& x) { return std::lower_bound(begin(v), end(v), x) - begin(v); } template struct DirectedShortestCycle { struct edge { int from, to; T cost; edge(int from, int to, T cost) : from(from), to(to), cost(cost) {} }; std::vector> G; std::vector edges; DirectedShortestCycle(int n) : n(n), G(n) {} void add_edge(int u, int v, T w) { assert(0 <= u and u < n); assert(0 <= v and v < n); assert(w >= 0); G[u].emplace_back(edges.size()); edges.emplace_back(u, v, w); } std::tuple, std::vector> solve(int r) { std::vector prve(n, -1); std::vector dist(n, inf); std::priority_queue, std::vector>, std::greater<>> pq; for (const int& idx : G[r]) { int u = edges[idx].to, w = edges[idx].cost; if (w < dist[u]) { dist[u] = w; prve[u] = idx; pq.emplace(dist[u], u); } } while (not pq.empty()) { auto [d, v] = pq.top(); pq.pop(); if (dist[v] < d) continue; for (const int& idx : G[v]) { int u = edges[idx].to, w = edges[idx].cost; if (dist[u] <= dist[v] + w) continue; dist[u] = dist[v] + w; prve[u] = idx; pq.emplace(dist[u], u); } } if (dist[r] == inf) return {inf, {}, {}}; std::vector vs, es; vs.emplace_back(r); while (true) { int idx = prve[vs.back()]; es.emplace_back(idx); vs.emplace_back(edges[idx].from); if (vs.back() == r) break; } std::reverse(vs.begin(), vs.end()); std::reverse(es.begin(), es.end()); vs.pop_back(); return {dist[r], vs, es}; } private: constexpr static T inf = std::numeric_limits::max() / 2; int n; }; template struct UndirectedShortestCycle { struct edge { int from, to; T cost; edge(int from, int to, T cost) : from(from), to(to), cost(cost) {} }; std::vector> G; std::vector edges; UndirectedShortestCycle(int n) : n(n), G(n) {} void add_edge(int u, int v, T w) { assert(0 <= u and u < n); assert(0 <= v and v < n); assert(w >= 0); G[u].emplace_back(edges.size()); G[v].emplace_back(edges.size()); edges.emplace_back(u, v, w); } std::tuple, std::vector> solve(int r) { std::vector label(n, -1), prve(n, -1); std::vector dist(n, inf); std::priority_queue, std::vector>, std::greater<>> pq; dist[r] = 0; pq.emplace(dist[r], r); label[r] = r; while (not pq.empty()) { auto [d, v] = pq.top(); pq.pop(); if (dist[v] < d) continue; for (const int& idx : G[v]) { int u = edges[idx].from ^ edges[idx].to ^ v, w = edges[idx].cost; if (dist[u] <= dist[v] + w) continue; dist[u] = dist[v] + w; label[u] = label[v] == r ? u : label[v]; prve[u] = idx; pq.emplace(dist[u], u); } } std::vector used(edges.size(), false); for (int& idx : prve) { if (idx != -1) { used[idx] = true; } } T mini = inf; int argmin = -1; for (int i = 0; i < int(edges.size()); i++) { if (used[i]) continue; auto [u, v, w] = edges[i]; if (label[u] == -1) continue; if (label[u] == label[v]) continue; if (dist[u] + dist[v] + w < mini) { mini = dist[u] + dist[v] + w; argmin = i; } } if (argmin == -1) return {mini, {}, {}}; std::vector vs, es; int a = edges[argmin].from, b = edges[argmin].to; while (a != r) { int idx = prve[a]; vs.emplace_back(a); es.emplace_back(idx); a = edges[idx].from ^ edges[idx].to ^ a; } vs.emplace_back(a); std::reverse(vs.begin(), vs.end()); std::reverse(es.begin(), es.end()); es.emplace_back(argmin); while (b != r) { int idx = prve[b]; vs.emplace_back(b); es.emplace_back(idx); b = edges[idx].from ^ edges[idx].to ^ b; } return {mini, vs, es}; } private: constexpr static T inf = std::numeric_limits::max() / 2; int n; }; constexpr int INF = (1 << 30) - 1; constexpr long long IINF = (1LL << 60) - 1; constexpr int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}; typedef long long ll; #define all(x) begin(x), end(x) using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; int N, M; cin >> N >> M; ll ans = IINF; if (T == 0) { UndirectedShortestCycle G(N); for (; M--;) { int u, v, w; cin >> u >> v >> w; G.add_edge(--u, --v, w); } for (int i = 0; i < N; i++) ans = min(ans, get<0>(G.solve(i))); } else { DirectedShortestCycle G(N); for (; M--;) { int u, v, w; cin >> u >> v >> w; G.add_edge(--u, --v, w); } for (int i = 0; i < N; i++) ans = min(ans, get<0>(G.solve(i))); } cout << (ans == IINF ? -1 : ans) << '\n'; return 0; }