結果
問題 | No.1320 Two Type Min Cost Cycle |
ユーザー | hitonanode |
提出日時 | 2020-12-17 00:55:01 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 294 ms / 2,000 ms |
コード長 | 5,105 bytes |
コンパイル時間 | 2,773 ms |
コンパイル使用メモリ | 230,880 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-09-20 05:41:30 |
合計ジャッジ時間 | 8,134 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,376 KB |
testcase_02 | AC | 2 ms
5,376 KB |
testcase_03 | AC | 2 ms
5,376 KB |
testcase_04 | AC | 1 ms
5,376 KB |
testcase_05 | AC | 2 ms
5,376 KB |
testcase_06 | AC | 4 ms
5,376 KB |
testcase_07 | AC | 150 ms
5,376 KB |
testcase_08 | AC | 6 ms
5,376 KB |
testcase_09 | AC | 230 ms
5,376 KB |
testcase_10 | AC | 202 ms
5,376 KB |
testcase_11 | AC | 150 ms
5,376 KB |
testcase_12 | AC | 19 ms
5,376 KB |
testcase_13 | AC | 65 ms
5,376 KB |
testcase_14 | AC | 4 ms
5,376 KB |
testcase_15 | AC | 214 ms
5,376 KB |
testcase_16 | AC | 3 ms
5,376 KB |
testcase_17 | AC | 81 ms
5,376 KB |
testcase_18 | AC | 80 ms
5,376 KB |
testcase_19 | AC | 169 ms
5,376 KB |
testcase_20 | AC | 2 ms
5,376 KB |
testcase_21 | AC | 136 ms
5,376 KB |
testcase_22 | AC | 2 ms
5,376 KB |
testcase_23 | AC | 2 ms
5,376 KB |
testcase_24 | AC | 2 ms
5,376 KB |
testcase_25 | AC | 2 ms
5,376 KB |
testcase_26 | AC | 2 ms
5,376 KB |
testcase_27 | AC | 2 ms
5,376 KB |
testcase_28 | AC | 5 ms
5,376 KB |
testcase_29 | AC | 286 ms
5,376 KB |
testcase_30 | AC | 2 ms
5,376 KB |
testcase_31 | AC | 47 ms
5,376 KB |
testcase_32 | AC | 1 ms
5,376 KB |
testcase_33 | AC | 67 ms
5,376 KB |
testcase_34 | AC | 41 ms
5,376 KB |
testcase_35 | AC | 3 ms
5,376 KB |
testcase_36 | AC | 2 ms
5,376 KB |
testcase_37 | AC | 2 ms
5,376 KB |
testcase_38 | AC | 3 ms
5,376 KB |
testcase_39 | AC | 2 ms
5,376 KB |
testcase_40 | AC | 2 ms
5,376 KB |
testcase_41 | AC | 1 ms
5,376 KB |
testcase_42 | AC | 1 ms
5,376 KB |
testcase_43 | AC | 292 ms
5,376 KB |
testcase_44 | AC | 144 ms
5,376 KB |
testcase_45 | AC | 294 ms
5,376 KB |
testcase_46 | AC | 60 ms
5,376 KB |
testcase_47 | AC | 57 ms
5,376 KB |
testcase_48 | AC | 251 ms
5,376 KB |
testcase_49 | AC | 27 ms
5,376 KB |
testcase_50 | AC | 2 ms
5,376 KB |
testcase_51 | AC | 2 ms
5,376 KB |
testcase_52 | AC | 15 ms
5,376 KB |
testcase_53 | AC | 7 ms
5,376 KB |
testcase_54 | AC | 248 ms
5,376 KB |
testcase_55 | AC | 243 ms
5,376 KB |
testcase_56 | AC | 244 ms
5,376 KB |
testcase_57 | AC | 165 ms
5,376 KB |
testcase_58 | AC | 163 ms
5,376 KB |
testcase_59 | AC | 158 ms
5,376 KB |
ソースコード
#include <cassert> #include <limits> #include <queue> #include <utility> #include <vector> // Shortest cycle detection of UNDIRECTED SIMPLE graphs // Verified: <https://yukicoder.me/problems/no/1320> template <typename T> struct ShortestCycleOfUndirectedWeighted { int V, E; std::vector<std::vector<std::pair<int, T>>> to; // (nxt, weight) const T INF; ShortestCycleOfUndirectedWeighted() = default; ShortestCycleOfUndirectedWeighted(int V) : V(V), E(0), to(V), INF(std::numeric_limits<T>::max() / 2) {} void add_edge(int s, int t, T len) { assert(0 <= s and s < V); assert(0 <= t and t < V); assert(len >= 0); to[s].emplace_back(t, len); to[t].emplace_back(s, len); E++; } std::vector<T> dist; std::vector<int> prev; // Find minimum length simple cycle which passes vertex `v` // - Complexity: O(E log V) // - return: (LEN, (a, b)) // - LEN: length of the shortest cycles if exists, INF ( = numeric_limits<int>::max() / 2 ) otherwise. // - the cycle consists of vertices [v, ..., prev[prev[a]], prev[a], a, b, prev[b], prev[prev[b]], ..., v] std::pair<T, std::pair<int, int>> Solve(int v) { assert(0 <= v and v < V); dist.assign(V, INF), dist[v] = 0; prev.assign(V, -1); using P = std::pair<T, std::pair<int, int>>; std::priority_queue<P, std::vector<P>, std::greater<P>> pq; std::vector<std::pair<std::pair<int, int>, T>> add_edge; pq.emplace(0, std::make_pair(v, -1)); while (!pq.empty()) { const int now = pq.top().second.first, prv = pq.top().second.second; pq.pop(); for (const auto &nxt : to[now]) if (nxt.first != prv) { if (dist[nxt.first] == INF) { dist[nxt.first] = dist[now] + nxt.second; prev[nxt.first] = now; pq.emplace(dist[nxt.first], std::make_pair(nxt.first, now)); } else { add_edge.emplace_back(std::make_pair(now, nxt.first), nxt.second); } } } T minimum_cycle = INF; int s = -1, t = -1; for (auto edge : add_edge) { int a = edge.first.first, b = edge.first.second; T L = dist[a] + dist[b] + edge.second; if (L < minimum_cycle) minimum_cycle = L, s = a, t = b; } return std::make_pair(minimum_cycle, std::make_pair(s, t)); } }; #include <bits/stdc++.h> using namespace std; using lint = long long; struct fast_ios { fast_ios(){ cin.tie(nullptr), ios::sync_with_stdio(false), cout << fixed << setprecision(20); }; } fast_ios_; #define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++) #define REP(i, n) FOR(i,0,n) template <typename T> bool chmin(T &m, const T q) { if (m > q) {m = q; return true;} else return false; } template <typename T> struct ShortestPath { int V, E; int INVALID = -1; std::vector<std::vector<std::pair<int, T>>> to; ShortestPath() = default; ShortestPath(int V) : V(V), E(0), to(V) {} void add_edge(int s, int t, T len) { assert(0 <= s and s < V); assert(0 <= t and t < V); to[s].emplace_back(t, len); E++; } std::vector<T> dist; std::vector<int> prev; // Dijkstra algorithm // Complexity: O(E log E) void Dijkstra(int s) { assert(0 <= s and s < V); dist.assign(V, std::numeric_limits<T>::max()); dist[s] = 0; prev.assign(V, INVALID); using P = std::pair<T, int>; std::priority_queue<P, std::vector<P>, std::greater<P>> pq; pq.emplace(0, s); while (!pq.empty()) { T d; int v; std::tie(d, v) = pq.top(); pq.pop(); if (dist[v] < d) continue; for (auto nx : to[v]) { T dnx = d + nx.second; if (dist[nx.first] > dnx) { dist[nx.first] = dnx, prev[nx.first] = v; pq.emplace(dnx, nx.first); } } } } }; int main() { int T, N, M; cin >> T >> N >> M; lint ret = 1e18; if (T == 1) { vector<vector<pair<int, int>>> to(N); while (M--) { int u, v, w; cin >> u >> v >> w; u--, v--; to[u].emplace_back(v, w); } REP(s, N) { ShortestPath<lint> graph(N + 1); REP(i, N) for (auto [j, w] : to[i]) { graph.add_edge(i, j, w); if (j == s) graph.add_edge(i, N, w); } graph.Dijkstra(s); chmin(ret, graph.dist[N]); } } else { ShortestCycleOfUndirectedWeighted<lint> graph(N); while (M--) { int u, v, w; cin >> u >> v >> w; u--, v--; graph.add_edge(u, v, w); } REP(i, N) chmin<lint>(ret, graph.Solve(i).first); } cout << (ret < 1e18 ? ret : -1) << '\n'; }