結果

問題 No.1288 yuki collection
ユーザー どららどらら
提出日時 2020-11-13 23:39:06
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 5,170 bytes
コンパイル時間 2,234 ms
コンパイル使用メモリ 186,484 KB
実行使用メモリ 8,760 KB
最終ジャッジ日時 2023-09-30 04:20:37
合計ジャッジ時間 9,104 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 3 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 3 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 3 ms
4,376 KB
testcase_13 TLE -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
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;
#define REP(i,a,n) for(int i=(a); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)
#define FOR(it,c) for(__typeof((c).begin()) it=(c).begin(); it!=(c).end(); ++it)
#define ALLOF(c) (c).begin(), (c).end()
typedef long long ll;
typedef unsigned long long ull;

class Dinic {
  int MAX_V;
  int INF;
  struct edge{ int to, cap, rev, icap, flow; };

  vector< vector<edge> > G;
  vector<int> level; //sからの距離
  vector<int> iter; //どこまで調べたか

  void max_flow_bfs(int s){
    fill(level.begin(), level.end(), -1);
    queue<int> que;
    level[s] = 0;
    que.push(s);
    while(!que.empty()){
      int v = que.front(); que.pop();
      for(int i=0; i<G[v].size(); i++){
        edge &e = G[v][i];
        if(e.cap>0 && level[e.to]<0){
          level[e.to] = level[v] + 1;
          que.push(e.to);
        }
      }
    }
  }
  int max_flow_dfs(int v, int t, int f){
    if(v==t) return f;
    for(int &i=iter[v]; i<G[v].size(); i++){
      edge &e = G[v][i];
      if(e.cap>0 && level[v]<level[e.to]){
        int d = max_flow_dfs(e.to, t, min(f, e.cap));
        if(d>0){
          e.cap -= d;
          G[e.to][e.rev].cap += d;
          e.flow += d;
          return d;
        }
      }
    }
    return 0;
  }

public:
  Dinic(int N):MAX_V(N),G(N),level(N),iter(N){
    INF = 99999999;
  }

  void add_edge(int from, int to, int cap){
    G[from].push_back((edge){to, cap, (int)G[to].size(), cap, 0});
    G[to].push_back((edge){from, 0, (int)G[from].size()-1, 0, 0});
  }

  int get_flow(int from, int to){ //untried
    rep(i,G[from].size()){
      if(G[from][i].to == to){
        return G[from][i].flow;
      }
    }
    return -1;
  }

  int max_flow(int s, int t){
    int flow = 0;
    while(true){
      max_flow_bfs(s);
      if(level[t]<0) return flow;
      fill(iter.begin(), iter.end(), 0);
      int f;
      while((f = max_flow_dfs(s, t, INF))>0){
        flow += f;
      }
    }
  }

  int min_cut(int s, int t, vector<int>& S, vector<int>& T){
    S.clear();
    T.clear();

    int maxf = max_flow(s, t);
    for(int i=0; i<level.size(); i++){
      if(level[i] >= 0) S.push_back(i);
      else T.push_back(i);
    }

    return maxf;
  }
};



template<class F, class C>
class MinCostFlow {
  struct Edge {
    int rev, from, to;
    F cap, icap;
    C cost;
    Edge(int rev, int from, int to, F cap, C cost):
    rev(rev), from(from), to(to), cap(cap), icap(cap), cost(cost){}
  };

  int N;
  vector<vector<Edge>> G;
  const C INF;
public:
  MinCostFlow(int N):N(N),G(N),INF(numeric_limits<C>::max()){}

  void add_edge(int from, int to, F cap, C cost){
    G[from].emplace_back((int)(G[to].size()), from, to, cap, cost);
    G[to].emplace_back((int)(G[from].size()) - 1, to, from, 0, -cost);
  }
  
  C solve(int s, int t, F init_f){
    vector<C> dist(N);
    vector<int> prevv(N);
    vector<int> preve(N);

    C ret = 0;
    F f = init_f;
    while(f > 0){
      fill(dist.begin(), dist.end(), INF);
      dist[s] = 0;
      while(true){
        bool update = false;
        for(int v=0; v<G.size(); v++){
          if(dist[v] == INF) 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) {
              dist[e.to] = dist[v] + e.cost;
              prevv[e.to] = v;
              preve[e.to] = i;
              update = true;
            }
          }
        }
        if(!update) break;
      }
      if(dist[t] == INF) return 0;
      
      F d = f;
      for(int v=t; v!=s; v=prevv[v]){
        d = min(d, G[prevv[v]][preve[v]].cap);
      }
      f -= d;
      ret += dist[t] * d;
      for(int v=t; v!=s; v=prevv[v]){
        Edge& e = G[prevv[v]][preve[v]];
        Edge& re = (e.from != e.to) ? G[e.to][e.rev] : G[e.to][e.rev+1];
        e.cap -= d;
        re.cap += d;
      }
    }
    return ret;
  }

  vector<int> get_selected_edges(int i){
    vector<int> ret;
    for(int j=0; j<G[i].size(); j++){
      Edge& e = G[i][j];
      if(e.cap == 0 && e.icap == 1) ret.push_back(e.to);
    }
    return ret;
  }  
};


int main(){
  int N;
  cin >> N;
  string S;
  cin >> S;
  vector<ll> v;
  rep(i,N){
    ll a;
    cin >> a;
    v.push_back(a);
  }

  Dinic dinic(2*N+2);
  MinCostFlow<ll,ll> mcf(2*N+2);
  int s = 2*N;
  int t = s+1;

  rep(i,N){
    if(S[i] == 'y'){
      mcf.add_edge(s, i, 1, -v[i]);
      dinic.add_edge(s, i, 1);
    }
    mcf.add_edge(i, i+N, 1, 0);
    dinic.add_edge(i, i+N, 1);
    if(S[i] == 'i'){
      mcf.add_edge(i+N, t, 1, 0);
      dinic.add_edge(i+N, t, 1);
    }
    rep(j,N){
      if(S[i] == 'y' && S[j] == 'u'){
        if(i<j){
          mcf.add_edge(i+N, j, 1, -v[j]);
          dinic.add_edge(i+N, j, 1);
        }
      }
      if(S[i] == 'u' && S[j] == 'k'){
        if(i<j){
          mcf.add_edge(i+N, j, 1, -v[j]);
          dinic.add_edge(i+N, j, 1);
        }
      }
      if(S[i] == 'k' && S[j] == 'i'){
        if(i<j){
          mcf.add_edge(i+N, j, 1, -v[j]);
          dinic.add_edge(i+N, j, 1);
        }
      }
    }
  }

  int f = dinic.max_flow(s, t);
  cout << -mcf.solve(s, t, f) << endl;
  
  return 0;
}
0