結果
問題 | No.1320 Two Type Min Cost Cycle |
ユーザー | rniya |
提出日時 | 2024-04-07 20:55:21 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 297 ms / 2,000 ms |
コード長 | 6,617 bytes |
コンパイル時間 | 2,440 ms |
コンパイル使用メモリ | 227,200 KB |
実行使用メモリ | 6,824 KB |
最終ジャッジ日時 | 2024-10-01 04:43:43 |
合計ジャッジ時間 | 6,326 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,816 KB |
testcase_01 | AC | 2 ms
6,820 KB |
testcase_02 | AC | 1 ms
6,816 KB |
testcase_03 | AC | 2 ms
6,820 KB |
testcase_04 | AC | 2 ms
6,816 KB |
testcase_05 | AC | 1 ms
6,816 KB |
testcase_06 | AC | 2 ms
6,820 KB |
testcase_07 | AC | 3 ms
6,824 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 | 162 ms
6,820 KB |
testcase_12 | AC | 27 ms
6,820 KB |
testcase_13 | AC | 86 ms
6,820 KB |
testcase_14 | AC | 9 ms
6,820 KB |
testcase_15 | AC | 4 ms
6,824 KB |
testcase_16 | AC | 3 ms
6,820 KB |
testcase_17 | AC | 3 ms
6,820 KB |
testcase_18 | AC | 3 ms
6,816 KB |
testcase_19 | AC | 47 ms
6,816 KB |
testcase_20 | AC | 4 ms
6,820 KB |
testcase_21 | AC | 142 ms
6,816 KB |
testcase_22 | AC | 2 ms
6,816 KB |
testcase_23 | AC | 2 ms
6,820 KB |
testcase_24 | AC | 1 ms
6,820 KB |
testcase_25 | AC | 2 ms
6,820 KB |
testcase_26 | AC | 2 ms
6,816 KB |
testcase_27 | AC | 1 ms
6,816 KB |
testcase_28 | AC | 8 ms
6,820 KB |
testcase_29 | AC | 76 ms
6,816 KB |
testcase_30 | AC | 2 ms
6,820 KB |
testcase_31 | AC | 14 ms
6,820 KB |
testcase_32 | AC | 2 ms
6,820 KB |
testcase_33 | AC | 117 ms
6,816 KB |
testcase_34 | AC | 64 ms
6,816 KB |
testcase_35 | AC | 2 ms
6,820 KB |
testcase_36 | AC | 2 ms
6,820 KB |
testcase_37 | AC | 1 ms
6,820 KB |
testcase_38 | AC | 3 ms
6,816 KB |
testcase_39 | AC | 2 ms
6,816 KB |
testcase_40 | AC | 2 ms
6,820 KB |
testcase_41 | AC | 1 ms
6,820 KB |
testcase_42 | AC | 1 ms
6,820 KB |
testcase_43 | AC | 4 ms
6,820 KB |
testcase_44 | AC | 3 ms
6,816 KB |
testcase_45 | AC | 297 ms
6,816 KB |
testcase_46 | AC | 7 ms
6,820 KB |
testcase_47 | AC | 66 ms
6,816 KB |
testcase_48 | AC | 59 ms
6,820 KB |
testcase_49 | AC | 8 ms
6,820 KB |
testcase_50 | AC | 2 ms
6,816 KB |
testcase_51 | AC | 1 ms
6,820 KB |
testcase_52 | AC | 17 ms
6,816 KB |
testcase_53 | AC | 9 ms
6,820 KB |
testcase_54 | AC | 77 ms
6,820 KB |
testcase_55 | AC | 78 ms
6,816 KB |
testcase_56 | AC | 77 ms
6,820 KB |
testcase_57 | AC | 170 ms
6,820 KB |
testcase_58 | AC | 171 ms
6,816 KB |
testcase_59 | AC | 173 ms
6,824 KB |
ソースコード
#include <bits/stdc++.h> #ifdef LOCAL #include <debug.hpp> #else #define debug(...) void(0) #endif template <class T> std::istream& operator>>(std::istream& is, std::vector<T>& v) { for (auto& x : v) is >> x; return is; } template <class T> std::ostream& operator<<(std::ostream& os, const std::vector<T>& v) { auto sep = ""; for (const auto& x : v) os << std::exchange(sep, " ") << x; return os; } template <class T, class U = T> bool chmin(T& x, U&& y) { return y < x and (x = std::forward<U>(y), true); } template <class T, class U = T> bool chmax(T& x, U&& y) { return x < y and (x = std::forward<U>(y), true); } template <class T> void mkuni(std::vector<T>& v) { std::sort(begin(v), end(v)); v.erase(unique(begin(v), end(v)), end(v)); } template <class T> int lwb(const std::vector<T>& v, const T& x) { return std::lower_bound(begin(v), end(v), x) - begin(v); } 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 int INF = (1 << 30) - 1; constexpr long long IINF = (1LL << 60) - 1; constexpr int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}; typedef long long ll; #define all(x) begin(x), end(x) using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; int N, M; cin >> N >> M; ll ans = IINF; if (T == 0) { UndirectedShortestCycle<long long> G(N); for (; M--;) { int u, v, w; cin >> u >> v >> w; G.add_edge(--u, --v, w); } for (int i = 0; i < N; i++) ans = min(ans, get<0>(G.solve(i))); } else { DirectedShortestCycle<long long> G(N); for (; M--;) { int u, v, w; cin >> u >> v >> w; G.add_edge(--u, --v, w); } for (int i = 0; i < N; i++) ans = min(ans, get<0>(G.solve(i))); } cout << (ans == IINF ? -1 : ans) << '\n'; return 0; }