結果

問題 No.1780 [Cherry Anniversary] 真冬に咲く26の櫻の木
ユーザー sten_sansten_san
提出日時 2022-01-19 07:16:10
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 110 ms / 2,000 ms
コード長 6,066 bytes
コンパイル時間 2,606 ms
コンパイル使用メモリ 211,244 KB
実行使用メモリ 6,272 KB
最終ジャッジ日時 2024-05-02 19:40:20
合計ジャッジ時間 6,884 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 3 ms
5,376 KB
testcase_03 AC 4 ms
5,376 KB
testcase_04 AC 3 ms
5,376 KB
testcase_05 AC 3 ms
5,376 KB
testcase_06 AC 3 ms
5,376 KB
testcase_07 AC 3 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 2 ms
5,376 KB
testcase_13 AC 3 ms
5,376 KB
testcase_14 AC 21 ms
5,376 KB
testcase_15 AC 84 ms
5,504 KB
testcase_16 AC 80 ms
5,504 KB
testcase_17 AC 14 ms
5,376 KB
testcase_18 AC 51 ms
5,376 KB
testcase_19 AC 18 ms
5,376 KB
testcase_20 AC 61 ms
5,376 KB
testcase_21 AC 103 ms
6,144 KB
testcase_22 AC 104 ms
6,016 KB
testcase_23 AC 27 ms
5,376 KB
testcase_24 AC 110 ms
6,272 KB
testcase_25 AC 25 ms
5,376 KB
testcase_26 AC 20 ms
5,376 KB
testcase_27 AC 52 ms
5,376 KB
testcase_28 AC 13 ms
5,376 KB
testcase_29 AC 10 ms
5,376 KB
testcase_30 AC 99 ms
6,016 KB
testcase_31 AC 6 ms
5,376 KB
testcase_32 AC 10 ms
5,376 KB
testcase_33 AC 45 ms
5,376 KB
testcase_34 AC 2 ms
5,376 KB
testcase_35 AC 5 ms
5,376 KB
testcase_36 AC 2 ms
5,376 KB
testcase_37 AC 5 ms
5,376 KB
testcase_38 AC 5 ms
5,376 KB
testcase_39 AC 5 ms
5,376 KB
testcase_40 AC 5 ms
5,376 KB
testcase_41 AC 5 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

struct iofast_t {
    iofast_t() {
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
    }
} iofast;

struct uns_t {} uns;
template <typename Element, typename Head, typename ...Args>
auto vec(Element init, Head arg, Args ...args) {
    if constexpr (sizeof...(Args) == 0) return vector(arg, init);
    else return vector(arg, vec(init, args...));
}
template <typename Element, typename Head, typename ...Args>
auto vec(uns_t, Head arg, Args ...args) {
    return vec(Element(), arg, args...);
}

template <typename Container>
auto distance(const Container &c, decltype(begin(c)) iter) {
    return distance(begin(c), iter);
}

template <typename RIter, typename Compare = less<typename iterator_traits<RIter>::value_type>>
auto isort(RIter first, RIter last, Compare comp = Compare()) {
    vector<int> i(distance(first, last));
    iota(begin(i), end(i), 0);
    sort(begin(i), end(i), [&](auto x, auto y) {
        return comp(*(first + x), *(first + y));
    });
    return i;
}

template <typename, template <typename> typename, typename = void_t<>>
struct detect : false_type {};
template <typename T, template <typename> typename Check>
struct detect<T, Check, void_t<Check<T>>> : true_type {};
template <typename T, template <typename> typename Check>
constexpr inline bool detect_v = detect<T, Check>::value;

template <typename T>
using has_member_sort = decltype(declval<T>().sort());

template <typename Container, typename Compare = less<typename Container::value_type>>
auto sorted(Container c, Compare comp = Compare()) {
    if constexpr (detect_v<Container, has_member_sort>) {
        c.sort(comp);
        return c;
    }
    else {
        sort(begin(c), end(c), comp);
        return c;
    }
}

template <typename Container, typename Compare = equal_to<typename Container::value_type>>
auto uniqued(Container c, Compare comp = Compare()) {
    c.erase(unique(begin(c), end(c), comp), end(c));
    return c;
}

template <typename T, typename Compare = less<T>>
T &chmin(T &l, T r, Compare &&f = less<T>()) { return l = min(l, r, f); }
template <typename T, typename Compare = less<T>>
T &chmax(T &l, T r, Compare &&f = less<T>()) { return l = max(l, r, f); }

