#include using namespace std; using ll = long long; template using vec = vector; template using vvec = vector>; template bool chmin(T& a,T b){if(a>b) {a = b; return true;} return false;} template bool chmax(T& a,T b){if(a=0;i--) #define all(x) (x).begin(),(x).end() #define debug(x) cerr << #x << " = " << (x) << endl; using P = pair; constexpr ll inf = 1e18; void dijkstra(int s,vvec

& g,vec& dist, pair forbid = {-1,-1}){ dist[s] = 0; priority_queue,greater

> Q; Q.push({0,s}); while(!Q.empty()){ auto [d,cur] = Q.top(); Q.pop(); if(dist[cur] p = {to,cur}; if(p.first>p.second) swap(p.first,p.second); if(forbid!=p){ if(chmin(dist[to],dist[cur]+c)){ Q.push({dist[to],to}); } } } } } void solve0(){ int N,M; cin >> N >> M; vvec

g(N); rep(i,M){ int a,b,c; cin >> a >> b >> c; a--,b--; g[a].emplace_back(c,b); g[b].emplace_back(c,a); } ll ans = inf; rep(i,N){ for(auto& [c,to]:g[i]){ vec dist(N,inf); dijkstra(i,g,dist,minmax({i,to})); chmin(ans,dist[to]+c); } } cout << (ans!=inf? ans:-1) << "\n"; } void solve1(){ int N,M; cin >> N >> M; vvec

g(N); rep(i,M){ int a,b,c; cin >> a >> b >> c; a--,b--; g[a].emplace_back(c,b); } ll ans = inf; rep(i,N){ vec dist(N,inf); dijkstra(i,g,dist); rep(j,N){ for(auto& [c,to]:g[j]) if(to==i) chmin(ans,dist[j]+c); } } cout << (ans!=inf? ans:-1) << "\n"; } int main(){ cin.tie(0); ios::sync_with_stdio(false); int T; cin >> T; if(T==0) solve0(); else solve1(); }