結果
問題 | No.1780 [Cherry Anniversary] 真冬に咲く26の櫻の木 |
ユーザー |
|
提出日時 | 2021-12-09 17:19:53 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 2,267 bytes |
コンパイル時間 | 884 ms |
コンパイル使用メモリ | 80,020 KB |
実行使用メモリ | 6,944 KB |
最終ジャッジ日時 | 2024-07-17 10:06:54 |
合計ジャッジ時間 | 4,847 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 40 WA * 2 |
ソースコード
#include <iostream> #include <vector> #include <string> using namespace std; const long long INF = (1LL << 50); template<typename T> class Matrix { public: explicit Matrix(int N) : m(N, vector<T>(N, 0)) {} static Matrix ident(int N) { Matrix res(N); for(int i=0;i<N;i++){ for(int j=0;j<N;j++){ res[i][j] = (i == j ? 0 : -INF); } } return res; } vector<T>& operator [](int n) { return m[n]; } const vector<T>& operator [](int n) const { return m[n]; } int size() const { return m.size(); } public: vector<vector<T>> m; }; template<typename T> Matrix<T> mul(const Matrix<T>& a, const Matrix<T>& b){ const int N = a.size(); Matrix<T> res(N); for(int i=0;i<N;i++){ for(int j=0;j<N;j++){ res[i][j] = -INF; for(int k=0;k<N;k++) res[i][j] = max(res[i][j], a[i][k] + b[k][j]); } } return res; } template<typename T> Matrix<T> pow(const Matrix<T>& a, int p){ const int N = a.size(); if(p == 0) return Matrix<T>::ident(N); if(p == 1) return a; auto res = pow(a, p/2); res = mul(res, res); if(p%2) res = mul(res, a); return res; } bool solve(){ vector<int> C(26); for(auto& c : C){ if(!(cin >> c)) return false; } vector<int> K(26); for(auto& k : K) cin >> k; int N; cin >> N; vector<Matrix<long long>> v(26, Matrix<long long>::ident(16)); for(int i=0;i<N;i++){ string S; int A, B, E; cin >> S >> A >> B >> E; for(auto& s : S){ if(v[s-'A'][A-1][B-1] < E){ v[s-'A'][A-1][B-1] = v[s-'A'][B-1][A-1] = E; } } } vector<long long> res(16, 0); for(int i=0;i<26;i++){ auto m = pow(v[i], K[i]); for(int j=0;j<16;j++){ if(res[j] < -INF) continue; if(m[C[i]-1][j] == -INF){ res[j] = -INF-1; } else { res[j] += m[C[i]-1][j]; } } } long long best = -INF; for(auto& t : res) best = max(t, best); if(best == -INF){ cout << "Impossible" << endl; } else { cout << best << endl; } return true; } int main(){ while(solve()); }