#include using namespace std; using ll = long long; #define all(a) (a).begin(), (a).end() #define pb push_back #define fi first #define se second mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count()); const ll MOD1000000007 = 1000000007; const ll MOD998244353 = 998244353; const ll MOD[3] = {999727999, 1070777777, 1000000007}; const ll LINF = 1LL << 60LL; const int IINF = (1 << 30) - 2; template struct edge{ int from; int to; T cost; int id; edge(){} edge(int to, T cost=1) : from(-1), to(to), cost(cost){} edge(int to, T cost, int id) : from(-1), to(to), cost(cost), id(id){} edge(int from, int to, T cost, int id) : from(from), to(to), cost(cost), id(id){} void reverse(){swap(from, to);} }; template struct edges : std::vector>{ void sort(){ std::sort( (*this).begin(), (*this).end(), [](const edge& a, const edge& b){ return a.cost < b.cost; } ); } }; template struct graph : std::vector>{ private: int n = 0; int m = 0; edges es; bool dir; public: graph(int n, bool dir) : n(n), dir(dir){ (*this).resize(n); } void add_edge(int from, int to, T cost=1){ if(dir){ es.push_back(edge(from, to, cost, m)); (*this)[from].push_back(edge(from, to, cost, m++)); }else{ if(from > to) swap(from, to); es.push_back(edge(from, to, cost, m)); (*this)[from].push_back(edge(from, to, cost, m)); (*this)[to].push_back(edge(to, from, cost, m++)); } } int get_vnum(){ return n; } int get_enum(){ return m; } bool get_dir(){ return dir; } edge get_edge(int i){ return es[i]; } edges get_edge_set(){ return es; } }; template struct redge{ int from, to; T cap, cost; int rev; redge(int to, T cap, T cost=(T)(1)) : from(-1), to(to), cap(cap), cost(cost){} redge(int to, T cap, T cost, int rev) : from(-1), to(to), cap(cap), cost(cost), rev(rev){} }; template using Edges = vector>; template using weighted_graph = vector>; template using tree = vector>; using unweighted_graph = vector>; template using residual_graph = vector>>; template struct dijkstra{ private: const T TINF = numeric_limits::max()/2; int n, s; graph G; vector dist; vector vpar; edges epar; public: dijkstra(graph G, int s) : G(G), s(s){ // initilization n = G.get_vnum(); dist.resize(n, TINF); vpar.resize(n, -1); epar.resize(n); // running Dijkstra algorithm run(G, s); } void run(graph &G, int s){ dist[s] = 0; priority_queue, vector>, greater<>> que; que.push({0, s}); while(!que.empty()){ auto [d, v] = que.top(); que.pop(); if(dist[v] < d) continue; for(auto e : G[v]){ if(dist[v] + e.cost < dist[e.to]){ dist[e.to] = dist[v] + e.cost; vpar[e.to] = v; epar[e.to] = e; que.push({dist[e.to], e.to}); } } } } T get_dist(int t){ return dist[t]; } vector get_dist(){ return dist; } vector get_vpar(){ return vpar; } int get_vpar(int v){ return vpar[v]; } edges get_epar(){ return epar; } edge get_epar(int v){ return epar[v]; } vector get_vpath(int t){ vector vpath; int cur = t; while(cur != -1){ vpath.push_back(cur); cur = vpar[cur]; } reverse(vpath.begin(), vpath.end()); return vpath; } edges get_epath(int t){ edges epath; int cur = t; while(cur != s){ epath.push_back(epar[cur]); cur = vpar[cur]; } reverse(epath.begin(), epath.end()); return epath; } graph get_shotest_path_tree(){ graph spt(n, false); for(int v=0; v static edges find_cycle(graph &G){ int n = G.get_vnum(); vector used(n, 0); edges cyc; function dfs = [&](int v, int e_id){ for(auto e : G[v]) if(e.id != e_id){ if(used[e.to]==1){ cyc.push_back(e); return true; }else if(used[e.to]==0){ used[e.to] = used[v]; if(dfs(e.to, e.id)){ cyc.push_back(e); return true; } } } used[v] = 2; return false; }; for(int v=0; v static edges find_oddcycle(graph &G, int s){ } template static edges find_evencycle(graph &G, int s){ } template static edges find_mincostcycle(graph &G, int s){ int n = G.get_vnum(); const S SINF = numeric_limits::max()/2; bool dir = G.get_dir(); dijkstra dijk(G, s); auto dist = dijk.get_dist(); edges cyc; // find minimum cost cycle on directed graph if(dir){ S cost = SINF; edge emin; for(int v=0; v> ch(n); for(int v=0; v label(n, -1); label[s] = s; function labeling = [&](int v, int l){ label[v] = l; for(int to : ch[v]) labeling(to, l); }; for(int to : ch[s]) labeling(to, to); S cost = SINF; edge emin; for(int v=0; v static edges find_mincostcycle(graph &G){ int n = G.get_vnum(); const S SINF = numeric_limits::max()/2; S cost = SINF; edges min_cyc; for(int s=0; s static edges find_minmeancycle(graph &G){ } template static edges enumerate_3cycle(graph &G){ } template static edges enumerate_4cycle(graph &G){ } }; void solve(){ int dir; cin >> dir; int n, m; cin >> n >> m; graph G(n, dir); for(int i=0; i> u >> v; u--; v--; ll w; cin >> w; G.add_edge(u, v, w); } auto cycle = cycle::find_mincostcycle(G); if(cycle.empty()){ cout << -1 << '\n'; return; } ll ans = 0; for(auto e : cycle) ans += e.cost; cout << ans << '\n'; } int main(){ cin.tie(nullptr); ios::sync_with_stdio(false); int T=1; //cin >> T; while(T--) solve(); }