結果
問題 | No.1320 Two Type Min Cost Cycle |
ユーザー | rniya |
提出日時 | 2024-04-07 20:59:57 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 297 ms / 2,000 ms |
コード長 | 5,786 bytes |
コンパイル時間 | 1,259 ms |
コンパイル使用メモリ | 106,420 KB |
実行使用メモリ | 6,820 KB |
最終ジャッジ日時 | 2024-10-01 04:44:09 |
合計ジャッジ時間 | 4,771 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,820 KB |
testcase_01 | AC | 1 ms
6,820 KB |
testcase_02 | AC | 1 ms
6,816 KB |
testcase_03 | AC | 2 ms
6,816 KB |
testcase_04 | AC | 1 ms
6,816 KB |
testcase_05 | AC | 2 ms
6,816 KB |
testcase_06 | AC | 2 ms
6,816 KB |
testcase_07 | AC | 4 ms
6,820 KB |
testcase_08 | AC | 9 ms
6,820 KB |
testcase_09 | AC | 235 ms
6,820 KB |
testcase_10 | AC | 4 ms
6,820 KB |
testcase_11 | AC | 166 ms
6,816 KB |
testcase_12 | AC | 28 ms
6,820 KB |
testcase_13 | AC | 82 ms
6,820 KB |
testcase_14 | AC | 7 ms
6,816 KB |
testcase_15 | AC | 4 ms
6,816 KB |
testcase_16 | AC | 3 ms
6,820 KB |
testcase_17 | AC | 2 ms
6,820 KB |
testcase_18 | AC | 3 ms
6,816 KB |
testcase_19 | AC | 39 ms
6,816 KB |
testcase_20 | AC | 3 ms
6,820 KB |
testcase_21 | AC | 141 ms
6,816 KB |
testcase_22 | AC | 2 ms
6,816 KB |
testcase_23 | AC | 2 ms
6,816 KB |
testcase_24 | AC | 1 ms
6,816 KB |
testcase_25 | AC | 1 ms
6,820 KB |
testcase_26 | AC | 1 ms
6,820 KB |
testcase_27 | AC | 1 ms
6,820 KB |
testcase_28 | AC | 7 ms
6,820 KB |
testcase_29 | AC | 66 ms
6,820 KB |
testcase_30 | AC | 2 ms
6,816 KB |
testcase_31 | AC | 13 ms
6,816 KB |
testcase_32 | AC | 2 ms
6,816 KB |
testcase_33 | AC | 113 ms
6,816 KB |
testcase_34 | AC | 59 ms
6,816 KB |
testcase_35 | AC | 2 ms
6,820 KB |
testcase_36 | AC | 2 ms
6,816 KB |
testcase_37 | AC | 1 ms
6,816 KB |
testcase_38 | AC | 2 ms
6,816 KB |
testcase_39 | AC | 2 ms
6,820 KB |
testcase_40 | AC | 2 ms
6,820 KB |
testcase_41 | AC | 2 ms
6,820 KB |
testcase_42 | AC | 2 ms
6,820 KB |
testcase_43 | AC | 4 ms
6,816 KB |
testcase_44 | AC | 3 ms
6,820 KB |
testcase_45 | AC | 297 ms
6,820 KB |
testcase_46 | AC | 7 ms
6,820 KB |
testcase_47 | AC | 66 ms
6,820 KB |
testcase_48 | AC | 59 ms
6,820 KB |
testcase_49 | AC | 8 ms
6,820 KB |
testcase_50 | AC | 2 ms
6,820 KB |
testcase_51 | AC | 2 ms
6,816 KB |
testcase_52 | AC | 16 ms
6,816 KB |
testcase_53 | AC | 9 ms
6,816 KB |
testcase_54 | AC | 79 ms
6,820 KB |
testcase_55 | AC | 78 ms
6,820 KB |
testcase_56 | AC | 77 ms
6,820 KB |
testcase_57 | AC | 170 ms
6,820 KB |
testcase_58 | AC | 174 ms
6,820 KB |
testcase_59 | AC | 171 ms
6,820 KB |
ソースコード
#define PROBLEM "https://yukicoder.me/problems/no/1320" #include <iostream> #include <algorithm> #include <cassert> #include <limits> #include <queue> #include <tuple> #include <vector> template <typename T> struct DirectedShortestCycle { struct edge { int from, to; T cost; edge(int from, int to, T cost) : from(from), to(to), cost(cost) {} }; std::vector<std::vector<int>> G; std::vector<edge> edges; DirectedShortestCycle(int n) : n(n), G(n) {} void add_edge(int u, int v, T w) { assert(0 <= u and u < n); assert(0 <= v and v < n); assert(w >= 0); G[u].emplace_back(edges.size()); edges.emplace_back(u, v, w); } std::tuple<T, std::vector<int>, std::vector<int>> solve(int r) { std::vector<int> prve(n, -1); std::vector<T> dist(n, inf); std::priority_queue<std::pair<T, int>, std::vector<std::pair<T, int>>, std::greater<>> pq; for (const int& idx : G[r]) { int u = edges[idx].to, w = edges[idx].cost; if (w < dist[u]) { dist[u] = w; prve[u] = idx; pq.emplace(dist[u], u); } } while (not pq.empty()) { auto [d, v] = pq.top(); pq.pop(); if (dist[v] < d) continue; for (const int& idx : G[v]) { int u = edges[idx].to, w = edges[idx].cost; if (dist[u] <= dist[v] + w) continue; dist[u] = dist[v] + w; prve[u] = idx; pq.emplace(dist[u], u); } } if (dist[r] == inf) return {inf, {}, {}}; std::vector<int> vs, es; vs.emplace_back(r); while (true) { int idx = prve[vs.back()]; es.emplace_back(idx); vs.emplace_back(edges[idx].from); if (vs.back() == r) break; } std::reverse(vs.begin(), vs.end()); std::reverse(es.begin(), es.end()); vs.pop_back(); return {dist[r], vs, es}; } private: constexpr static T inf = std::numeric_limits<T>::max() / 2; int n; }; template <typename T> struct UndirectedShortestCycle { struct edge { int from, to; T cost; edge(int from, int to, T cost) : from(from), to(to), cost(cost) {} }; std::vector<std::vector<int>> G; std::vector<edge> edges; UndirectedShortestCycle(int n) : n(n), G(n) {} void add_edge(int u, int v, T w) { assert(0 <= u and u < n); assert(0 <= v and v < n); assert(w >= 0); G[u].emplace_back(edges.size()); G[v].emplace_back(edges.size()); edges.emplace_back(u, v, w); } std::tuple<T, std::vector<int>, std::vector<int>> solve(int r) { std::vector<int> label(n, -1), prve(n, -1); std::vector<T> dist(n, inf); std::priority_queue<std::pair<T, int>, std::vector<std::pair<T, int>>, std::greater<>> pq; dist[r] = 0; pq.emplace(dist[r], r); label[r] = r; while (not pq.empty()) { auto [d, v] = pq.top(); pq.pop(); if (dist[v] < d) continue; for (const int& idx : G[v]) { int u = edges[idx].from ^ edges[idx].to ^ v, w = edges[idx].cost; if (dist[u] <= dist[v] + w) continue; dist[u] = dist[v] + w; label[u] = label[v] == r ? u : label[v]; prve[u] = idx; pq.emplace(dist[u], u); } } std::vector<bool> used(edges.size(), false); for (int& idx : prve) { if (idx != -1) { used[idx] = true; } } T mini = inf; int argmin = -1; for (int i = 0; i < int(edges.size()); i++) { if (used[i]) continue; auto [u, v, w] = edges[i]; if (label[u] == -1) continue; if (label[u] == label[v]) continue; if (dist[u] + dist[v] + w < mini) { mini = dist[u] + dist[v] + w; argmin = i; } } if (argmin == -1) return {mini, {}, {}}; std::vector<int> vs, es; int a = edges[argmin].from, b = edges[argmin].to; while (a != r) { int idx = prve[a]; vs.emplace_back(a); es.emplace_back(idx); a = edges[idx].from ^ edges[idx].to ^ a; } vs.emplace_back(a); std::reverse(vs.begin(), vs.end()); std::reverse(es.begin(), es.end()); es.emplace_back(argmin); while (b != r) { int idx = prve[b]; vs.emplace_back(b); es.emplace_back(idx); b = edges[idx].from ^ edges[idx].to ^ b; } return {mini, vs, es}; } private: constexpr static T inf = std::numeric_limits<T>::max() / 2; int n; }; constexpr long long inf = (1LL << 60) - 1; int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int T; std::cin >> T; int N, M; std::cin >> N >> M; long long ans = inf; if (T == 0) { UndirectedShortestCycle<long long> G(N); for (; M--;) { int u, v, w; std::cin >> u >> v >> w; G.add_edge(--u, --v, w); } for (int i = 0; i < N; i++) ans = std::min(ans, std::get<0>(G.solve(i))); } else { DirectedShortestCycle<long long> G(N); for (; M--;) { int u, v, w; std::cin >> u >> v >> w; G.add_edge(--u, --v, w); } for (int i = 0; i < N; i++) ans = std::min(ans, std::get<0>(G.solve(i))); } std::cout << (ans == inf ? -1 : ans) << '\n'; return 0; }