template <typename T, T (*E1)(), T (*E2)(), T (*Op)(T, T), T (*Prod)(T, T)>
struct matrix {
    matrix(): matrix(0, 0) {
    }

    matrix(size_t h, size_t w, T init = E1()):
        h_(h), w_(w), mat_(h * w, std::move(init)) {
    }

    matrix(const matrix &r):
        h_(r.h_), w_(r.w_), mat_(r.mat_) {
    }
    matrix(matrix &&r):
        h_(r.h_), w_(r.w_), mat_() {
        mat_ = std::move(r.mat_);
    }

    matrix &operator =(const matrix &r) {
        h_ = r.h_; w_ = r.w_;
        mat_ = r.mat_;
        return *this;
    }
    matrix &operator =(matrix &&r) {
        h_ = r.h_; w_ = r.w_;
        mat_ = std::move(r.mat_);
        return *this;
    }

    size_t height() const noexcept {
        return h_;
    }
    size_t width() const noexcept {
        return w_;
    }

    T &at(size_t y, size_t x) {
        return mat_[w_ * y + x];
    }
    const T &at(size_t y, size_t x) const {
        return mat_[w_ * y + x];
    }

    matrix &operator +=(const matrix &r) {
        assert(h_ == r.h_ && w_ == r.w_);
        for (size_t i = 0; i < w_; ++i) {
            for (size_t j = 0; j < h_; ++j) {
                at(i, j) = Op(at(i, j), r.at(i, j));
            }
        }
        return *this;
    }

    matrix operator +(const matrix &r) {
        return matrix(*this) += r;
    }

    matrix operator *(const matrix &r) {
        assert(w_ == r.h_);
        matrix m(h_, r.w_);
        for (size_t i = 0; i < h_; ++i) {
            for (size_t j = 0; j < r.w_; ++j) {
                for (size_t k = 0; k < w_; ++k) {
                    m.at(i, j) = Op(m.at(i, j), Prod(at(i, k), r.at(k, j)));
                }
            }
        }
        return m;
    }

private:
    size_t h_, w_;
    std::vector<T> mat_;
};

constexpr auto inf = INT64_MAX / 2;

constexpr int64_t e1() {
    return -inf;
}

constexpr int64_t e2() {
    return 0;
}

constexpr int64_t op(int64_t x, int64_t y) {
    return max(x, y);
}

constexpr int64_t prod(int64_t x, int64_t y) {
    return x + y;
}

using mat = matrix<int64_t, e1, e2, op, prod>;

int main() {
    constexpr int alpha = 'z' - 'a' + 1;
    constexpr auto lim = 16;

    array<int, alpha> c;
    for (auto &e : c) {
        cin >> e; --e;
    }

    array<int, alpha> k;
    for (auto &e : k) cin >> e;

    int n; cin >> n;
    auto edge = vec<array<int, 4>>(uns, n);
    for (auto &[bits, a, b, e] : edge) {
        string s; cin >> s >> a >> b >> e; --a; --b;

        bits = 0;
        for (auto c : s) {
            bits |= (1 << (c - 'A'));
        }
    }

    auto dist = vec<mat>(uns, alpha);
    for (int i = 0; i < alpha; ++i) {
        mat m(lim, lim);
        for (auto [bits, a, b, e] : edge) {
            if (!(bits & (1 << i))) {
                continue;
            }
            chmax<int64_t>(m.at(a, b), e);
            chmax<int64_t>(m.at(b, a), e);
        }
        for (int j = 0; j < lim; ++j) {
            m.at(j, j) = 0;
        }

        mat x(lim, lim);
        for (int j = 0; j < lim; ++j) {
            x.at(j, j) = 0;
        }

        int y = k[i];
        while (0 < y) {
            if (y & 1) {
                x = m * x;
            }
            m = m * m;
            y >>= 1;
        }

        mat r(lim, 1);
        r.at(c[i], 0) = 0;

        dist[i] = x * r;
    }

    int64_t ans = -inf;
    for (int i = 0; i < lim; ++i) {
        bool found = false;

        int64_t sum = 0;
        for (int j = 0; j < alpha; ++j) {
            if (-inf / 10 < dist[j].at(i, 0)) {
                sum += dist[j].at(i, 0);
            }
            else {
                found = true;
            }
        }

        if (found) {
            continue;
        }

        chmax(ans, sum);
    }

    if (ans != -inf) {
        cout << ans << endl;
    }
    else {
        cout << "Impossible" << endl;
    }
}

0