結果
| 問題 |
No.1308 ジャンプビーコン
|
| コンテスト | |
| ユーザー |
emthrm
|
| 提出日時 | 2020-12-05 00:31:42 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 7,448 bytes |
| コンパイル時間 | 3,302 ms |
| コンパイル使用メモリ | 227,808 KB |
| 最終ジャッジ日時 | 2025-01-16 16:25:41 |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 19 TLE * 18 |
ソースコード
#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>
struct HLD {
std::vector<int> parent, subtree, id, inv, head;
std::vector<CostType> cost;
HLD(const std::vector<std::vector<Edge<CostType>>> &graph, int root = 0) : graph(graph) {
int n = graph.size();
parent.assign(n, -1);
subtree.assign(n, 1);
id.resize(n);
inv.resize(n);
head.resize(n);
dfs1(root);
head[root] = root;
int now_id = 0;
dfs2(root, now_id);
}
template <typename Fn>
void v_update(int u, int v, Fn f) const {
while (true) {
if (id[u] > id[v]) std::swap(u, v);
f(std::max(id[head[v]], id[u]), id[v] + 1);
if (head[u] == head[v]) return;
v = parent[head[v]];
}
}
template <typename T, typename F, typename G>
T v_query(int u, int v, F f, G g, const T UNITY) const {
T left = UNITY, right = UNITY;
while (true) {
if (id[u] > id[v]) {
std::swap(u, v);
std::swap(left, right);
}
left = g(left, f(std::max(id[head[v]], id[u]), id[v] + 1));
if (head[u] == head[v]) break;
v = parent[head[v]];
}
return g(left, right);
}
template <typename Fn>
void subtree_v_update(int ver, Fn f) const { f(id[ver], id[ver] + subtree[ver]); }
template <typename T, typename Fn>
T subtree_v_query(int ver, Fn f) const { return f(id[ver], id[ver] + subtree[ver]); }
template <typename Fn>
void e_update(int u, int v, Fn f) const {
while (true) {
if (id[u] > id[v]) std::swap(u, v);
if (head[u] == head[v]) {
f(id[u], id[v]);
break;
} else {
f(id[head[v]] - 1, id[v]);
v = parent[head[v]];
}
}
}
template <typename T, typename F, typename G>
T e_query(int u, int v, F f, G g, const T UNITY) const {
T left = UNITY, right = UNITY;
while (true) {
if (id[u] > id[v]) {
std::swap(u, v);
std::swap(left, right);
}
if (head[u] == head[v]) {
left = g(left, f(id[u], id[v]));
break;
} else {
left = g(left, f(id[head[v]] - 1, id[v]));
v = parent[head[v]];
}
}
return g(left, right);
}
template <typename Fn>
void subtree_e_update(int ver, Fn f) const { f(id[ver], id[ver] + subtree[ver] - 1); }
template <typename T, typename Fn>
T subtree_e_query(int ver, Fn f) const { return f(id[ver], id[ver] + subtree[ver] - 1); }
int lca(int u, int v) const {
while (true) {
if (id[u] > id[v]) std::swap(u, v);
if (head[u] == head[v]) return u;
v = parent[head[v]];
}
}
private:
std::vector<std::vector<Edge<CostType>>> graph;
void dfs1(int ver) {
for (Edge<CostType> &e : graph[ver]) {
if (e.dst != parent[ver]) {
parent[e.dst] = ver;
dfs1(e.dst);
subtree[ver] += subtree[e.dst];
if (subtree[e.dst] > subtree[graph[ver].front().dst]) std::swap(e, graph[ver].front());
}
}
}
void dfs2(int ver, int &now_id) {
id[ver] = now_id++;
inv[id[ver]] = ver;
for (const Edge<CostType> &e : graph[ver]) {
if (e.dst != parent[ver]) {
head[e.dst] = (e.dst == graph[ver].front().dst ? head[ver] : e.dst);
cost.emplace_back(e.cost);
dfs2(e.dst, now_id);
}
}
}
};
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);
}
}
};
int main() {
int n, q, c; cin >> n >> q >> c;
vector<vector<Edge<ll>>> graph(n);
REP(_, n - 1) {
int u, v, l; cin >> u >> v >> l; --u; --v;
graph[u].emplace_back(u, v, l);
graph[v].emplace_back(v, u, l);
}
vector<int> x(q); REP(i, q) cin >> x[i], --x[i];
HLD hld(graph);
LCADoubling lca(graph);
lca.build();
vector<ll> dp(n, LINF);
dp[x[0]] = 0;
FOR(i, 1, q) {
vector<vector<ll>> ins(n + 1), era(n + 1);
REP(v, n) {
ll t = dp[v] + c + lca.distance(v, x[i]);
hld.v_update(v, x[i], [&](int a, int b) {
ins[a].emplace_back(t);
era[b].emplace_back(t);
});
}
ll thr = *min_element(ALL(dp)) + lca.distance(x[i - 1], x[i]);
hld.v_update(x[i - 1], x[i], [&](int a, int b) {
ins[a].emplace_back(thr);
era[b].emplace_back(thr);
});
vector<ll> nx(n, LINF);
map<ll, int> mp;
REP(j, n) {
for (ll e : ins[j]) ++mp[e];
for (ll e : era[j]) {
--mp[e];
if (mp[e] == 0) mp.erase(e);
}
if (!mp.empty()) nx[j] = mp.begin()->first;
}
REP(j, n) {
dp[j] += lca.distance(x[i - 1], x[i]);
chmin(dp[j], nx[hld.id[j]]);
}
}
cout << *min_element(ALL(dp)) << '\n';
return 0;
}
emthrm