#include using namespace std; using ll = long long; using pll = pair; #define drep(i, cc, n) for (ll i = (cc); i <= (n); ++i) #define rep(i, n) drep(i, 0, n - 1) #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) - 1; 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>{ int n = 0; int m = 0; edges es; bool directed; graph(int n, bool directed) : n(n), directed(directed){ (*this).resize(n); } void add_edge(int from, int to, T cost=1){ if(directed){ 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++)); } } }; 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 tuple, vector> mincostcycle(graph g, int s){ struct dat{ T dist; int pv; int pe; }; int n = g.n; const T TINF = numeric_limits::max()/2; bool is_directed = g.directed; // return shortest path tree vec, vec[v] = {dist(r, v), parent of r, edge id of (parent of r, r)}; function()> dijkstra = [&](){ vector vec(n, {TINF, -1, -1}); vec[s].dist = 0; priority_queue, vector>, greater<>> pq; pq.push({0, s}); while(!pq.empty()){ auto [tmp, v] = pq.top(); pq.pop(); if(vec[v].dist < tmp) continue; for(edge e : g[v]){ if(vec[v].dist+e.cost < vec[e.to].dist){ vec[e.to].dist = vec[v].dist + e.cost; vec[e.to].pv = v; vec[e.to].pe = e.id; pq.push({vec[e.to].dist, e.to}); } } } return vec; }; // g is a directed graph if(is_directed){ vector vec = dijkstra(); T cost = TINF; int vlast = -1; int elast = -1; for(int v=0; v e : g[v]) if(e.to == s){ if(vec[v].dist + e.cost < cost){ cost = vec[v].dist + e.cost; vlast = v; elast = e.id; } } if(cost == TINF) return {T(-1), {}, {}}; vector vcycle; vector ecycle; ecycle.push_back(elast); while(vlast != -1){ vcycle.push_back(vlast); ecycle.push_back(vec[vlast].pe); vlast = vec[vlast].pv; } ecycle.pop_back(); reverse(all(vcycle)); reverse(all(ecycle)); return {cost, vcycle, ecycle}; } // g is an undirected graph if(!is_directed){ vector vec = dijkstra(); graph spt(n, false); for(int v=0; v label(n, -1); label[s] = s; function dfs = [&](int v, int p, int l){ label[v] = l; for(edge e : spt[v]) if(e.to != p) dfs(e.to, v, l); }; for(edge e : spt[s]) dfs(e.to, s, e.to); T cost = TINF; int x = -1, y = -1, min_e = -1; for(int v=0; v e : g[v]){ if(vec[v].pv != e.to && label[v] != label[e.to]){ if(vec[v].dist + vec[e.to].dist + e.cost < cost){ cost = vec[v].dist + vec[e.to].dist + e.cost; x = v; y = e.to; min_e = e.id; } } } if(cost == TINF) return {T(-1), {}, {}}; vector vcycle, ecycle; ecycle.push_back(min_e); while(x != -1){ vcycle.push_back(x); ecycle.push_back(vec[x].pe); x = vec[x].pv; } ecycle.pop_back(); reverse(all(vcycle)); reverse(all(ecycle)); while(y != s){ vcycle.push_back(y); ecycle.push_back(vec[y].pe); y = vec[y].pv; } return {cost, vcycle, ecycle}; } } template tuple, vector> mincostcycle(graph g){ int n = g.n; const T TINF = numeric_limits::max()/2; T cost = TINF; vector vcycle; vector ecycle; for(int r=0; r(g, r); if(r_cost != -1 && r_cost < cost){ cost = r_cost; vcycle = r_vcycle; ecycle = r_ecycle; } } if(cost == TINF) return {T(-1), {}, {}}; else return {cost, vcycle, ecycle}; } void solve(){ bool type; cin >> type; int n, m; cin >> n >> m; graph g(n, type); for(int i=0; i> u >> v; ll w; cin >> w; u--; v--; g.add_edge(u, v, w); } auto [cost, vcycle, ecycle] = mincostcycle(g); cout << cost << '\n'; } int main(){ cin.tie(nullptr); ios::sync_with_stdio(false); int T=1; //cin >> T; while(T--) solve(); }