結果
問題 | No.1442 I-wate Shortest Path Problem |
ユーザー | ei1333333 |
提出日時 | 2021-03-26 22:20:56 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 397 ms / 3,000 ms |
コード長 | 8,982 bytes |
コンパイル時間 | 2,690 ms |
コンパイル使用メモリ | 233,736 KB |
実行使用メモリ | 27,796 KB |
最終ジャッジ日時 | 2024-05-06 16:02:11 |
合計ジャッジ時間 | 9,143 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 1 ms
5,376 KB |
testcase_02 | AC | 6 ms
5,376 KB |
testcase_03 | AC | 35 ms
5,376 KB |
testcase_04 | AC | 6 ms
5,376 KB |
testcase_05 | AC | 4 ms
5,376 KB |
testcase_06 | AC | 30 ms
5,376 KB |
testcase_07 | AC | 4 ms
5,376 KB |
testcase_08 | AC | 30 ms
5,376 KB |
testcase_09 | AC | 9 ms
5,376 KB |
testcase_10 | AC | 34 ms
5,376 KB |
testcase_11 | AC | 32 ms
5,376 KB |
testcase_12 | AC | 226 ms
21,400 KB |
testcase_13 | AC | 103 ms
15,104 KB |
testcase_14 | AC | 159 ms
19,088 KB |
testcase_15 | AC | 156 ms
19,012 KB |
testcase_16 | AC | 219 ms
21,460 KB |
testcase_17 | AC | 380 ms
25,596 KB |
testcase_18 | AC | 361 ms
25,636 KB |
testcase_19 | AC | 239 ms
21,844 KB |
testcase_20 | AC | 360 ms
25,636 KB |
testcase_21 | AC | 397 ms
25,632 KB |
testcase_22 | AC | 105 ms
21,120 KB |
testcase_23 | AC | 226 ms
27,796 KB |
testcase_24 | AC | 77 ms
14,604 KB |
testcase_25 | AC | 193 ms
21,972 KB |
testcase_26 | AC | 90 ms
20,036 KB |
ソースコード
#include<bits/stdc++.h> using namespace std; using int64 = long long; // const int mod = 1e9 + 7; const int mod = 998244353; const int64 infll = (1LL << 62) - 1; const int inf = (1 << 30) - 1; struct IoSetup { IoSetup() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(10); cerr << fixed << setprecision(10); } } iosetup; template< typename T1, typename T2 > ostream &operator<<(ostream &os, const pair< T1, T2 > &p) { os << p.first << " " << p.second; return os; } template< typename T1, typename T2 > istream &operator>>(istream &is, pair< T1, T2 > &p) { is >> p.first >> p.second; return is; } template< typename T > ostream &operator<<(ostream &os, const vector< T > &v) { for(int i = 0; i < (int) v.size(); i++) { os << v[i] << (i + 1 != v.size() ? " " : ""); } return os; } template< typename T > istream &operator>>(istream &is, vector< T > &v) { for(T &in : v) is >> in; return is; } template< typename T1, typename T2 > inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); } template< typename T1, typename T2 > inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); } template< typename T = int64 > vector< T > make_v(size_t a) { return vector< T >(a); } template< typename T, typename... Ts > auto make_v(size_t a, Ts... ts) { return vector< decltype(make_v< T >(ts...)) >(a, make_v< T >(ts...)); } template< typename T, typename V > typename enable_if< is_class< T >::value == 0 >::type fill_v(T &t, const V &v) { t = v; } template< typename T, typename V > typename enable_if< is_class< T >::value != 0 >::type fill_v(T &t, const V &v) { for(auto &e : t) fill_v(e, v); } template< typename F > struct FixPoint : F { FixPoint(F &&f) : F(forward< F >(f)) {} template< typename... Args > decltype(auto) operator()(Args &&... args) const { return F::operator()(*this, forward< Args >(args)...); } }; template< typename F > inline decltype(auto) MFP(F &&f) { return FixPoint< F >{forward< F >(f)}; } template< typename T = int > struct Edge { int from, to; T cost; int idx; Edge() = default; Edge(int from, int to, T cost = 1, int idx = -1) : from(from), to(to), cost(cost), idx(idx) {} operator int() const { return to; } }; template< typename T = int > struct Graph { vector< vector< Edge< T > > > g; int es; Graph() = default; explicit Graph(int n) : g(n), es(0) {} size_t size() const { return g.size(); } void add_directed_edge(int from, int to, T cost = 1) { g[from].emplace_back(from, to, cost, es++); } void add_edge(int from, int to, T cost = 1) { g[from].emplace_back(from, to, cost, es); g[to].emplace_back(to, from, cost, es++); } void read(int M, int padding = -1, bool weighted = false, bool directed = false) { for(int i = 0; i < M; i++) { int a, b; cin >> a >> b; a += padding; b += padding; T c = T(1); if(weighted) cin >> c; if(directed) add_directed_edge(a, b, c); else add_edge(a, b, c); } } }; template< typename T = int > using Edges = vector< Edge< T > >; /** * @brief Heavy-Light-Decomposition(HL分解) * @see https://smijake3.hatenablog.com/entry/2019/09/15/200200 */ template< typename T = int > struct HeavyLightDecomposition : Graph< T > { public: using Graph< T >::Graph; using Graph< T >::g; vector< int > sz, in, out, head, rev, par, dep; void build() { sz.assign(g.size(), 0); in.assign(g.size(), 0); out.assign(g.size(), 0); head.assign(g.size(), 0); rev.assign(g.size(), 0); par.assign(g.size(), 0); dep.assign(g.size(), 0); dfs_sz(0, -1, 0); int t = 0; dfs_hld(0, -1, t); } /* k: 0-indexed */ int la(int v, int k) { while(1) { int u = head[v]; if(in[v] - k >= in[u]) return rev[in[v] - k]; k -= in[v] - in[u] + 1; v = par[u]; } } int lca(int u, int v) const { for(;; v = par[head[v]]) { if(in[u] > in[v]) swap(u, v); if(head[u] == head[v]) return u; } } int dist(int u, int v) const { return dep[u] + dep[v] - 2 * dep[lca(u, v)]; } template< typename E, typename Q, typename F, typename S > E query(int u, int v, const E &ti, const Q &q, const F &f, const S &s, bool edge = false) { E l = ti, r = ti; for(;; v = par[head[v]]) { if(in[u] > in[v]) swap(u, v), swap(l, r); if(head[u] == head[v]) break; l = f(q(in[head[v]], in[v] + 1), l); } return s(f(q(in[u] + edge, in[v] + 1), l), r); } template< typename E, typename Q, typename F > E query(int u, int v, const E &ti, const Q &q, const F &f, bool edge = false) { return query(u, v, ti, q, f, f, edge); } template< typename Q > void add(int u, int v, const Q &q, bool edge = false) { for(;; v = par[head[v]]) { if(in[u] > in[v]) swap(u, v); if(head[u] == head[v]) break; q(in[head[v]], in[v] + 1); } q(in[u] + edge, in[v] + 1); } /* {parent, child} */ vector< pair< int, int > > compress(vector< int > &remark) { auto cmp = [&](int a, int b) { return in[a] < in[b]; }; sort(begin(remark), end(remark), cmp); remark.erase(unique(begin(remark), end(remark)), end(remark)); int K = (int) remark.size(); for(int k = 1; k < K; k++) remark.emplace_back(lca(remark[k - 1], remark[k])); sort(begin(remark), end(remark), cmp); remark.erase(unique(begin(remark), end(remark)), end(remark)); vector< pair< int, int > > es; stack< int > st; for(auto &k : remark) { while(!st.empty() && out[st.top()] <= in[k]) st.pop(); if(!st.empty()) es.emplace_back(st.top(), k); st.emplace(k); } return es; } explicit HeavyLightDecomposition(const Graph< T > &g) : Graph< T >(g) {} private: void dfs_sz(int idx, int p, int d) { dep[idx] = d; par[idx] = p; sz[idx] = 1; if(g[idx].size() && g[idx][0] == p) swap(g[idx][0], g[idx].back()); for(auto &to : g[idx]) { if(to == p) continue; dfs_sz(to, idx, d + 1); sz[idx] += sz[to]; if(sz[g[idx][0]] < sz[to]) swap(g[idx][0], to); } } void dfs_hld(int idx, int p, int ×) { in[idx] = times++; rev[in[idx]] = idx; for(auto &to : g[idx]) { if(to == p) continue; head[to] = (g[idx][0] == to ? head[idx] : to); dfs_hld(to, idx, times); } out[idx] = times; } }; /** * @brief Dijkstra(単一始点最短路) * @docs docs/dijkstra.md */ template< typename T > struct ShortestPath { vector< T > dist; vector< int > from, id; }; template< typename T > ShortestPath< T > dijkstra(const Graph< T > &g, vector< int > &ss) { const auto INF = numeric_limits< T >::max(); vector< T > dist(g.size(), INF); vector< int > from(g.size(), -1), id(g.size(), -1); using Pi = pair< T, int >; priority_queue< Pi, vector< Pi >, greater<> > que; for(auto &s : ss) { dist[s] = 0; que.emplace(dist[s], s); } while(!que.empty()) { T cost; int idx; tie(cost, idx) = que.top(); que.pop(); if(dist[idx] < cost) continue; for(auto &e : g.g[idx]) { auto next_cost = cost + e.cost; if(dist[e.to] <= next_cost) continue; dist[e.to] = next_cost; from[e.to] = idx; id[e.to] = e.idx; que.emplace(dist[e.to], e.to); } } return {dist, from, id}; } int main() { int N, K; cin >> N >> K; HeavyLightDecomposition< int64 > g(N); g.read(N - 1, -1, true); g.build(); vector< int > P(K); vector< vector< int > > xs; for(int i = 0; i < K; i++) { int M; cin >> M >> P[i]; vector< int > X(M); cin >> X; for(auto &x : X) --x; xs.emplace_back(X); } vector< vector< int64 > > mat(K); for(int i = 0; i < K; i++) { mat[i] = dijkstra(g, xs[i]).dist; } vector< int64 > dep(N); MFP([&](auto rec, int idx, int par, int64 d) -> void { dep[idx] = d; for(auto &to : g.g[idx]) { if(to != par) { rec(to, idx, d + to.cost); } } })(0, -1, 0); auto mat2 = make_v< int64 >(K * 2, K * 2); fill_v(mat2, infll); for(int i = 0; i < K * 2; i++) { mat2[i][i] = 0; } auto in = [&](int idx) { return idx; }; auto out = [&](int idx) { return idx + K; }; for(int i = 0; i < K; i++) { mat2[in(i)][out(i)] = P[i]; } for(int i = 0; i < K; i++) { for(int j = 0; j < K; j++) { for(auto &p : xs[j]) { chmin(mat2[out(j)][in(i)], mat[i][p]); } } } for(int k = 0; k < K * 2; k++) { for(int i = 0; i < K * 2; i++) { for(int j = 0; j < K * 2; j++) { chmin(mat2[i][j], mat2[i][k] + mat2[k][j]); } } } int Q; cin >> Q; for(int i = 0; i < Q; i++) { int u, v; cin >> u >> v; --u, --v; int64 ret = dep[u] + dep[v] - 2 * dep[g.lca(u, v)]; for(int j = 0; j < K; j++) { for(int k = 0; k < K; k++) { chmin(ret, mat[j][u] + mat2[in(j)][out(k)] + mat[k][v]); } } cout << ret << "\n"; } }