結果

問題 No.3561 Collect KCPC
コンテスト
ユーザー T1610
提出日時 2026-07-23 23:29:31
言語 C++23
(gcc 15.2.0 + boost 1.90.0)
コンパイル:
g++-15 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 3,822 ms / 6,000 ms
+ 260µs
コード長 2,759 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 4,953 ms
コンパイル使用メモリ 387,708 KB
実行使用メモリ 25,136 KB
最終ジャッジ日時 2026-07-23 23:30:21
合計ジャッジ時間 44,132 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
サブタスク 配点 結果
部分点1 10 % AC * 15
部分点2 20 % AC * 15
部分点3 20 % AC * 13
部分点4 50 % AC * 51
合計 100 点
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <atcoder/all>
#include <bits/stdc++.h>
using namespace std;
using namespace atcoder;
#define rep(i, n) REP(i, 0, n)
#define REP(i, s, e) for (int i = (s); i < (int)(e); i++)
#define repr(i, n) REPR(i, n, 0)
#define REPR(i, s, e) for (int i = (int)(s - 1); i >= (int)(e); i--)
#define all(r) r.begin(), r.end()
#define rall(r) r.rbegin(), r.rend()

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;

template <typename T, typename U>
T chmax(T& a, const U& b) {
    if (a >= b) return false;
    a = b;
    return true;
}
template <typename T, typename U>
T chmin(T& a, const U& b) {
    if (a <= b) return false;
    a = b;
    return true;
}

void yes_no(bool f, string yes = "Yes", string no = "No") { cout << (f ? yes : no) << "\n"; }

unsigned int randxor() {
    static unsigned int x = 123456789, y = 362436069, z = 521288629, w = 88675123;
    unsigned int t;
    t = (x ^ (x << 11));
    x = y;
    y = z;
    z = w;
    return (w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)));
}

void solve() {
    int n, m;
    cin >> n >> m;
    using P = pair<ll, ll>;
    vector<vector<P>> es(n);
    rep(i, m) {
        int a, b, c;
        cin >> a >> b >> c;
        --a;
        --b;
        es[a].emplace_back(b, c);
    }
    string s;
    cin >> s;
    const int ma = 5;
    const ll inf = 1e18;
    auto calc = [&](const string s) -> ll {
        vector d(n, vl(ma, inf));
        d[0][0] = 0;
        using T = tuple<ll, ll, ll>;
        priority_queue<T, vector<T>, greater<T>> q;
        q.emplace(0, 0, 0);
        while (q.size()) {
            auto [v, cur, dig] = q.top();
            q.pop();
            if (d[cur][dig] != v) continue;
            for (auto&& [nxt, cost] : es[cur]) {
                ll ncost = cost + v;
                if (d[nxt][dig] > ncost) {
                    d[nxt][dig] = ncost;
                    q.emplace(ncost, nxt, dig);
                }
            }
            if ((dig == 0 && s[cur] == 'K') || (dig == 1 && s[cur] == 'C') || (dig == 2 && s[cur] == 'P') || (dig == 3 && s[cur] == 'c')) {
                int ndig = dig + 1;
                if (d[cur][ndig] > v) {
                    d[cur][ndig] = v;
                    q.emplace(v, cur, ndig);
                }
            }
        }
        ll ans = inf;
        rep(i, n) chmin(ans, d[i][ma - 1]);
        return ans;
    };
    ll ans = inf;
    rep(_, 30) {
        string t = s;
        rep(i, n) if (t[i] == 'C') {
            if (randxor() & 1) {
                t[i] = 'c';
            }
        }
        chmin(ans, calc(t));
    }
    if (ans == inf) ans = -1;

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

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int t = 1;
    // cin >> t;
    rep(ti, t) solve();
    return 0;
}
0