結果
問題 | No.1602 With Animals into Institute 2 |
ユーザー | hitonanode |
提出日時 | 2021-07-10 16:19:53 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 829 ms / 4,000 ms |
コード長 | 4,564 bytes |
コンパイル時間 | 1,452 ms |
コンパイル使用メモリ | 113,692 KB |
実行使用メモリ | 40,448 KB |
最終ジャッジ日時 | 2024-07-02 02:35:44 |
合計ジャッジ時間 | 12,352 ms |
ジャッジサーバーID (参考情報) |
judge1 / 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 | 8 ms
5,376 KB |
testcase_04 | AC | 8 ms
5,376 KB |
testcase_05 | AC | 12 ms
5,376 KB |
testcase_06 | AC | 533 ms
36,192 KB |
testcase_07 | AC | 531 ms
36,116 KB |
testcase_08 | AC | 829 ms
40,448 KB |
testcase_09 | AC | 768 ms
39,984 KB |
testcase_10 | AC | 764 ms
40,068 KB |
testcase_11 | AC | 760 ms
40,028 KB |
testcase_12 | AC | 726 ms
40,064 KB |
testcase_13 | AC | 726 ms
40,096 KB |
testcase_14 | AC | 728 ms
40,068 KB |
testcase_15 | AC | 676 ms
40,036 KB |
testcase_16 | AC | 673 ms
39,832 KB |
testcase_17 | AC | 685 ms
40,060 KB |
testcase_18 | AC | 10 ms
5,376 KB |
testcase_19 | AC | 11 ms
5,376 KB |
testcase_20 | AC | 11 ms
5,376 KB |
testcase_21 | AC | 10 ms
5,376 KB |
testcase_22 | AC | 10 ms
5,376 KB |
testcase_23 | AC | 11 ms
5,376 KB |
testcase_24 | AC | 10 ms
5,376 KB |
testcase_25 | AC | 10 ms
5,376 KB |
testcase_26 | AC | 10 ms
5,376 KB |
testcase_27 | AC | 2 ms
5,376 KB |
testcase_28 | AC | 2 ms
5,376 KB |
testcase_29 | AC | 2 ms
5,376 KB |
testcase_30 | AC | 2 ms
5,376 KB |
testcase_31 | AC | 2 ms
5,376 KB |
testcase_32 | AC | 2 ms
5,376 KB |
testcase_33 | AC | 2 ms
5,376 KB |
testcase_34 | AC | 2 ms
5,376 KB |
testcase_35 | AC | 2 ms
5,376 KB |
testcase_36 | AC | 2 ms
5,376 KB |
testcase_37 | AC | 2 ms
5,376 KB |
testcase_38 | AC | 2 ms
5,376 KB |
ソースコード
#include <cassert> #include <queue> #include <tuple> #include <vector> // This implementation is based on: https://gist.github.com/wata-orz/d3037bd0b919c76dd9ddc0379e1e3192 template <class T, T INF, class G, G (*op)(G, G), G (*e)()> struct SSSUP { int V; std::vector<std::vector<std::tuple<int, T, G>>> to; SSSUP(int n) : V(n), to(n) { static_assert(INF > 0, "INF must be positive"); } void add_edge(int u, int v, T len, G g) { assert(u >= 0 and u < V); assert(v >= 0 and v < V); assert(len >= 0); to[u].emplace_back(v, len, g); to[v].emplace_back(u, len, g); } int s; std::vector<T> dist_sp, dist; std::vector<int> parent, depth; std::vector<G> psi; // psi[path = v0v1...vn] = psi[v0v1] * psi[v1v2] * ... * psi[v(n - 1)vn] std::vector<int> uf_ps; int find(int x) { if (uf_ps[x] == -1) { return x; } else { int r = find(uf_ps[x]); return uf_ps[x] = r; } } void unite(int r, int c) { uf_ps[c] = r; } void solve(int s_) { s = s_; assert(s >= 0 and s < V); // Solve SSSP { dist_sp.assign(V, INF); depth.assign(V, -1), parent.assign(V, -1); psi.assign(V, e()); std::priority_queue<std::pair<T, int>, std::vector<std::pair<T, int>>, std::greater<std::pair<T, int>>> que; dist_sp[s] = 0, depth[s] = 0; que.emplace(0, s); while (que.size()) { T d, l; int u, v; G g = e(); std::tie(d, u) = que.top(); que.pop(); if (dist_sp[u] != d) continue; for (const auto &p : to[u]) { std::tie(v, l, g) = p; const auto d2 = d + l; if (dist_sp[v] > d2) { dist_sp[v] = d2, depth[v] = depth[u] + 1, parent[v] = u, psi[v] = op(psi[u], g); que.emplace(d2, v); } } } } // sssup() uf_ps.assign(V, -1); using P = std::tuple<T, int, int>; std::priority_queue<P, std::vector<P>, std::greater<P>> que; for (int u = 0; u < V; u++) { for (int i = 0; i < int(to[u].size()); i++) { int v; T l; G g = e(); std::tie(v, l, g) = to[u][i]; if (u < v and !(op(psi[u], g) == psi[v])) que.emplace(dist_sp[u] + dist_sp[v] + l, u, i); } } dist.assign(V, INF); while (que.size()) { T h; int u0, i; std::tie(h, u0, i) = que.top(); que.pop(); const int v0 = std::get<0>(to[u0][i]); int u = find(u0), v = find(v0); std::vector<int> bs; while (u != v) { if (depth[u] > depth[v]) { bs.push_back(u), u = find(parent[u]); } else { bs.push_back(v), v = find(parent[v]); } } for (const int x : bs) { unite(u, x); dist[x] = h - dist_sp[x]; for (int i = 0; i < int(to[x].size()); i++) { int y; T l; G g = e(); std::tie(y, l, g) = to[x][i]; if (op(psi[x], g) == psi[y]) { que.emplace(dist[x] + dist_sp[y] + l, x, i); } } } } for (int i = 0; i < V; i++) { if (!(psi[i] == e()) and dist_sp[i] < dist[i]) dist[i] = dist_sp[i]; } } }; struct G { unsigned g; G(unsigned x) : g(x) {} bool operator==(const G &x) const { return g == x.g; } }; G op(G x, G y) { return G(x.g ^ y.g); } G e() { return G(0); } #include <iostream> using namespace std; int main() { int N, M, K; cin >> N >> M >> K; constexpr long long INF = 1LL << 60; SSSUP<long long, INF, G, op, e> graph(N); vector<tuple<int, int, int>> edges; while (M--) { int a, b, c; string x; cin >> a >> b >> c >> x; unsigned m = 0; for (auto c : x) m = m * 2 + c - '0'; a--, b--; graph.add_edge(a, b, c, m); graph.add_edge(b, a, c, m); } graph.solve(N - 1); for (int i = 0; i < N - 1; i++) { cout << (graph.dist[i] == INF ? -1 : graph.dist[i]) << '\n'; } }