// #pragma GCC target("avx512f") // #pragma GCC optimize("Ofast") // #pragma GCC optimize("unroll-loops") // ループの部分展開 #include // #include // using namespace atcoder; #ifdef LOCAL #include #define dbg(...) debug::dbg(#__VA_ARGS__, __VA_ARGS__) #else #define dbg(...) void(0) #endif using namespace std; // #define int ll #define rep(i, a, b) for(auto i=static_cast(a), i##_end__=static_cast(b); i < i##_end__; i++) #define rep_r(i, a, b) for(auto i=static_cast(a), i##_end__=static_cast(b); i >= i##_end__; i--) #define fore(i, a) for(auto& i: a) #define forei(i, it, a) for(auto [i, it] = std::pair{0, std::begin(a)}; it != std::end(a); i++, it++) #define all(x) std::begin(x), std::end(x) using ll = long long; // __int128; using ull = unsigned long long; template using priority_queue_asc = priority_queue, greater>; template ostream& operator<<(ostream& os, const vector& v){ rep(i, 0, v.size()){ os << (i == 0 ? "" : " ") << v[i]; } return os; } template istream& operator>>(istream& is, pair& p) { is >> p.first >> p.second; return is; } template istream& operator>>(istream& is, vector& v) { fore(e, v){ is >> e; } return is; } template int siz(const C& c) { return static_cast(c.size()); } template constexpr int siz(const T (&)[N]) { return static_cast(N); } template constexpr void fil(A& a, const V &v){ if constexpr(is_array_v) for(auto& s: a) fil(s, v); else a = v; }; template constexpr auto vct(size_t n, const V& v){ return vector(n, v); } template constexpr auto vct(size_t n, A... a){ return vector(n, vct(a...)); } template constexpr bool chmax(T& a, S b) { if (a < b) { a = b; return 1; } return 0; } template constexpr bool chmin(T& a, S b) { if (a > b) { a = b; return 1; } return 0; } template constexpr T pow2(T x){ return x * x; } template constexpr T divceil(T x, S div) { return (x % div == 0) ? x / div : (x >= 0) ? (x / div) + 1 : -((-x) / div); } template constexpr T divfloor(T x, S div){ return (x % div == 0) ? x / div : (x >= 0) ? (x / div) : -((-x) / div) - 1; } template inline void outif(bool c, T t, S f){ if(c) cout << t << endl; else cout << f << endl; } inline void yesno(bool x){ cout << (x ? "Yes" : "No") << endl; } constexpr long long INF = 1LL << 60; // 1.15e18 constexpr int MOD = (int)1e9 + 7; // 最短路木 - ヒープバージョン O((N+M)logN) // 戻り値: d[v]=最短距離(重み), p[v]=親頂点(rootの場合、-1), l[v]=通る経路の色分け。最初の頂点(root直下の頂点番号) template tuple, vector, vector> shortest_path_tree(const vector>>& g, int root, T inf = numeric_limits::max()){ const int n = siz(g); vector d(n, inf); vector p(n, -1), l(n, -1); vector done(n); priority_queue_asc> q; d[root] = 0; q.push({d[root], root}); while(!q.empty()){ auto [mn, v] = q.top(); q.pop(); if(done[v]) continue; done[v] = true; for(auto [e, w] : g[v]){ if(done[e]) continue; if(d[e] <= mn + w) continue; d[e] = mn + w; p[e] = v; l[e] = v == root ? e : l[v]; q.push({d[e], e}); } } return {d, p, l}; } // 無向グラフ 最小サイクル - ヒープバージョン O((N+M)logN) template pair> shortest_cycle_undirected(const vector>>& g, int root, T inf = numeric_limits::max()){ const int n = siz(g); auto [d, p, l] = shortest_path_tree(g, root, inf); T mn = inf; int mu = -1, mv = -1; rep(u, 0, n) for(auto [v, w] : g[u]){ if(u == root || v == root || l[u] == l[v] || d[u] >= inf || d[v] >= inf || w >= inf) continue; if(mn <= d[u] + d[v] + w) continue; mn = d[u] + d[v] + w; mu = u, mv = v; } if(mn == inf) return {inf, {}}; vector path; path.push_back(root); stack st; int v = mu; while(v != root) st.push(v), v = p[v]; while(!st.empty()) path.push_back(st.top()), st.pop(); v = mv; while(v != root) path.push_back(v), v = p[v]; return {mn, path}; } template pair> shortest_cycle_undirected_all(const vector>>& g, T inf = numeric_limits::max()){ T mn = inf; vector path; rep(i, 0, siz(g)){ auto [m, p] = shortest_cycle_undirected(g, i, inf); if(mn > m) mn = m, path = p; } return {mn, path}; } // 有向グラフ 最小サイクル - ヒープバージョン O((N+M)logN) template pair> shortest_cycle_directed(const vector>>& g, int root, T inf = numeric_limits::max()){ const int n = siz(g); auto [d, p, l] = shortest_path_tree(g, root, inf); T mn = inf; int mu = -1; rep(u, 0, n) for(auto [v, w] : g[u]){ if(u == root || v != root || d[u] >= inf || w >= inf) continue; if(mn <= d[u] + w) continue; mn = d[u] + w; mu = u; } if(mn == inf) return {inf, {}}; vector path; path.push_back(root); stack st; int v = mu; while(v != root) st.push(v), v = p[v]; while(!st.empty()) path.push_back(st.top()), st.pop(); return {mn, path}; } template pair> shortest_cycle_directed_all(const vector>>& g, T inf = numeric_limits::max()){ T mn = inf; vector path; rep(i, 0, siz(g)){ auto [m, p] = shortest_cycle_directed(g, i, inf); if(mn > m) mn = m, path = p; } return {mn, path}; } void _main() { int T, N, M; cin >> T >> N >> M; constexpr auto inf = numeric_limits::max(); vector g(N, vector>()); rep(i, 0, M){ int u, v, w; cin >> u >> v >> w; u--; v--; g[u].push_back({v, w}); if(T == 0) g[v].push_back({u, w}); } if(T == 0){ auto [mn, path] = shortest_cycle_undirected_all(g, inf); outif(mn < inf, mn, -1); } else{ auto [mn, path] = shortest_cycle_directed_all(g, inf); outif(mn < inf, mn, -1); } } signed main() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); cerr << fixed << setprecision(15); _main(); }