結果

問題 No.572 妖精の演奏
ユーザー MisterMister
提出日時 2020-09-06 21:33:07
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 30 ms / 2,000 ms
コード長 1,894 bytes
コンパイル時間 1,229 ms
コンパイル使用メモリ 81,716 KB
実行使用メモリ 4,500 KB
最終ジャッジ日時 2023-08-19 14:21:01
合計ジャッジ時間 2,114 ms
ジャッジサーバーID
(参考情報)
judge10 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 3 ms
4,500 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 3 ms
4,380 KB
testcase_09 AC 3 ms
4,376 KB
testcase_10 AC 13 ms
4,376 KB
testcase_11 AC 21 ms
4,384 KB
testcase_12 AC 25 ms
4,380 KB
testcase_13 AC 30 ms
4,376 KB
testcase_14 AC 7 ms
4,380 KB
testcase_15 AC 10 ms
4,380 KB
testcase_16 AC 7 ms
4,380 KB
testcase_17 AC 10 ms
4,380 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 1 ms
4,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>

using lint = long long;
constexpr lint INF = 1LL << 60;

template <class T>
struct Matrix {
    using M = std::vector<std::vector<T>>;

    int h, w;
    M mat;

    // constructor
    Matrix(int h, int w, T val = -INF)
        : h(h), w(w), mat(h, std::vector<T>(w, val)) {}

    static Matrix id(int n) {
        Matrix m(n, n);
        for (int i = 0; i < n; ++i) m[i][i] = 0;
        return m;
    }

    // getter
    std::vector<T>& operator[](int i) { return mat[i]; }
    std::vector<T> operator[](int i) const { return mat[i]; }
    typename M::iterator begin() { return mat.begin(); }
    typename M::iterator end() { return mat.end(); }

    // arithmetic
    Matrix operator*(const Matrix& m) const { return Matrix(*this) *= m; }

    template <class U>
    Matrix pow(U k) {
        Matrix ret = id(h);
        Matrix a = *this;

        while (k > 0) {
            if (k & 1) ret *= a;
            a *= a;
            k >>= 1;
        }
        return ret;
    }

    // compound assignment
    Matrix& operator*=(const Matrix& m) {
        std::vector<std::vector<T>> nmat(h, std::vector<T>(m.w, T(0)));
        for (int i = 0; i < h; ++i) {
            for (int j = 0; j < m.w; ++j) {
                for (int k = 0; k < w; ++k) {
                    nmat[i][j] = std::max(nmat[i][j], mat[i][k] + m[k][j]);
                }
            }
        }
        mat = nmat;
        return *this;
    }
};

void solve() {
    lint n;
    int m;
    std::cin >> n >> m;

    Matrix<lint> mat(m, m);
    for (auto& v : mat) {
        for (auto& x : v) std::cin >> x;
    }

    mat = mat.pow(n - 1);

    lint ans = -INF;
    for (auto& v : mat) {
        for (auto& x : v) ans = std::max(ans, x);
    }

    std::cout << ans << "\n";
}

int main() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);

    solve();

    return 0;
}
0