結果
問題 | No.1288 yuki collection |
ユーザー | どらら |
提出日時 | 2020-11-14 13:46:40 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 1,961 ms / 5,000 ms |
コード長 | 4,819 bytes |
コンパイル時間 | 2,166 ms |
コンパイル使用メモリ | 183,600 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-07-22 22:53:38 |
合計ジャッジ時間 | 28,102 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,376 KB |
testcase_02 | AC | 2 ms
5,376 KB |
testcase_03 | AC | 2 ms
5,376 KB |
testcase_04 | AC | 2 ms
5,376 KB |
testcase_05 | AC | 2 ms
5,376 KB |
testcase_06 | AC | 2 ms
5,376 KB |
testcase_07 | AC | 2 ms
5,376 KB |
testcase_08 | AC | 3 ms
5,376 KB |
testcase_09 | AC | 2 ms
5,376 KB |
testcase_10 | AC | 4 ms
5,376 KB |
testcase_11 | AC | 3 ms
5,376 KB |
testcase_12 | AC | 3 ms
5,376 KB |
testcase_13 | AC | 1,081 ms
5,376 KB |
testcase_14 | AC | 1,470 ms
5,376 KB |
testcase_15 | AC | 735 ms
5,376 KB |
testcase_16 | AC | 818 ms
5,376 KB |
testcase_17 | AC | 1,100 ms
5,376 KB |
testcase_18 | AC | 1,240 ms
5,376 KB |
testcase_19 | AC | 880 ms
5,376 KB |
testcase_20 | AC | 1,024 ms
5,376 KB |
testcase_21 | AC | 914 ms
5,376 KB |
testcase_22 | AC | 960 ms
5,376 KB |
testcase_23 | AC | 1,118 ms
5,376 KB |
testcase_24 | AC | 894 ms
5,376 KB |
testcase_25 | AC | 772 ms
5,376 KB |
testcase_26 | AC | 1,124 ms
5,376 KB |
testcase_27 | AC | 1,584 ms
5,376 KB |
testcase_28 | AC | 1,494 ms
5,376 KB |
testcase_29 | AC | 686 ms
5,376 KB |
testcase_30 | AC | 821 ms
5,376 KB |
testcase_31 | AC | 762 ms
5,376 KB |
testcase_32 | AC | 813 ms
5,376 KB |
testcase_33 | AC | 336 ms
5,376 KB |
testcase_34 | AC | 1,345 ms
5,376 KB |
testcase_35 | AC | 1,961 ms
5,376 KB |
testcase_36 | AC | 31 ms
5,376 KB |
testcase_37 | AC | 581 ms
5,376 KB |
testcase_38 | AC | 14 ms
5,376 KB |
testcase_39 | AC | 14 ms
5,376 KB |
testcase_40 | AC | 366 ms
5,376 KB |
testcase_41 | AC | 2 ms
5,376 KB |
testcase_42 | AC | 2 ms
5,376 KB |
ソースコード
#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 CostScalingMinCostFlow { static constexpr double alpha = 2; public: struct Node { int b; double p; int in_f, out_f; Node() : b(0), p(0), in_f(0), out_f(0) {} Node(int b, double p) : b(b), p(p), in_f(0), out_f(0) {} }; struct Edge { int from, to; int cap, cost; int rev; int f; bool isrev; Edge() : from(-1), to(-1), cap(0), cost(0), rev(-1), f(0), isrev(false) {} Edge(int from, int to, int cap, int cost, int rev, int f, bool isrev) : from(from), to(to), cap(cap), cost(cost), rev(rev), f(f), isrev(isrev) {} }; private: std::vector<Node> nodes; std::vector<std::vector<Edge>> G; double epsilon; std::queue<int> active_nodes; int residual_cap(const Edge& e) const { if (!e.isrev) return e.cap - e.f; else return G[e.to][e.rev].f; } double reduced_cost(const Edge& e) const { return e.cost + nodes[e.from].p - nodes[e.to].p; } int excess(int i) const { return nodes[i].b - nodes[i].out_f + nodes[i].in_f; } bool is_active(int i) const { return excess(i) > 0; } void push(Edge& edge, int delta) { if (!edge.isrev) { edge.f += delta; nodes[edge.from].out_f += delta; nodes[edge.to].in_f += delta; } else { G[edge.to][edge.rev].f -= delta; nodes[edge.to].out_f -= delta; nodes[edge.from].in_f -= delta; } } void relabel(int v) { double mx = std::numeric_limits<double>::lowest(); for (Edge& edge : G[v]) { if (residual_cap(edge) > 0) { mx = std::max(mx, nodes[edge.to].p - edge.cost - epsilon); } } nodes[v].p = mx; } void refine() { for (auto& edges : G) { for (Edge& edge : edges) { // if (edge.isrev) continue; if (reduced_cost(edge) >= 0) continue; if (residual_cap(edge) <= 0) continue; push(edge, residual_cap(edge)); } } for (int i = 0; i < nodes.size(); i++) { if (is_active(i)) active_nodes.push(i); } while (!active_nodes.empty()) { int v = active_nodes.front(); active_nodes.pop(); if (!is_active(v)) continue; bool is_pushed = false; for (Edge& edge : G[v]) { if (reduced_cost(edge) >= 0) continue; if (residual_cap(edge) <= 0) continue; push(edge, std::min(residual_cap(edge), excess(v))); if (is_active(edge.from)) active_nodes.push(edge.from); if (is_active(edge.to)) active_nodes.push(edge.to); is_pushed = true; break; } if (!is_pushed) { relabel(v); if (is_active(v)) active_nodes.push(v); } } } public: CostScalingMinCostFlow(int N) : nodes(N), G(N), epsilon(0) {} void add_edge(int from, int to, int cap, int cost) { epsilon = std::max(epsilon, (double)abs(cost)); G[from].emplace_back(from, to, cap, cost, G[to].size(), 0, false); G[to].emplace_back(to, from, cap, -cost, G[from].size() - 1, cap, true); } void set_b(int i, int b) { nodes[i].b = b; } long long mincostflow() { int N = nodes.size(); while (epsilon >= 1.0 / N) { epsilon /= alpha; refine(); } long long ret = 0; for (auto& edges : G) { for (Edge& edge : edges) { if (edge.isrev) continue; ret += edge.cost * edge.f; } } 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); } CostScalingMinCostFlow mcf(N+2); int s = N; int t = s+1; bool is_first = true; rep(i,N){ if(is_first && S[i] == 'y'){ mcf.add_edge(s, i, N, 0); is_first = false; } if(S[i] == 'i'){ mcf.add_edge(i, t, 1, -v[i]); } bool flg1 = true; bool flg2 = true; bool flg3 = true; bool flg4 = true; REP(j,i+1,N){ if(flg1 && S[i] == S[j]){ mcf.add_edge(i, j, N, 0); flg1 = false; } if(flg2 && S[i] == 'y' && S[j] == 'u'){ mcf.add_edge(i, j, 1, -v[i]); flg2 = false; } if(flg3 && S[i] == 'u' && S[j] == 'k'){ mcf.add_edge(i, j, 1, -v[i]); flg3 = false; } if(flg4 && S[i] == 'k' && S[j] == 'i'){ mcf.add_edge(i, j, 1, -v[i]); flg4 = false; } } } mcf.add_edge(s, t, 10000, 0); int f = 500; cerr << f << endl; mcf.set_b(s, f); mcf.set_b(t, -f); cout << -mcf.mincostflow() << endl; return 0; }