結果
問題 | No.1288 yuki collection |
ユーザー | どらら |
提出日時 | 2020-11-14 13:41:56 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 1,467 ms / 5,000 ms |
コード長 | 7,076 bytes |
コンパイル時間 | 2,664 ms |
コンパイル使用メモリ | 191,392 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-07-22 22:52:29 |
合計ジャッジ時間 | 26,484 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
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 | 3 ms
5,376 KB |
testcase_07 | AC | 2 ms
5,376 KB |
testcase_08 | AC | 3 ms
5,376 KB |
testcase_09 | AC | 3 ms
5,376 KB |
testcase_10 | AC | 3 ms
5,376 KB |
testcase_11 | AC | 2 ms
5,376 KB |
testcase_12 | AC | 3 ms
5,376 KB |
testcase_13 | AC | 929 ms
5,376 KB |
testcase_14 | AC | 1,000 ms
5,376 KB |
testcase_15 | AC | 653 ms
5,376 KB |
testcase_16 | AC | 774 ms
5,376 KB |
testcase_17 | AC | 914 ms
5,376 KB |
testcase_18 | AC | 1,192 ms
5,376 KB |
testcase_19 | AC | 831 ms
5,376 KB |
testcase_20 | AC | 959 ms
5,376 KB |
testcase_21 | AC | 973 ms
5,376 KB |
testcase_22 | AC | 1,012 ms
5,376 KB |
testcase_23 | AC | 1,161 ms
5,376 KB |
testcase_24 | AC | 934 ms
5,376 KB |
testcase_25 | AC | 837 ms
5,376 KB |
testcase_26 | AC | 1,166 ms
5,376 KB |
testcase_27 | AC | 1,179 ms
5,376 KB |
testcase_28 | AC | 950 ms
5,376 KB |
testcase_29 | AC | 672 ms
5,376 KB |
testcase_30 | AC | 765 ms
5,376 KB |
testcase_31 | AC | 711 ms
5,376 KB |
testcase_32 | AC | 692 ms
5,376 KB |
testcase_33 | AC | 379 ms
5,376 KB |
testcase_34 | AC | 1,354 ms
5,376 KB |
testcase_35 | AC | 1,467 ms
5,376 KB |
testcase_36 | AC | 59 ms
5,376 KB |
testcase_37 | AC | 665 ms
5,376 KB |
testcase_38 | AC | 47 ms
5,376 KB |
testcase_39 | AC | 48 ms
5,376 KB |
testcase_40 | AC | 81 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 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; } }; 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); } Dinic dinic(N+2); 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); dinic.add_edge(s, i, N); is_first = false; } if(S[i] == 'i'){ mcf.add_edge(i, t, 1, -v[i]); dinic.add_edge(i, t, 1); } 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); dinic.add_edge(i, j, N); flg1 = false; } if(flg2 && S[i] == 'y' && S[j] == 'u'){ mcf.add_edge(i, j, 1, -v[i]); dinic.add_edge(i, j, 1); flg2 = false; } if(flg3 && S[i] == 'u' && S[j] == 'k'){ mcf.add_edge(i, j, 1, -v[i]); dinic.add_edge(i, j, 1); flg3 = false; } if(flg4 && S[i] == 'k' && S[j] == 'i'){ mcf.add_edge(i, j, 1, -v[i]); dinic.add_edge(i, j, 1); flg4 = false; } } } mcf.add_edge(s, t, 10000, 0); int f = dinic.max_flow(s, t); cerr << f << endl; mcf.set_b(s, f); mcf.set_b(t, -f); cout << -mcf.mincostflow() << endl; return 0; }