#include #include #include #include #include #include #include #include using namespace std; /* * @title Graph * @docs md/graph/Graph.md */ template class Graph{ private: const size_t N,H,W; public: vector>> edges; Graph(const size_t N):H(-1),W(-1),N(N), edges(N) {} Graph(const size_t H, const size_t W):H(H),W(W),N(H*W), edges(H*W) {} inline void make_edge(size_t from, size_t to, T w) { edges[from].emplace_back(to,w); } //{from_y,from_x} -> {to_y,to_x} inline void make_edge(pair from, pair to, T w) { make_edge(from.first*W+from.second,to.first*W+to.second,w); } inline void make_bidirectional_edge(size_t from, size_t to, T w) { make_edge(from,to,w); make_edge(to,from,w); } inline void make_bidirectional_edge(pair from, pair to, T w) { make_edge(from.first*W+from.second,to.first*W+to.second,w); make_edge(to.first*W+to.second,from.first*W+from.second,w); } inline size_t size(){return N;} inline size_t idx(pair yx){return yx.first*W+yx.second;} }; /* * @title MinimumDirectedClosedCircuit - 有向グラフの最小閉路検出 * @docs md/graph/MinimumDirectedClosedCircuit.md */ template class MinimumDirectedClosedCircuit { //Tは整数型のみ static_assert(std::is_integral::value, "template parameter T must be integral type"); Graph& graph; vector dist; vector parent; size_t N; T inf; int last,root; private: T solve_impl() { T mini = inf; last = -1; using plli = pair; priority_queue,greater> q; q.push({0,root}); dist[root] = 0; while (q.size()) { auto top = q.top(); q.pop(); size_t curr = top.second; /* 嘘dijkstra if(top.first > dist[curr]) continue; */ for(auto& edge:graph.edges[curr]){ size_t next = edge.first; T w = edge.second; if(dist[next] > dist[curr]+w) { dist[next] = dist[curr] + w; parent[next] = curr; q.push({dist[next],next}); } //根に返って来てるなら閉路候補 if(next == root && mini > dist[curr]+w) { mini = dist[curr]+w; last = curr; } } } return mini; } public: MinimumDirectedClosedCircuit(Graph& graph, T inf) : graph(graph),N(graph.size()),dist(graph.size()),parent(graph.size()),inf(inf) { } //rootを含む最小閉路の集合を返す O(NlogN) 閉路がないときは空集合 inline T solve(size_t rt){ root = rt; //初期化 for(int i = 0; i < N; ++i) dist[i] = inf, parent[i] = -1; //最小閉路の大きさを決める T mini = solve_impl(); return mini; } vector restore() { vector res; if(last == -1) return res; int curr = last; res.push_back(curr); while(curr != root) res.push_back(curr = parent[curr]); reverse(res.begin(),res.end()); return res; } }; /* * @title MinimumUndirectedClosedCircuit - 無向グラフの最小閉路検出 * @docs md/graph/MinimumUndirectedClosedCircuit.md */ template class MinimumUndirectedClosedCircuit { //Tは整数型のみ static_assert(std::is_integral::value, "template parameter T must be integral type"); Graph graph; vector dist; vector parent,label; size_t N; T inf; int last_l,last_r,root; private: void solve_impl() { using plli = pair; priority_queue,greater> q; q.push({0,root}); dist[root] = 0; while (q.size()) { auto top = q.top(); q.pop(); size_t curr = top.second; /* 嘘dijkstra if(top.first > dist[curr]) continue; */ for(auto& edge:graph.edges[curr]){ size_t next = edge.first; T w = edge.second; if(parent[curr] == next) continue; if(dist[next] > dist[curr] + w) { dist[next] = dist[curr] + w; parent[next] = curr; label[next] = (curr==root?next:label[curr]); q.push({dist[next],next}); } } } } T solve_cycle() { T mini = inf; last_l=-1,last_r=-1; for(int l=0;l& graph, T inf) : graph(graph),N(graph.size()),dist(graph.size()),parent(graph.size()),label(graph.size()),inf(inf) { } //rootを含む最小閉路の集合を返す O(NlogN) 閉路がないときは空集合 inline T solve(size_t rt){ root = rt; //初期化 for(int i = 0; i < N; ++i) dist[i] = inf, parent[i] = -1; solve_impl(); T mini=solve_cycle(); return mini; } //復元 vector restore() { stack s; queue q; vector res; if(last_l != -1 && last_r != -1){ for(int curr = last_l; curr != -1; curr = parent[curr]) s.push(curr); for(int curr = last_r; curr != root; curr = parent[curr]) q.push(curr); while(s.size()) res.push_back(s.top()) ,s.pop(); while(q.size()) res.push_back(q.front()),q.pop(); } return res; } }; template void chmin(T& a, const T b){a=min(a,b);} int main(void) { int T,N,M; scanf("%d",&T); scanf("%d%d",&N,&M); Graph graph(N); while(M--) { int u,v,w; scanf("%d%d%d",&u,&v,&w); u--,v--; if(T) graph.make_edge(u,v,w); else graph.make_bidirectional_edge(u,v,w); } long long inf = 1e15; long long ans = inf; if(T) { MinimumDirectedClosedCircuit mdcc(graph,inf); for(int i=0;i mucc(graph,inf); for(int i=0;i