#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, 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){} }; 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 directed_min_weight_cycle{ int n; weighted_graph G; const T TINF = numeric_limits::max()/2; directed_min_weight_cycle(weighted_graph G_) : G(G_){ n = (int)G.size(); } // return dist, dist[v] = {dist(r, v), parent of r}; vector> dijkstra(int r){ vector> dist(n, {TINF, -1}); dist[r].first = 0; priority_queue, vector>, greater<>> pq; pq.push({0, r}); while(!pq.empty()){ auto [tmp, v] = pq.top(); pq.pop(); if(dist[v].first < tmp) continue; for(edge e : G[v]){ if(dist[v].first+e.cost < dist[e.to].first){ dist[e.to].first = dist[v].first+e.cost; dist[e.to].second = v; pq.push({dist[e.to].first, e.to}); } } } return dist; } /* # return weight of min weight cycle including vertex r # if there does not exist cycle, return -1 */ T get_weight(int r){ vector> dist = dijkstra(r); T ans = TINF; for(int v=0; v e : G[v]) if(e.to == r){ ans = min(ans, dist[v].first + e.cost); } if(ans == TINF) return T(-1); else return ans; } /* # return weight of min weight cycle # if there does not exist cycle, return -1 */ T get_weight(){ T ans = TINF; for(int r=0; r get_cycle(int r){ vector> dist = dijkstra(r); T weight = TINF; int last = -1; for(int v=0; v e : G[v]) if(e.to == r){ if(dist[v].first + e.cost < weight){ weight = dist[v].first + e.cost; last = v; } } vector ans; while(last != -1){ ans.push_back(last); last = dist[last].second; } reverse(all(ans)); return ans; } vector get_cycle(){ vector ans; T weight = TINF; for(int r=0; r> dist = dijkstra(r); T sw = TINF; int last = -1; for(int v=0; v e : G[v]) if(e.to == r){ if(dist[v].first + e.cost < sw){ sw = dist[v].first + e.cost; last = v; } } if(weight <= sw) continue; vector ans; while(last != -1){ ans.push_back(last); last = dist[last].second; } } reverse(all(ans)); return ans; } }; template struct undirected_min_weight_cycle{ int n; weighted_graph G; const T TINF = numeric_limits::max()/2; undirected_min_weight_cycle(weighted_graph G_) : G(G_){ n = (int)G.size(); } // return dist, dist[v] = {dist(r, v), parent of r}; vector> dijkstra(int r){ vector> dist(n, {TINF, -1}); dist[r].first = 0; priority_queue, vector>, greater<>> pq; pq.push({0, r}); while(!pq.empty()){ auto [tmp, v] = pq.top(); pq.pop(); if(dist[v].first < tmp) continue; for(edge e : G[v]){ if(dist[v].first+e.cost < dist[e.to].first){ dist[e.to].first = dist[v].first+e.cost; dist[e.to].second = v; pq.push({dist[e.to].first, e.to}); } } } return dist; } /* # return weight of min weight cycle including vertex r # if there does not exist cycle, return -1 */ T get_weight(int r){ vector> dist = dijkstra(r); tree spt(n); for(int v=0; v(dist[v].second)); spt[dist[v].second].push_back(edge(v)); } vector label(n, -1); label[r] = r; 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[r]){ dfs(e.to, r, e.to); } T ans = TINF; for(int v=0; v e : G[v]){ if(dist[v].second != e.to && label[v] != label[e.to]){ ans = min(ans, dist[v].first + dist[e.to].first + e.cost); } } if(ans == TINF) return T(-1); else return ans; } /* # return weight of min weight cycle # if there does not exist cycle, return -1 */ T get_weight(){ T ans = TINF; for(int r=0; r> type >> n >> m; weighted_graph G(n); for(int i=0; i> u >> v; u--; v--; ll w; cin >> w; G[u].push_back(edge(v, w)); if(type==0) G[v].push_back(edge(u, w)); } if(type == 0){ undirected_min_weight_cycle mwc(G); cout << mwc.get_weight() << '\n'; } if(type == 1){ directed_min_weight_cycle mwc(G); cout << mwc.get_weight() << '\n'; } } int main(){ cin.tie(nullptr); ios::sync_with_stdio(false); int T=1; //cin >> T; while(T--) solve(); }