結果
問題 | No.1320 Two Type Min Cost Cycle |
ユーザー | 👑 emthrm |
提出日時 | 2020-12-19 05:05:25 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 6,623 bytes |
コンパイル時間 | 3,126 ms |
コンパイル使用メモリ | 237,136 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-09-21 10:01:26 |
合計ジャッジ時間 | 14,962 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
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 | 2 ms
5,376 KB |
testcase_05 | AC | 7 ms
5,376 KB |
testcase_06 | AC | 2 ms
5,376 KB |
testcase_07 | AC | 3 ms
5,376 KB |
testcase_08 | AC | 90 ms
5,376 KB |
testcase_09 | AC | 1,009 ms
5,376 KB |
testcase_10 | AC | 3 ms
5,376 KB |
testcase_11 | AC | 897 ms
5,376 KB |
testcase_12 | AC | 182 ms
5,376 KB |
testcase_13 | AC | 427 ms
5,376 KB |
testcase_14 | AC | 84 ms
5,376 KB |
testcase_15 | AC | 3 ms
5,376 KB |
testcase_16 | AC | 15 ms
5,376 KB |
testcase_17 | AC | 2 ms
5,376 KB |
testcase_18 | AC | 3 ms
5,376 KB |
testcase_19 | AC | 38 ms
5,376 KB |
testcase_20 | AC | 23 ms
5,376 KB |
testcase_21 | AC | 563 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 | 72 ms
5,376 KB |
testcase_29 | AC | 39 ms
5,376 KB |
testcase_30 | AC | 2 ms
5,376 KB |
testcase_31 | AC | 8 ms
5,376 KB |
testcase_32 | AC | 2 ms
5,376 KB |
testcase_33 | AC | 906 ms
5,376 KB |
testcase_34 | AC | 394 ms
5,376 KB |
testcase_35 | AC | 3 ms
5,376 KB |
testcase_36 | AC | 4 ms
5,376 KB |
testcase_37 | AC | 3 ms
5,376 KB |
testcase_38 | WA | - |
testcase_39 | AC | 4 ms
5,376 KB |
testcase_40 | AC | 2 ms
5,376 KB |
testcase_41 | AC | 2 ms
5,376 KB |
testcase_42 | AC | 2 ms
5,376 KB |
testcase_43 | AC | 3 ms
5,376 KB |
testcase_44 | AC | 2 ms
5,376 KB |
testcase_45 | AC | 1,675 ms
5,376 KB |
testcase_46 | AC | 6 ms
5,376 KB |
testcase_47 | AC | 623 ms
5,376 KB |
testcase_48 | AC | 63 ms
5,376 KB |
testcase_49 | AC | 6 ms
5,376 KB |
testcase_50 | AC | 2 ms
5,376 KB |
testcase_51 | AC | 2 ms
5,376 KB |
testcase_52 | AC | 105 ms
5,376 KB |
testcase_53 | AC | 61 ms
5,376 KB |
testcase_54 | AC | 69 ms
5,376 KB |
testcase_55 | AC | 69 ms
5,376 KB |
testcase_56 | AC | 69 ms
5,376 KB |
testcase_57 | AC | 935 ms
5,376 KB |
testcase_58 | AC | 928 ms
5,376 KB |
testcase_59 | AC | 942 ms
5,376 KB |
ソースコード
#define _USE_MATH_DEFINES #include <bits/stdc++.h> using namespace std; #define FOR(i,m,n) for(int i=(m);i<(n);++i) #define REP(i,n) FOR(i,0,n) #define ALL(v) (v).begin(),(v).end() using ll = long long; constexpr int INF = 0x3f3f3f3f; constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL; constexpr double EPS = 1e-8; constexpr int MOD = 1000000007; // constexpr int MOD = 998244353; constexpr int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1}; constexpr int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1}; template <typename T, typename U> inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; } template <typename T, typename U> inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; } struct IOSetup { IOSetup() { std::cin.tie(nullptr); std::ios_base::sync_with_stdio(false); std::cout << fixed << setprecision(20); } } iosetup; template <typename CostType> struct Edge { int src, dst; CostType cost; Edge(int src, int dst, CostType cost = 0) : src(src), dst(dst), cost(cost) {} inline bool operator<(const Edge &x) const { return cost != x.cost ? cost < x.cost : dst != x.dst ? dst < x.dst : src < x.src; } inline bool operator<=(const Edge &x) const { return !(x < *this); } inline bool operator>(const Edge &x) const { return x < *this; } inline bool operator>=(const Edge &x) const { return !(*this < x); } }; template <typename CostType> CostType girth_in_directed_graph(const std::vector<std::vector<Edge<CostType>>> &graph) { int n = graph.size(); CostType res = -1; std::vector<CostType> dist(n); std::priority_queue<Edge<CostType>, std::vector<Edge<CostType>>, std::greater<Edge<CostType>>> que; for (int root = 0; root < n; ++root) { std::fill(dist.begin(), dist.end(), -1); dist[root] = 0; for (const Edge<CostType> &e : graph[root]) { if (e.dst == root) { if (res == -1 || e.cost < res) res = e.cost; } else { que.emplace(e); } } while (!que.empty()) { Edge<CostType> edge = que.top(); que.pop(); if (dist[edge.dst] != -1) { if (edge.dst == root && (res == -1 || dist[edge.src] + edge.cost < res)) res = dist[edge.src] + edge.cost; continue; } dist[edge.dst] = dist[edge.src] + edge.cost; for (const Edge<CostType> &e : graph[edge.dst]) { if (dist[e.dst] != -1) { if (e.dst == root && (res == -1 || dist[edge.dst] + e.cost < res)) res = dist[edge.dst] + e.cost; } else { que.emplace(e); } } } } return res; } template <typename CostType> struct LCADoubling { std::vector<int> depth; std::vector<CostType> dist; LCADoubling(const std::vector<std::vector<Edge<CostType>>> &graph) : graph(graph) { n = graph.size(); depth.resize(n); dist.resize(n); while ((1 << table_h) <= n) ++table_h; parent.resize(table_h, std::vector<int>(n)); } void build(int root = 0) { is_built = true; dfs(-1, root, 0, 0); for (int i = 0; i + 1 < table_h; ++i) for (int ver = 0; ver < n; ++ver) { parent[i + 1][ver] = parent[i][ver] == -1 ? -1 : parent[i][parent[i][ver]]; } } int query(int u, int v) const { assert(is_built); if (depth[u] > depth[v]) std::swap(u, v); for (int i = 0; i < table_h; ++i) { if ((depth[v] - depth[u]) >> i & 1) v = parent[i][v]; } if (u == v) return u; for (int i = table_h - 1; i >= 0; --i) { if (parent[i][u] != parent[i][v]) { u = parent[i][u]; v = parent[i][v]; } } return parent[0][u]; } CostType distance(int u, int v) const { assert(is_built); return dist[u] + dist[v] - dist[query(u, v)] * 2; } private: bool is_built = false; int n, table_h = 1; std::vector<std::vector<Edge<CostType>>> graph; std::vector<std::vector<int>> parent; void dfs(int par, int ver, int now_depth, CostType now_dist) { depth[ver] = now_depth; dist[ver] = now_dist; parent[0][ver] = par; for (const Edge<CostType> &e : graph[ver]) { if (e.dst != par) dfs(ver, e.dst, now_depth + 1, now_dist + e.cost); } } }; template <typename CostType> CostType girth_in_undirected_graph(int n, const std::vector<Edge<CostType>> &edges) { int m = edges.size(); std::vector<std::vector<int>> graph(n); for (int i = 0; i < m; ++i) { graph[edges[i].src].emplace_back(i); graph[edges[i].dst].emplace_back(i); } std::vector<bool> used(m, false), visited(n, false); using P = std::pair<int, int>; std::priority_queue<P, std::vector<P>, std::function<bool(const P&, const P&)>> que( [&](const P &a, const P &b) { const Edge<CostType> &a_edge = edges[a.first], &b_edge = edges[b.first]; return a_edge.cost != b_edge.cost ? a_edge.cost > b_edge.cost : a_edge.dst != b_edge.dst ? a_edge.dst > b_edge.dst : a_edge.src > b_edge.src; } ); CostType res = -1; for (int root = 0; root < n; ++root) { std::fill(used.begin(), used.end(), false); std::fill(visited.begin(), visited.end(), false); visited[root] = true; for (int id : graph[root]) { int dst = edges[id].src == root ? edges[id].dst : edges[id].src; if (dst != root) que.emplace(id, dst); } std::vector<std::vector<Edge<CostType>>> tree(n); while (!que.empty()) { int id, dst; std::tie(id, dst) = que.top(); que.pop(); if (visited[dst]) continue; int src = edges[id].dst == dst ? edges[id].src : edges[id].dst; used[id] = visited[dst] = true; tree[src].emplace_back(src, dst, edges[id].cost); tree[dst].emplace_back(dst, src, edges[id].cost); for (int e : graph[dst]) { int nx = edges[e].src == dst ? edges[e].dst : edges[e].src; if (!visited[nx]) que.emplace(e, nx); } } LCADoubling<CostType> lca(tree); lca.build(root); for (int i = 0; i < m; ++i) { if (!used[i] && visited[edges[i].src] && visited[edges[i].dst]) { CostType loop = lca.distance(edges[i].src, edges[i].dst) + edges[i].cost; if (res == -1 || loop < res) res = loop; } } } return res; } int main() { int t, n, m; cin >> t >> n >> m; if (t == 0) { vector<Edge<ll>> edges; while (m--) { int u, v, w; cin >> u >> v >> w; --u; --v; edges.emplace_back(u, v, w); } cout << girth_in_undirected_graph(n, edges) << '\n'; } else if (t == 1) { vector<vector<Edge<ll>>> graph(n); while (m--) { int u, v, w; cin >> u >> v >> w; --u; --v; graph[u].emplace_back(u, v, w); } cout << girth_in_directed_graph(graph) << '\n'; } return 0; }