結果

問題 No.1780 [Cherry Anniversary] 真冬に咲く26の櫻の木
ユーザー pes_magicpes_magic
提出日時 2021-12-09 18:50:02
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 220 ms / 2,000 ms
コード長 2,353 bytes
コンパイル時間 826 ms
コンパイル使用メモリ 80,552 KB
実行使用メモリ 4,500 KB
最終ジャッジ日時 2023-09-24 10:36:16
合計ジャッジ時間 4,869 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 5 ms
4,380 KB
testcase_03 AC 4 ms
4,380 KB
testcase_04 AC 4 ms
4,380 KB
testcase_05 AC 4 ms
4,380 KB
testcase_06 AC 4 ms
4,376 KB
testcase_07 AC 4 ms
4,376 KB
testcase_08 AC 4 ms
4,376 KB
testcase_09 AC 5 ms
4,376 KB
testcase_10 AC 4 ms
4,376 KB
testcase_11 AC 4 ms
4,376 KB
testcase_12 AC 4 ms
4,376 KB
testcase_13 AC 4 ms
4,376 KB
testcase_14 AC 41 ms
4,380 KB
testcase_15 AC 163 ms
4,376 KB
testcase_16 AC 163 ms
4,380 KB
testcase_17 AC 27 ms
4,380 KB
testcase_18 AC 103 ms
4,500 KB
testcase_19 AC 36 ms
4,380 KB
testcase_20 AC 120 ms
4,380 KB
testcase_21 AC 207 ms
4,376 KB
testcase_22 AC 212 ms
4,380 KB
testcase_23 AC 53 ms
4,380 KB
testcase_24 AC 220 ms
4,376 KB
testcase_25 AC 48 ms
4,380 KB
testcase_26 AC 38 ms
4,376 KB
testcase_27 AC 104 ms
4,376 KB
testcase_28 AC 23 ms
4,380 KB
testcase_29 AC 18 ms
4,376 KB
testcase_30 AC 199 ms
4,376 KB
testcase_31 AC 10 ms
4,380 KB
testcase_32 AC 18 ms
4,376 KB
testcase_33 AC 90 ms
4,380 KB
testcase_34 AC 1 ms
4,380 KB
testcase_35 AC 8 ms
4,376 KB
testcase_36 AC 1 ms
4,380 KB
testcase_37 AC 8 ms
4,376 KB
testcase_38 AC 8 ms
4,376 KB
testcase_39 AC 9 ms
4,380 KB
testcase_40 AC 8 ms
4,376 KB
testcase_41 AC 8 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++){
                if(a[i][k] == -INF || b[k][j] == -INF) continue;
                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);
    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;
        --c;
    }
    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;
        --A; --B;
        for(auto& s : S){
            if(v[s-'A'][A][B] < E){
                v[s-'A'][A][B] = v[s-'A'][B][A] = 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]][j] == -INF){
                res[j] = -INF-1;
            } else {
                res[j] += m[C[i]][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