結果
問題 | No.1308 ジャンプビーコン |
ユーザー | jupiro |
提出日時 | 2020-12-05 19:25:28 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 3,026 ms / 4,000 ms |
コード長 | 6,859 bytes |
コンパイル時間 | 2,627 ms |
コンパイル使用メモリ | 175,596 KB |
実行使用メモリ | 266,880 KB |
最終ジャッジ日時 | 2024-09-15 23:04:31 |
合計ジャッジ時間 | 41,493 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
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 | 2 ms
5,376 KB |
testcase_06 | AC | 2 ms
5,376 KB |
testcase_07 | AC | 2 ms
5,376 KB |
testcase_08 | AC | 3 ms
5,376 KB |
testcase_09 | AC | 3 ms
5,376 KB |
testcase_10 | AC | 3 ms
5,376 KB |
testcase_11 | AC | 3 ms
5,376 KB |
testcase_12 | AC | 3 ms
5,376 KB |
testcase_13 | AC | 109 ms
15,744 KB |
testcase_14 | AC | 83 ms
15,984 KB |
testcase_15 | AC | 79 ms
15,984 KB |
testcase_16 | AC | 53 ms
15,872 KB |
testcase_17 | AC | 50 ms
15,872 KB |
testcase_18 | AC | 2,568 ms
266,624 KB |
testcase_19 | AC | 2,540 ms
266,624 KB |
testcase_20 | AC | 1,660 ms
266,752 KB |
testcase_21 | AC | 1,462 ms
266,624 KB |
testcase_22 | AC | 1,464 ms
266,624 KB |
testcase_23 | AC | 2 ms
5,376 KB |
testcase_24 | AC | 3 ms
5,376 KB |
testcase_25 | AC | 2 ms
5,376 KB |
testcase_26 | AC | 1,183 ms
266,624 KB |
testcase_27 | AC | 1,164 ms
266,624 KB |
testcase_28 | AC | 1,125 ms
266,624 KB |
testcase_29 | AC | 2,565 ms
266,880 KB |
testcase_30 | AC | 2,541 ms
266,752 KB |
testcase_31 | AC | 3,026 ms
266,752 KB |
testcase_32 | AC | 2,386 ms
266,752 KB |
testcase_33 | AC | 2,660 ms
266,752 KB |
testcase_34 | AC | 1,351 ms
266,496 KB |
testcase_35 | AC | 1,335 ms
266,624 KB |
testcase_36 | AC | 1,583 ms
266,752 KB |
testcase_37 | AC | 1,631 ms
266,752 KB |
testcase_38 | AC | 2,456 ms
266,624 KB |
testcase_39 | AC | 2,471 ms
266,624 KB |
ソースコード
#pragma GCC optimize("Ofast") #include <iostream> #include <string> #include <sstream> #include <stack> #include <algorithm> #include <cmath> #include <queue> #include <bitset> #include <iomanip> #include <limits> #include <chrono> #include <random> #include <array> #include <unordered_map> #include <functional> #include <complex> #include <numeric> #include <cctype> #include <map> #include <set> #include <cstdlib> #include <bitset> #include <tuple> #include <assert.h> #include <deque> #include <utility> #include <fstream> using namespace std; typedef long long ll; using ull = unsigned long long; template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } //template<typename T> T gcd(T a, T b) { a = abs(a), b = abs(b); while (b > 0) { tie(a, b) = make_pair(b, a % b); }return a; } //mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count()); constexpr long long INF = 1LL << 60; constexpr int inf = 1000000007; constexpr long long mod = 1000000007LL; //constexpr long long mod = 998244353; struct HLD { vector<int> sz, in, out, head, par; vector<vector<int>> g; HLD() {} HLD(vector<vector<int>>& _g) : g(_g), sz(_g.size()), in(_g.size()), out(_g.size()), head(_g.size()), par(_g.size()) { build(); } void dfs_sz(int cur, int pre) { sz[cur] = 1; for (int& next : g[cur]) { if (next == pre) continue; dfs_sz(next, cur); par[next] = cur; sz[cur] += sz[next]; if (sz[next] > sz[g[cur][0]]) swap(next, g[cur][0]); } } void dfs_hld(int cur, int pre, int& idx) { in[cur] = idx++; for (int& next : g[cur]) { if (next == pre) continue; if (next == g[cur][0]) head[next] = head[cur]; else head[next] = next; dfs_hld(next, cur, idx); } out[cur] = idx; } void build() { dfs_sz(0, -1); int idx = 0; dfs_hld(0, -1, idx); } void build(vector<vector<int>>& _g) { g = _g; sz.resize(g.size()); in.resize(g.size()); out.resize(g.size()); head.resize(g.size()); par.resize(g.size()); dfs_sz(0, -1); int idx = 0; dfs_hld(0, -1, idx); } int lca(int u, int v) { for (;; v = par[head[v]]) { if (in[u] > in[v]) swap(u, v); if (head[u] == head[v]) return u; } } //[l, r)の区間集合を返す vector<pair<int, int>> path_query(int u, int v, bool edge = false) { vector<pair<int, int>> ret; for (;; v = par[head[v]]) { if (in[u] > in[v]) swap(u, v); if (head[u] == head[v]) break; ret.emplace_back(in[head[v]], in[v] + 1); } ret.emplace_back(in[u] + edge, in[v] + 1); return ret; } }; template< typename OperatorMonoid, typename H > struct DualSegmentTree { int sz, height; vector< OperatorMonoid > lazy; const H h; const OperatorMonoid OM0; DualSegmentTree(int n, const H h, const OperatorMonoid& OM0) : h(h), OM0(OM0) { sz = 1; height = 0; while (sz < n) sz <<= 1, height++; lazy.assign(2 * sz, OM0); } inline void propagate(int k) { if (lazy[k] != OM0) { lazy[2 * k + 0] = h(lazy[2 * k + 0], lazy[k]); lazy[2 * k + 1] = h(lazy[2 * k + 1], lazy[k]); lazy[k] = OM0; } } inline void thrust(int k) { for (int i = height; i > 0; i--) propagate(k >> i); } void update(int a, int b, const OperatorMonoid& x) { thrust(a += sz); thrust(b += sz - 1); for (int l = a, r = b + 1; l < r; l >>= 1, r >>= 1) { if (l & 1) lazy[l] = h(lazy[l], x), ++l; if (r & 1) --r, lazy[r] = h(lazy[r], x); } } OperatorMonoid operator[](int k) { thrust(k += sz); return lazy[k]; } }; template< typename OperatorMonoid, typename H > DualSegmentTree< OperatorMonoid, H > get_dual_segment_tree(int N, const H& h, const OperatorMonoid& OM0) { return { N, h, OM0 }; } struct Edge { int from, to; ll cost; Edge(int from, int to, ll cost) :from(from), to(to), cost(cost) {} }; vector<ll> bfs(int st, vector<vector<pair<int, ll>>>& g) { int n = g.size(); vector<ll> d(n, INF); d[st] = 0; queue<int> q; q.emplace(st); while (!q.empty()) { int cur = q.front(); q.pop(); for (auto [nxt, len] : g[cur]) { if (chmin(d[nxt], d[cur] + len)) { q.emplace(nxt); } } } return d; } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int n, kkt; cin >> n >> kkt; ll C; cin >> C; vector<vector<int>> g(n); vector<vector<pair<int, ll>>> tg(n); for (int i = 0; i < n - 1; i++) { int u, v, l; cin >> u >> v >> l; u--; v--; g[u].emplace_back(v); g[v].emplace_back(u); tg[u].emplace_back(v, l); tg[v].emplace_back(u, l); } vector<vector<ll>> dist(n); for (int i = 0; i < n; i++) dist[i] = move(bfs(i, tg)); HLD hld(g); auto f1 = [](ll a, ll b) {return min(a, b); }; auto f2 = [](ll a, ll b) { return min(a, b); }; auto lsg = get_dual_segment_tree(n, f2, INF); vector d(kkt, lsg); vector<int> x(kkt); for (int i = 0; i < kkt; i++) cin >> x[i], x[i]--; d[0].update(hld.in[x[0]], hld.in[x[0]] + 1, 0); for (int jupi = 1; jupi < kkt; jupi++) { int cur = x[jupi]; int pre = x[jupi - 1]; ll p = INF; for (int j = 0; j < n; j++) { chmin(p, d[jupi - 1][j]); } { int u = pre; int v = cur; for (;; v = hld.par[hld.head[v]]) { if (hld.in[u] > hld.in[v]) swap(u, v); if (hld.head[u] == hld.head[v]) break; d[jupi].update(hld.in[hld.head[v]], hld.in[v] + 1, p + dist[pre][cur]); } d[jupi].update(hld.in[u], hld.in[v] + 1, p + dist[pre][cur]); } for (auto [l, r] : hld.path_query(pre, cur)) { d[jupi].update(l, r, p + dist[pre][cur]); } for (int j = 0; j < n; j++) { ll t = dist[pre][cur]; ll p = d[jupi - 1][hld.in[j]]; if (p == INF) continue; d[jupi].update(hld.in[j], hld.in[j] + 1, p + t); if (C + dist[j][cur] < t) { { int u = j; int v = cur; for (;; v = hld.par[hld.head[v]]) { if (hld.in[u] > hld.in[v]) swap(u, v); if (hld.head[u] == hld.head[v]) break; d[jupi].update(hld.in[hld.head[v]], hld.in[v] + 1, p + C + dist[j][cur]); } d[jupi].update(hld.in[u], hld.in[v] + 1, p + C + dist[j][cur]); } } } } ll res = INF; for (int i = 0; i < n; i++) chmin(res, d.back()[i]); cout << res << "\n"; }