結果

問題 No.1308 ジャンプビーコン
ユーザー PachicobuePachicobue
提出日時 2020-12-05 02:20:23
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 14,405 bytes
コンパイル時間 3,672 ms
コンパイル使用メモリ 246,532 KB
実行使用メモリ 712,536 KB
最終ジャッジ日時 2024-09-15 10:02:09
合計ジャッジ時間 11,278 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
10,624 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 MLE -
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 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using ll = long long;
using uint = unsigned int;
using ull = unsigned long long;
using ld = long double;
template<typename T> using max_heap = std::priority_queue<T>;
template<typename T> using min_heap = std::priority_queue<T, std::vector<T>, std::greater<T>>;
constexpr int popcount(const ull v) { return v ? __builtin_popcountll(v) : 0; }
constexpr int log2p1(const ull v) { return v ? 64 - __builtin_clzll(v) : 0; }
constexpr int lsbp1(const ull v) { return __builtin_ffsll(v); }
constexpr int clog(const ull v) { return v ? log2p1(v - 1) : 0; }
constexpr ull ceil2(const ull v) { return 1ULL << clog(v); }
constexpr ull floor2(const ull v) { return v ? (1ULL << (log2p1(v) - 1)) : 0ULL; }
constexpr bool btest(const ull mask, const int ind) { return (mask >> ind) & 1ULL; }
template<typename T> void bset(T& mask, const int ind) { mask |= ((T)1 << ind); }
template<typename T> void breset(T& mask, const int ind) { mask &= ~((T)1 << ind); }
template<typename T> void bflip(T& mask, const int ind) { mask ^= ((T)1 << ind); }
template<typename T> void bset(T& mask, const int ind, const bool b) { (b ? bset(mask, ind) : breset(mask, ind)); }
template<typename MergeMonoid, typename OpMonoid, typename Act>
class lazyseg
{
    static_assert(std::is_same_v<typename MergeMonoid::T, typename Act::T>);
    static_assert(std::is_same_v<typename OpMonoid::F, typename Act::F>);
    using T = typename MergeMonoid::T;
    using F = typename OpMonoid::F;
    static constexpr T e() { return MergeMonoid::e(); }
    static constexpr F id() { return OpMonoid::id(); }
public:
    lazyseg(const std::vector<T>& vs) : m_size{(int)vs.size()},
                                        m_depth{clog(m_size)},
                                        m_half{1 << m_depth},
                                        m_vals(m_half << 1, MergeMonoid::e()),
                                        m_ops(m_half << 1, OpMonoid::id()),
                                        m_merge{},
                                        m_compose{},
                                        m_apply{}
    {
        std::copy(vs.begin(), vs.end(), m_vals.begin() + m_half);
        for (int i = m_half - 1; i >= 1; i--) { up(i); }
    }
    T get(const int a) { return assert(a < m_size), fold(a, a + 1); }
    void set(int a, const T& v)
    {
        assert(0 <= a and a < m_size);
        top_down(a += m_half), top_down(a + 1), m_ops[a] = id(), m_vals[a] = v;
        while (a >>= 1) { up(a); }
    }
    T fold(int l, int r)
    {
        assert(0 <= l and l <= r and r <= m_size);
        if (l >= r) { return e(); }
        top_down(l += m_half), top_down(r += m_half);
        T accl = e(), accr = e();
        for (; l < r; l >>= 1, r >>= 1) {
            if (l & 1) { accl = m_merge(accl, m_vals[l++]); }
            if (r & 1) { accr = m_merge(m_vals[--r], accr); }
        }
        return m_merge(accl, accr);
    }
    void act(int l, int r, const F& f)
    {
        assert(0 <= l and l <= r and r <= m_size);
        const int lin = l + m_half, rin = r + m_half;
        top_down(l += m_half), top_down(r += m_half);
        for (int ls = 1, rs = 1; l < r; l >>= 1, r >>= 1, ls <<= 1, rs <<= 1) {
            if (l & 1) { update(l++, f, ls); }
            if (r & 1) { update(--r, f, rs); }
        }
        bottom_up(lin), bottom_up(rin);
    }
    friend std::ostream& operator<<(std::ostream& os, const lazyseg& lseg)
    {
        auto lseg2 = lseg;
        os << "[";
        for (int i = 0; i < lseg.m_size; i++) { os << lseg2.get(i) << (i + 1 == lseg2.m_size ? "" : ","); }
        return (os << "]\n");
    }
private:
    void up(const int i) { m_vals[i] = m_merge(m_vals[i << 1], m_vals[i << 1 | 1]); }
    void update(const int a, const F& f, const int l) { m_ops[a] = m_compose(f, m_ops[a]), m_vals[a] = m_apply(f, m_vals[a], l); }
    void down(const int a, const int l) { update(a << 1, m_ops[a], l >> 1), update(a << 1 | 1, m_ops[a], l >> 1), m_ops[a] = id(); }
    void top_down(const int a)
    {
        const int b = (a / (a & -a)) >> 1;
        for (int i = 0, l = m_half; i < m_depth; i++, l >>= 1) {
            const int v = a >> (m_depth - i);
            if (v > b) { break; }
            down(v, l);
        }
    }
    void bottom_up(int a)
    {
        a = (a / (a & -a)) >> 1;
        for (; a >= 1; a >>= 1) { up(a); }
    }
    const int m_size, m_depth, m_half;
    std::vector<T> m_vals;
    std::vector<F> m_ops;
    const MergeMonoid m_merge;
    const OpMonoid m_compose;
    const Act m_apply;
};
template<typename T = int>
class graph
{
public:
    graph(const int size) : m_size{size}, m_eis(m_size) {}
    void add_edge(const int u, const int v, const T& c, const bool bi = false)
    {
        const int ei = (int)m_us.size();
        m_eis[u].push_back(ei);
        if (bi) { m_eis[v].push_back(ei); }
        m_us.push_back(u), m_vs.push_back(v), m_cs.push_back(c);
    }
    void add_edge(const int u, const int v, const bool bi = false) { add_edge(u, v, 1, bi); }
    const std::vector<int>& operator[](const int u) const { return m_eis[u]; }
    std::vector<int>& operator[](const int u) { return m_eis[u]; }
    std::tuple<int, int, T> edge(const int u, const int i) const { return std::make_tuple(u, m_us[i] ^ m_vs[i] ^ u, m_cs[i]); }
    int size() const { return m_size; }
    friend std::ostream& operator<<(std::ostream& os, const graph& g)
    {
        for (int u = 0; u < g.size(); u++) {
            for (const int ei : g[u]) {
                const auto& [from, to, cost] = g.edge(u, ei);
                os << "[" << ei << "]: " << from << "->" << to << "(" << cost << ")\n";
            }
        }
        return os;
    }
private:
    int m_size;
    std::vector<std::vector<int>> m_eis;
    std::vector<int> m_us, m_vs;
    std::vector<T> m_cs;
};
template<typename T>
class hl_decomp
{
public:
    hl_decomp(graph<T>& g, const int r = 0) : m_pars(g.size(), -1), m_tops{m_pars}, m_ins{m_pars}, m_ords{m_pars}, m_outs{m_pars}
    {
        const int N = g.size();
        std::vector<int> szs(N, 1);
        auto dfs1 = [&](auto self, const int u, const int p) -> int {
            m_pars[u] = p;
            for (int& ei : g[u]) {
                [[maybe_unused]] const auto& [from, to, cost] = g.edge(u, ei);
                if (p == to) { continue; }
                szs[u] += self(self, to, u);
                if (szs[std::get<1>(g.edge(u, g[u][0]))] < szs[to]) { std::swap(g[u][0], ei); }
            }
            return szs[u];
        };
        dfs1(dfs1, r, -1);
        m_tops[r] = r;
        auto dfs2 = [&](auto&& self, const int u, const int p, int& ind) -> void {
            m_ins[u] = ind++, m_ords[m_ins[u]] = u;
            for (const int ei : g[u]) {
                [[maybe_unused]] const auto& [from, to, cost] = g.edge(u, ei);
                if (to == p) { continue; }
                m_tops[to] = (ei == g[u][0] ? m_tops[u] : to);
                self(self, to, u, ind);
            }
            m_outs[u] = ind;
        };
        int ind = 0;
        dfs2(dfs2, r, -1, ind);
    }
    int pos(const int v) const { return m_ins[v]; }
    int at(const int n) const { return m_ords[n]; }
    std::pair<int, int> sub(const int v) const { return {m_ins[v], m_outs[v]}; }
    std::vector<std::pair<int, int>> path(int u, int v) const
    {
        using P = std::pair<int, int>;
        std::vector<P> head, tail;
        for (int pu = m_tops[u], pv = m_tops[v]; pu != pv;) {
            if (m_ins[pu] < m_ins[pv]) {
                tail.push_back({m_ins[pv], m_ins[v]});
                v = m_pars[pv], pv = m_tops[v];
            } else {
                head.push_back({m_ins[u], m_ins[pu]});
                u = m_pars[pu], pu = m_tops[u];
            }
        }
        head.push_back({m_ins[u], m_ins[v]});
        std::reverse(tail.begin(), tail.end());
        for (const auto& p : tail) { head.push_back(p); }
        return head;
    }
    friend std::ostream& operator<<(std::ostream& os, const hl_decomp& hld)
    {
        os << "ord = {";
        for (const int v : hld.m_ords) { os << v << ","; }
        os << "}\ntops = {";
        for (const int v : hld.m_tops) { os << v << ","; }
        return os << "}";
    }
private:
    std::vector<int> m_pars, m_tops, m_ins, m_ords, m_outs;
};
template<typename T> bool chmin(T& a, const T& b) { return (a > b ? a = b, true : false); }
template<typename T> bool chmax(T& a, const T& b) { return (a < b ? a = b, true : false); }
template<typename T> constexpr T inf_v = std::numeric_limits<T>::max() / 4;
template<typename Real> constexpr Real pi_v = Real{3.141592653589793238462643383279502884};
template<typename T> constexpr T TEN(const int n) { return n == 0 ? T{1} : TEN<T>(n - 1) * T{10}; }
template<typename F> struct fix : F
{
    fix(F&& f) : F{std::forward<F>(f)} {}
    template<typename... Args> auto operator()(Args&&... args) const { return F::operator()(*this, std::forward<Args>(args)...); }
};
template<typename T, int n, int i = 0>
auto nd_array(int const (&szs)[n], const T x = T{})
{
    if constexpr (i == n) {
        return x;
    } else {
        return std::vector(szs[i], nd_array<T, n, i + 1>(szs, x));
    }
}
class printer
{
public:
    printer(std::ostream& os_ = std::cout) : m_os{os_} { m_os << std::fixed << std::setprecision(15); }
    template<typename... Args> int ln(const Args&... args) { return dump(args...), m_os << '\n', 0; }
    template<typename... Args> int el(const Args&... args) { return dump(args...), m_os << std::endl, 0; }
private:
    template<typename T> void dump(const T& v) { m_os << v; }
    template<typename T> void dump(const std::vector<T>& vs)
    {
        for (int i = 0; i < (int)vs.size(); i++) { m_os << (i ? " " : ""), dump(vs[i]); }
    }
    template<typename T> void dump(const std::vector<std::vector<T>>& vss)
    {
        for (int i = 0; i < (int)vss.size(); i++) { m_os << (0 <= i or i + 1 < (int)vss.size() ? "\n" : ""), dump(vss[i]); }
    }
    template<typename T, typename... Args> int dump(const T& v, const Args&... args) { return dump(v), m_os << ' ', dump(args...), 0; }
    std::ostream& m_os;
};
printer out;
class scanner
{
public:
    scanner(std::istream& is_ = std::cin) : m_is{is_} { m_is.tie(nullptr), std::ios::sync_with_stdio(false); }
    void fastio(const bool on)
    {
        if (on) {
            m_is.tie(nullptr), std::ios::sync_with_stdio(false);
        } else {
            m_is.tie(&std::cout), std::ios::sync_with_stdio(true);
        }
    }
    template<typename T> T val()
    {
        T v;
        return m_is >> v, v;
    }
    template<typename T> T val(const T offset) { return val<T>() - offset; }
    template<typename T> std::vector<T> vec(const int n)
    {
        return make_v<T>(n, [this]() { return val<T>(); });
    }
    template<typename T> std::vector<T> vec(const int n, const T offset)
    {
        return make_v<T>(n, [this, offset]() { return val<T>(offset); });
    }
    template<typename T> std::vector<std::vector<T>> vvec(const int n0, const int n1)
    {
        return make_v<std::vector<T>>(n0, [this, n1]() { return vec<T>(n1); });
    }
    template<typename T> std::vector<std::vector<T>> vvec(const int n0, const int n1, const T offset)
    {
        return make_v<std::vector<T>>(n0, [this, n1, offset]() { return vec<T>(n1, offset); });
    }
    template<typename... Args> auto tup() { return std::tuple<std::decay_t<Args>...>{val<Args>()...}; }
    template<typename... Args> auto tup(const Args&... offsets) { return std::tuple<std::decay_t<Args>...>{val<Args>(offsets)...}; }
private:
    template<typename T, typename F>
    std::vector<T> make_v(const int n, F f)
    {
        std::vector<T> ans;
        for (int i = 0; i < n; i++) { ans.push_back(f()); }
        return ans;
    }
    std::istream& m_is;
};
scanner in;
int main()
{
    const auto [N, Q, C] = in.tup<int, int, ll>();
    graph<ll> g(N);
    for (int i = 0; i < N - 1; i++) {
        const auto [u, v, c] = in.tup<int, int, ll>(1, 1, 0);
        g.add_edge(u, v, c, true);
    }
    auto dss = nd_array<ll>({N, N}, 0);
    auto vss = nd_array<int>({N, N}, 0);
    auto pss = nd_array<int>({N, N}, 0);
    std::vector<hl_decomp<ll>> hlds;
    for (int i = 0; i < N; i++) {
        fix([&](auto self, const int s, const int p) -> void {
            for (const int ei : g[s]) {
                const auto [from, to, cost] = g.edge(s, ei);
                if (to == p) { continue; }
                dss[i][to] = dss[i][from] + cost;
                self(to, s);
            }
        })(i, -1);
        hlds.push_back(hl_decomp(g, i));
    }
    struct Min
    {
        using T = ll;
        T operator()(const T x1, const T x2) const { return std::min(x1, x2); }
        static constexpr T e() { return inf_v<T>; }
    };
    struct Assign
    {
        using F = ll;
        F operator()(const F f1, const F f2) const { return f1 == inf_v<F> ? f2 : f1; }
        static constexpr F id() { return inf_v<F>; }
    };
    struct Act
    {
        using T = ll;
        using F = ll;
        T operator()(const F& f, const T x, const int) const { return f == inf_v<ll> ? x : f; }
    };
    lazyseg<Min, Assign, Act> dp(std::vector<ll>(N, inf_v<ll>));
    std::vector<lazyseg<Min, Assign, Act>> segs(N, std::vector<ll>(N, inf_v<ll>));
    auto xs = in.vec<int>(Q, 1);
    xs.push_back(xs.back() == 0 ? 1 : 0);
    dp.set(hlds[xs[1]].pos(xs[0]), 0);
    segs[xs[1]].set(hlds[xs[1]].pos(xs[0]), dss[xs[1]][xs[0]]);
    for (int i = 1; i < Q; i++) {
        lazyseg<Min, Assign, Act> ndp(std::vector<ll>(N, inf_v<ll>));
        const auto ps = hlds[xs[i + 1]].path(xs[i - 1], xs[i]);
        for (auto [l, r] : ps) {
            if (l > r) { std::swap(l, r); }
            r++;
            ndp.act(l, r, dp.fold(0, N) + dss[xs[i]][xs[i - 1]]);
        }
        for (int j = 0; j < N; j++) {
            const int pos = hlds[xs[i + 1]].pos(j);
            ll min = dp.get(j) + dss[xs[i - 1]][xs[i]];
            const auto [l, r] = hlds[xs[i]].sub(j);
            chmin(min, segs[xs[i]].fold(l, r) + C);
            ndp.set(pos, std::min(min, ndp.get(pos)));
        }
        for (int i = 0; i < N; i++) { dp.set(i, ndp.get(i)); }
        for (int j = 0; j < N; j++) {
            const int pos = hlds[xs[i + 1]].pos(j);
            segs[xs[i + 1]].set(pos, dss[xs[i + 1]][j] + dp.get(pos));
        }
    }
    out.ln(dp.fold(0, N));
}
0