結果

問題 No.1288 yuki collection
ユーザー m_tsubasam_tsubasa
提出日時 2020-11-13 23:48:49
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 368 ms / 5,000 ms
コード長 2,702 bytes
コンパイル時間 2,606 ms
コンパイル使用メモリ 223,136 KB
実行使用メモリ 4,420 KB
最終ジャッジ日時 2023-09-30 04:22:17
合計ジャッジ時間 10,526 ms
ジャッジサーバーID
(参考情報)
judge12 / 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 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 283 ms
4,376 KB
testcase_14 AC 279 ms
4,376 KB
testcase_15 AC 224 ms
4,376 KB
testcase_16 AC 224 ms
4,380 KB
testcase_17 AC 284 ms
4,380 KB
testcase_18 AC 289 ms
4,376 KB
testcase_19 AC 288 ms
4,380 KB
testcase_20 AC 292 ms
4,376 KB
testcase_21 AC 288 ms
4,376 KB
testcase_22 AC 289 ms
4,380 KB
testcase_23 AC 289 ms
4,376 KB
testcase_24 AC 290 ms
4,376 KB
testcase_25 AC 283 ms
4,380 KB
testcase_26 AC 289 ms
4,376 KB
testcase_27 AC 132 ms
4,380 KB
testcase_28 AC 152 ms
4,376 KB
testcase_29 AC 138 ms
4,380 KB
testcase_30 AC 20 ms
4,376 KB
testcase_31 AC 25 ms
4,376 KB
testcase_32 AC 27 ms
4,420 KB
testcase_33 AC 247 ms
4,376 KB
testcase_34 AC 298 ms
4,380 KB
testcase_35 AC 283 ms
4,376 KB
testcase_36 AC 368 ms
4,376 KB
testcase_37 AC 360 ms
4,380 KB
testcase_38 AC 189 ms
4,380 KB
testcase_39 AC 187 ms
4,380 KB
testcase_40 AC 16 ms
4,392 KB
testcase_41 AC 1 ms
4,376 KB
testcase_42 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

template <class T>
struct Primal_Dual {
  using Pa = pair<T, int>;
  long long infinity = (long long)(1e16);
  struct edge {
    int to;
    T cap, cost;
    int rev;
  };
  int v;
  vector<vector<edge>> edges;
  vector<T> h;
  vector<T> dist;
  vector<int> prevv, preve;
  Primal_Dual(int vsize = 1) {
    v = vsize;
    edges.resize(v);
    h.resize(v);
    dist.resize(v);
    prevv.resize(v);
    preve.resize(v);
  }
  bool add(int from, int to, T cap, T cost) {
    edges[from].push_back((edge){to, cap, cost, (int)edges[to].size()});
    edges[to].push_back((edge){from, 0, -cost, (int)edges[from].size() - 1});
    return 1;
  }
  T solve(int s, int t, T f) {
    T ans = 0;
    h.assign(v, 0);
    while (f > 0) {
      priority_queue<Pa, vector<Pa>, greater<Pa>> qu;
      dist.assign(v, infinity);
      dist[s] = 0;
      qu.push({0, s});
      while (!qu.empty()) {
        Pa now = qu.top();
        qu.pop();
        int nowv = now.second;
        if (dist[nowv] < now.first) continue;
        for (int i = 0; i < (int)edges[nowv].size(); ++i) {
          edge &e = edges[nowv][i];
          if (e.cap > 0 &&
              dist[e.to] > dist[nowv] + e.cost + h[nowv] - h[e.to]) {
            dist[e.to] = dist[nowv] + e.cost + h[nowv] - h[e.to];
            prevv[e.to] = nowv;
            preve[e.to] = i;
            qu.push({dist[e.to], e.to});
          }
        }
      }
      if (dist[t] == infinity) return -1;
      for (int i = 0; i < v; ++i) h[i] += dist[i];
      T d = f;
      for (int i = t; i != s; i = prevv[i])
        d = min(d, edges[prevv[i]][preve[i]].cap);
      f -= d;
      ans += d * h[t];
      for (int i = t; i != s; i = prevv[i]) {
        edge &e = edges[prevv[i]][preve[i]];
        e.cap -= d;
        edges[i][e.rev].cap += d;
      }
    }
    return ans;
  }
};

int n;
vector<int> v;
string s;
map<char, int> mp;

long long solve();

int main() {
  for (int i = 0; i < 4; ++i) mp["yuki"[i]] = i;
  cin >> n >> s;
  v.resize(n);
  for (auto &p : v) cin >> p;
  cout << solve() << endl;
  return 0;
}

long long solve() {
  Primal_Dual<long long> pd(2 * n + 2);
  for (int i = 0; i < n; ++i) {
    pd.add(i, i + n, 1, (int)1e9 - v[i]);
    if (s[i] == 'y') pd.add(2 * n, i, 1, 0);
    if (s[i] == 'i') pd.add(i + n, 2 * n + 1, 1, 0);
    for (int j = i + 1; j < n; ++j)
      if (mp[s[i]] + 1 == mp[s[j]]) {
        pd.add(i + n, j, 1, 0);
        break;
      }
    for (int j = i + 1; j < n; ++j)
      if (mp[s[i]] == mp[s[j]]) {
        pd.add(i, j, n + 1, 0);
        break;
      }
  }
  pd.add(2 * n, 2 * n + 1, n / 4, 4e9);
  return 4LL * (n / 4) * (int)1e9 - pd.solve(2 * n, 2 * n + 1, n / 4);
}
0