#include #define int long long #define pb push_back using namespace std; const int N = 2e3 + 5; int T, n, m, u, v, w, ans = 1e18, dis[N]; bool vis[N], mark[N]; struct Node { int v, w, id; } ; vector g[N]; priority_queue, vector >, greater > > q; inline void dijkstra(int s) { fill(dis + 1, dis + 1 + n, 1e18); dis[s] = 0; q.push({0ll, s}); while(! q.empty()) { auto [_, u] = q.top(); q.pop(); if(vis[u]) continue ; vis[u] = true; for(auto [x, w, id] : g[u]) { if(mark[id]) continue ; if(dis[u] + w < dis[x]) { dis[x] = dis[u] + w; q.push({dis[x], x}); } } } return ; } signed main() { ios_base :: sync_with_stdio(NULL); cin.tie(nullptr); cout.tie(nullptr); cin >> T >> n >> m; for(int i = 1 ; i <= m ; ++ i) { cin >> u >> v >> w; g[u].pb({v, w, i}); if(! T) g[v].pb({u, w, i}); } for(int i = 1 ; i <= n ; ++ i) for(auto [j, w, id] : g[i]) { mark[id] = true; dijkstra(j); mark[id] = false; ans = min(ans, dis[i] + w); // cerr << "dis:" << dis[i] << '\n'; } cout << ans; return 0; }