結果
問題 | No.1308 ジャンプビーコン |
ユーザー | roaris |
提出日時 | 2020-12-05 04:51:34 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
TLE
|
実行時間 | - |
コード長 | 6,566 bytes |
コンパイル時間 | 2,621 ms |
コンパイル使用メモリ | 220,076 KB |
実行使用メモリ | 150,272 KB |
最終ジャッジ日時 | 2024-09-15 10:25:02 |
合計ジャッジ時間 | 31,861 ms |
ジャッジサーバーID (参考情報) |
judge6 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 51 ms
79,744 KB |
testcase_01 | AC | 49 ms
74,368 KB |
testcase_02 | AC | 49 ms
74,240 KB |
testcase_03 | AC | 49 ms
74,368 KB |
testcase_04 | AC | 50 ms
74,368 KB |
testcase_05 | AC | 50 ms
74,368 KB |
testcase_06 | AC | 50 ms
74,368 KB |
testcase_07 | AC | 51 ms
74,368 KB |
testcase_08 | AC | 57 ms
74,240 KB |
testcase_09 | AC | 59 ms
74,368 KB |
testcase_10 | AC | 58 ms
74,240 KB |
testcase_11 | AC | 58 ms
74,368 KB |
testcase_12 | AC | 60 ms
74,240 KB |
testcase_13 | TLE | - |
testcase_14 | TLE | - |
testcase_15 | TLE | - |
testcase_16 | TLE | - |
testcase_17 | TLE | - |
testcase_18 | TLE | - |
testcase_19 | -- | - |
testcase_20 | -- | - |
testcase_21 | -- | - |
testcase_22 | -- | - |
testcase_23 | -- | - |
testcase_24 | -- | - |
testcase_25 | -- | - |
testcase_26 | -- | - |
testcase_27 | -- | - |
testcase_28 | -- | - |
testcase_29 | -- | - |
testcase_30 | -- | - |
testcase_31 | -- | - |
testcase_32 | -- | - |
testcase_33 | -- | - |
testcase_34 | -- | - |
testcase_35 | -- | - |
testcase_36 | -- | - |
testcase_37 | -- | - |
testcase_38 | -- | - |
testcase_39 | -- | - |
ソースコード
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i=0; i<n; i++) #define pb push_back typedef long long ll; typedef pair<int, ll> P1; typedef pair<int, int> P2; ll N, Q, C; vector<P1> G[3010]; vector<vector<ll>> dists; int x[3010]; ll dp[3010][3010]; vector<ll> bfs(int s) { queue<int> q; q.push(s); vector<ll> dist(N); rep(i, N) dist[i] = -1; dist[s] = 0; while (q.size()) { int v = q.front(); q.pop(); rep(i, G[v].size()) { P1 p = G[v][i]; int nv = p.first; ll w = p.second; if (dist[nv]==-1) { dist[nv] = dist[v]+w; q.push(nv); } } } return dist; } int ceil_pow2(int n) { int x = 0; while ((1U << x) < (unsigned int)(n)) x++; return x; } template <class S, S (*op)(S, S), S (*e)(), class F, S (*mapping)(F, S), F (*composition)(F, F), F (*id)()> struct lazy_segtree { public: lazy_segtree() : lazy_segtree(0) {} lazy_segtree(int n) : lazy_segtree(std::vector<S>(n, e())) {} lazy_segtree(const std::vector<S>& v) : _n(int(v.size())) { log = ceil_pow2(_n); size = 1 << log; d = std::vector<S>(2 * size, e()); lz = std::vector<F>(size, id()); for (int i = 0; i < _n; i++) d[size + i] = v[i]; for (int i = size - 1; i >= 1; i--) { update(i); } } void set(int p, S x) { assert(0 <= p && p < _n); p += size; for (int i = log; i >= 1; i--) push(p >> i); d[p] = x; for (int i = 1; i <= log; i++) update(p >> i); } S get(int p) { assert(0 <= p && p < _n); p += size; for (int i = log; i >= 1; i--) push(p >> i); return d[p]; } S prod(int l, int r) { assert(0 <= l && l <= r && r <= _n); if (l == r) return e(); l += size; r += size; for (int i = log; i >= 1; i--) { if (((l >> i) << i) != l) push(l >> i); if (((r >> i) << i) != r) push(r >> i); } S sml = e(), smr = e(); while (l < r) { if (l & 1) sml = op(sml, d[l++]); if (r & 1) smr = op(d[--r], smr); l >>= 1; r >>= 1; } return op(sml, smr); } S all_prod() { return d[1]; } void apply(int p, F f) { assert(0 <= p && p < _n); p += size; for (int i = log; i >= 1; i--) push(p >> i); d[p] = mapping(f, d[p]); for (int i = 1; i <= log; i++) update(p >> i); } void apply(int l, int r, F f) { assert(0 <= l && l <= r && r <= _n); if (l == r) return; l += size; r += size; for (int i = log; i >= 1; i--) { if (((l >> i) << i) != l) push(l >> i); if (((r >> i) << i) != r) push((r - 1) >> i); } { int l2 = l, r2 = r; while (l < r) { if (l & 1) all_apply(l++, f); if (r & 1) all_apply(--r, f); l >>= 1; r >>= 1; } l = l2; r = r2; } for (int i = 1; i <= log; i++) { if (((l >> i) << i) != l) update(l >> i); if (((r >> i) << i) != r) update((r - 1) >> i); } } private: int _n, size, log; std::vector<S> d; std::vector<F> lz; void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); } void all_apply(int k, F f) { d[k] = mapping(f, d[k]); if (k < size) lz[k] = composition(f, lz[k]); } void push(int k) { all_apply(2 * k, lz[k]); all_apply(2 * k + 1, lz[k]); lz[k] = id(); } }; ll op(ll x, ll y) {return min(x, y);} ll e() {return 1000000000000000000;} ll mapping(ll f, ll x) {return min(f, x);} ll composition(ll f, ll g) {return min(f, g);} ll id() {return 1000000000000000000;} int par[3010], siz[3010], depth[3010]; int dfs1(int v, int pv, int d) { par[v] = pv; siz[v] = 1; depth[v] = d; rep(i, G[v].size()) { int nv = G[v][i].first; if (nv==pv) continue; siz[v] += dfs1(nv, v, d+1); } return siz[v]; } int in_order[3010], top[3010]; int in_order_now; void dfs2(int v, int pv, int top_node) { in_order[v] = in_order_now; in_order_now++; top[v] = top_node; if (siz[v]==1) return; int M = 0; int heavy_node = -1; rep(i, G[v].size()) { int nv = G[v][i].first; if (nv==pv) continue; if (siz[nv]>M) { M = siz[nv]; heavy_node = nv; } } dfs2(heavy_node, v, top_node); rep(i, G[v].size()) { int nv = G[v][i].first; if (nv==pv) continue; if (nv!=heavy_node) dfs2(nv, v, nv); } } vector<P2> query(int u, int v) { vector<P2> res; while (top[u]!=top[v]) { if (depth[top[u]]<=depth[top[v]]) { res.pb(P2(in_order[top[v]], in_order[v])); v = par[top[v]]; } else { res.pb(P2(in_order[top[u]], in_order[u])); u = par[top[u]]; } } res.pb(P2(min(in_order[u], in_order[v]), max(in_order[u], in_order[v]))); return res; } int main() { //ループ変数が被っていないか? //制約を確認しているか? //変数のtypoがないか? cin.tie(0); ios::sync_with_stdio(false); cin >> N >> Q >> C; rep(i, N-1) { int u, v; cin >> u >> v; ll l; cin >> l; G[u-1].pb(P1(v-1, l)); G[v-1].pb(P1(u-1, l)); } rep(i, N) dists.pb(bfs(i)); rep(i, Q) cin >> x[i]; rep(i, Q) x[i]--; rep(i, 3010) rep(j, 3010) dp[i][j] = 1000000000000000000; dp[0][x[0]] = 0; dfs1(0, -1, 0); dfs2(0, -1, 0); for (int i=1; i<Q; i++) { lazy_segtree<ll, op, e, ll, mapping, composition, id> lst(N); rep(j, N) { dp[i][j] = min(dp[i][j], dp[i-1][j]+dists[x[i-1]][x[i]]); vector<P2> section = query(j, x[i]); ll v = dp[i-1][j]+min(C, dists[x[i-1]][j])+dists[j][x[i]]; rep(k, section.size()) { ll l = section[k].first, r = section[k].second; lst.apply(l, r+1, v); } rep(j, N) dp[i][j] = min(dp[i][j], lst.get(in_order[j])); } } ll ans = 1000000000000000000; rep(i, N) ans = min(ans, dp[Q-1][i]); cout << ans << endl; }