結果
問題 | No.572 妖精の演奏 |
ユーザー |
|
提出日時 | 2020-09-06 21:33:07 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 53 ms / 2,000 ms |
コード長 | 1,894 bytes |
コンパイル時間 | 1,014 ms |
コンパイル使用メモリ | 81,040 KB |
最終ジャッジ日時 | 2025-01-14 08:02:25 |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 20 |
ソースコード
#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; }