結果

問題 No.1288 yuki collection
ユーザー IKyoproIKyopro
提出日時 2020-12-03 14:28:30
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 3,071 bytes
コンパイル時間 2,418 ms
コンパイル使用メモリ 215,212 KB
実行使用メモリ 4,916 KB
最終ジャッジ日時 2023-10-11 23:57:54
合計ジャッジ時間 18,635 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,352 KB
testcase_01 AC 2 ms
4,352 KB
testcase_02 AC 2 ms
4,352 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,352 KB
testcase_05 AC 2 ms
4,352 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,352 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,352 KB
testcase_10 AC 3 ms
4,348 KB
testcase_11 AC 2 ms
4,352 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 424 ms
4,620 KB
testcase_14 AC 427 ms
4,664 KB
testcase_15 AC 346 ms
4,636 KB
testcase_16 AC 340 ms
4,672 KB
testcase_17 AC 436 ms
4,712 KB
testcase_18 AC 440 ms
4,724 KB
testcase_19 AC 437 ms
4,660 KB
testcase_20 AC 436 ms
4,684 KB
testcase_21 AC 439 ms
4,744 KB
testcase_22 AC 446 ms
4,660 KB
testcase_23 AC 444 ms
4,680 KB
testcase_24 AC 449 ms
4,628 KB
testcase_25 AC 432 ms
4,856 KB
testcase_26 AC 433 ms
4,796 KB
testcase_27 AC 229 ms
4,704 KB
testcase_28 AC 261 ms
4,736 KB
testcase_29 AC 212 ms
4,816 KB
testcase_30 AC 19 ms
4,784 KB
testcase_31 AC 31 ms
4,916 KB
testcase_32 AC 36 ms
4,912 KB
testcase_33 TLE -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T> using vec = vector<T>;
template <class T> using vvec = vector<vec<T>>;
template<class T> bool chmin(T& a,T b){if(a>b) {a = b; return true;} return false;}
template<class T> bool chmax(T& a,T b){if(a<b) {a = b; return true;} return false;}
#define rep(i,n) for(int i=0;i<(n);i++)
#define drep(i,n) for(int i=(n)-1;i>=0;i--)
#define all(x) (x).begin(),(x).end()
#define debug(x)  cerr << #x << " = " << (x) << endl;

typedef pair<ll,ll> P;
const ll inf = 1e17;

class min_cost_flow{
private:
    int N;
    struct edge{int to; ll cap,cost; int rev; bool is_rev;};
    vector<vector<edge>> G;
    vector<ll> h,dist,prevv,preve;
public:
    min_cost_flow(int n){
        N = n;
        G = vector<vector<edge>>(N);
        h = dist = prevv = preve = vector<ll>(N,0);
    }
    void add_edge(int from,int to,ll cap,ll cost){
        G[from].push_back((edge){to,cap,cost,(int) G[to].size(),false});
        G[to].push_back((edge){from,0,-cost,(int) G[from].size()-1,true});
    }
    ll answer(int s,int t,ll f){
        ll res = 0;
        fill(h.begin(),h.end(),0);
        while(f>0){
            priority_queue<P,vector<P>,greater<P>> Q;
            fill(dist.begin(),dist.end(),inf);
            dist[s] = 0;
            Q.push(P(0,s));
            while(!Q.empty()){
                P p = Q.top(); Q.pop();
                int v = p.second;
                if(dist[v]<p.first) continue;
                for(int i=0;i<G[v].size();i++){
                    edge &e = G[v][i];
                    if(e.cap>0 && dist[e.to]>dist[v]+e.cost+h[v]-h[e.to]){
                        dist[e.to] = dist[v]+e.cost+h[v]-h[e.to];
                        prevv[e.to] = v;
                        preve[e.to] = i;
                        Q.push(P(dist[e.to],e.to));
                    }
                }
            }
            if(dist[t]==inf) return -1;
            for(int v=0;v<N;v++) h[v] += dist[v];
            ll d = f;
            for(int v=t;v!=s; v=prevv[v]){
                d = min(d,G[prevv[v]][preve[v]].cap);
            }
            f -= d;
            res += d*h[t];
            for(int v=t;v!=s;v=prevv[v]){
                edge &e = G[prevv[v]][preve[v]];
                e.cap -= d;
                G[v][e.rev].cap += d;
            }
        }
        return res;
    }
};

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N;
    cin >> N;
    string S;
    cin >> S;
    vec<ll> A(N);
    rep(i,N) cin >> A[i];
    min_cost_flow flow(4*N+2);
    string yuki = "yuki";
    int s = 4*N,t = 4*N+1;
    auto id = [&](int i,int j){
        return 4*i+j;
    };
    rep(i,N){
        rep(j,4){
            if(!j) flow.add_edge(s,id(i,j),1,0);
            if(i+1<N) flow.add_edge(id(i,j),id(i+1,j),inf,0);
            if(S[i]==yuki[j]){
                if(j<3) flow.add_edge(id(i,j),id(i,j+1),1,-A[i]);
                else flow.add_edge(id(i,j),t,1,-A[i]);
            }
        }
    }
    flow.add_edge(s,t,inf,0);
    cout << -flow.answer(s,t,N) << "\n";
}
0