結果

問題 No.1288 yuki collection
ユーザー m_tsubasam_tsubasa
提出日時 2020-11-13 23:13:13
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,530 bytes
コンパイル時間 2,684 ms
コンパイル使用メモリ 222,976 KB
実行使用メモリ 47,164 KB
最終ジャッジ日時 2023-09-30 04:12:22
合計ジャッジ時間 54,023 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
8,752 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 3 ms
4,384 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 AC 2,720 ms
34,576 KB
testcase_14 AC 2,714 ms
34,528 KB
testcase_15 AC 1,891 ms
29,124 KB
testcase_16 AC 1,922 ms
29,112 KB
testcase_17 AC 2,683 ms
33,944 KB
testcase_18 AC 2,640 ms
34,600 KB
testcase_19 AC 2,687 ms
33,996 KB
testcase_20 AC 2,805 ms
35,180 KB
testcase_21 AC 3,633 ms
47,164 KB
testcase_22 AC 3,680 ms
46,884 KB
testcase_23 AC 3,668 ms
46,596 KB
testcase_24 AC 2,954 ms
35,044 KB
testcase_25 AC 2,760 ms
35,088 KB
testcase_26 AC 2,805 ms
35,112 KB
testcase_27 AC 804 ms
20,500 KB
testcase_28 AC 1,202 ms
28,420 KB
testcase_29 AC 1,259 ms
31,020 KB
testcase_30 AC 123 ms
32,348 KB
testcase_31 AC 168 ms
33,532 KB
testcase_32 AC 175 ms
32,736 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;

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);
  }
  pd.add(2 * n, 2 * n + 1, n, 4e9);
  return 4LL * n * (int)1e9 - pd.solve(2 * n, 2 * n + 1, n);
}
0