結果

問題 No.1780 [Cherry Anniversary] 真冬に咲く26の櫻の木
ユーザー pes_magicpes_magic
提出日時 2021-12-09 17:19:53
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,267 bytes
コンパイル時間 886 ms
コンパイル使用メモリ 80,800 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-24 09:22:13
合計ジャッジ時間 4,865 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 3 ms
4,380 KB
testcase_04 AC 3 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 3 ms
4,376 KB
testcase_07 AC 3 ms
4,376 KB
testcase_08 AC 3 ms
4,376 KB
testcase_09 AC 3 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 WA -
testcase_12 AC 3 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 37 ms
4,380 KB
testcase_15 AC 162 ms
4,380 KB
testcase_16 AC 160 ms
4,376 KB
testcase_17 AC 24 ms
4,376 KB
testcase_18 AC 100 ms
4,376 KB
testcase_19 AC 33 ms
4,380 KB
testcase_20 AC 118 ms
4,380 KB
testcase_21 AC 206 ms
4,380 KB
testcase_22 AC 209 ms
4,376 KB
testcase_23 AC 50 ms
4,380 KB
testcase_24 AC 218 ms
4,380 KB
testcase_25 AC 46 ms
4,380 KB
testcase_26 AC 35 ms
4,380 KB
testcase_27 AC 101 ms
4,376 KB
testcase_28 AC 21 ms
4,376 KB
testcase_29 AC 15 ms
4,376 KB
testcase_30 AC 198 ms
4,380 KB
testcase_31 AC 8 ms
4,380 KB
testcase_32 AC 15 ms
4,376 KB
testcase_33 AC 87 ms
4,376 KB
testcase_34 AC 1 ms
4,380 KB
testcase_35 AC 6 ms
4,376 KB
testcase_36 AC 2 ms
4,376 KB
testcase_37 AC 5 ms
4,380 KB
testcase_38 WA -
testcase_39 AC 5 ms
4,376 KB
testcase_40 AC 6 ms
4,380 KB
testcase_41 AC 5 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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());
}
0