結果
問題 | No.1288 yuki collection |
ユーザー | leaf_1415 |
提出日時 | 2020-11-13 21:54:36 |
言語 | C++11 (gcc 11.4.0) |
結果 |
AC
|
実行時間 | 237 ms / 5,000 ms |
コード長 | 3,387 bytes |
コンパイル時間 | 1,324 ms |
コンパイル使用メモリ | 93,216 KB |
実行使用メモリ | 6,944 KB |
最終ジャッジ日時 | 2024-07-22 20:46:12 |
合計ジャッジ時間 | 6,219 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,812 KB |
testcase_01 | AC | 2 ms
6,816 KB |
testcase_02 | AC | 2 ms
6,940 KB |
testcase_03 | AC | 2 ms
6,940 KB |
testcase_04 | AC | 2 ms
6,940 KB |
testcase_05 | AC | 2 ms
6,944 KB |
testcase_06 | AC | 2 ms
6,944 KB |
testcase_07 | AC | 2 ms
6,940 KB |
testcase_08 | AC | 3 ms
6,940 KB |
testcase_09 | AC | 2 ms
6,940 KB |
testcase_10 | AC | 2 ms
6,944 KB |
testcase_11 | AC | 2 ms
6,940 KB |
testcase_12 | AC | 2 ms
6,940 KB |
testcase_13 | AC | 188 ms
6,944 KB |
testcase_14 | AC | 186 ms
6,940 KB |
testcase_15 | AC | 150 ms
6,944 KB |
testcase_16 | AC | 148 ms
6,944 KB |
testcase_17 | AC | 188 ms
6,940 KB |
testcase_18 | AC | 194 ms
6,940 KB |
testcase_19 | AC | 192 ms
6,940 KB |
testcase_20 | AC | 192 ms
6,944 KB |
testcase_21 | AC | 186 ms
6,940 KB |
testcase_22 | AC | 184 ms
6,940 KB |
testcase_23 | AC | 185 ms
6,940 KB |
testcase_24 | AC | 196 ms
6,940 KB |
testcase_25 | AC | 194 ms
6,944 KB |
testcase_26 | AC | 191 ms
6,944 KB |
testcase_27 | AC | 83 ms
6,940 KB |
testcase_28 | AC | 98 ms
6,944 KB |
testcase_29 | AC | 87 ms
6,940 KB |
testcase_30 | AC | 7 ms
6,944 KB |
testcase_31 | AC | 9 ms
6,944 KB |
testcase_32 | AC | 10 ms
6,944 KB |
testcase_33 | AC | 185 ms
6,940 KB |
testcase_34 | AC | 226 ms
6,944 KB |
testcase_35 | AC | 217 ms
6,944 KB |
testcase_36 | AC | 237 ms
6,940 KB |
testcase_37 | AC | 234 ms
6,940 KB |
testcase_38 | AC | 76 ms
6,940 KB |
testcase_39 | AC | 77 ms
6,944 KB |
testcase_40 | AC | 3 ms
6,944 KB |
testcase_41 | AC | 2 ms
6,944 KB |
testcase_42 | AC | 2 ms
6,944 KB |
ソースコード
#include <iostream> #include <cstdio> #include <cmath> #include <ctime> #include <cstdlib> #include <cassert> #include <vector> #include <list> #include <stack> #include <queue> #include <deque> #include <map> #include <set> #include <bitset> #include <string> #include <algorithm> #include <utility> #define rep(x, s, t) for(llint (x) = (s); (x) <= (t); (x)++) #define chmin(x, y) (x) = min((x), (y)) #define chmax(x, y) (x) = max((x), (y)) #define all(x) (x).begin(),(x).end() #define inf 1e18 using namespace std; typedef long long llint; typedef pair<llint, llint> P; struct edge{ llint to, cap, cost, rev; edge(){} edge(llint a, llint b, llint c, llint d){ to = a, cap = b, cost = c, rev = d; } }; llint n; string s; llint a[2005]; llint S, T; vector<edge> G[4005]; llint dist[4005]; llint prevv[4005], preve[4005]; llint h[4005]; llint succ[2005][4]; void BellmanFord() { for(int i = 0; i <= T; i++) dist[i] = inf; dist[S] = 0, prevv[S] = -1; bool update = true; while(update){ update = false; for(int i = 0; i <= T; i++){ for(int j = 0; j < G[i].size(); j++){ if(G[i][j].cap == 0) continue; if(dist[G[i][j].to] > dist[i] + G[i][j].cost){ dist[G[i][j].to] = dist[i] + G[i][j].cost; prevv[G[i][j].to] = i; preve[G[i][j].to] = j; update = true; } } } } } void Dijkstra() { for(int i = 0; i <= T; i++) dist[i] = inf; dist[S] = 0, prevv[S] = -1; priority_queue< P, vector<P>, greater<P> > Q; Q.push( make_pair(0, S) ); llint v, d; while(Q.size()){ d = Q.top().first; v = Q.top().second; Q.pop(); if(dist[v] < d) continue; for(int i = 0; i < G[v].size(); i++){ if(G[v][i].cap == 0) continue; llint u = G[v][i].to, c = h[v] - h[u] + G[v][i].cost; if(dist[u] > d + c){ dist[u] = d + c; prevv[u] = v; preve[u] = i; Q.push( make_pair(dist[u], u) ); } } } } void add_edge(llint from, llint to, llint cap, llint cost) { G[from].push_back( edge(to, cap, cost, G[to].size()) ); G[to].push_back( edge(from, 0, -cost, G[from].size()-1) ); } int main(void) { ios::sync_with_stdio(0); cin.tie(0); cin >> n >> s; rep(i, 1, n) cin >> a[i]; s = "#" + s; rep(i, 1, n){ if(s[i] == 'y') s[i] = '0'; if(s[i] == 'u') s[i] = '1'; if(s[i] == 'k') s[i] = '2'; if(s[i] == 'i') s[i] = '3'; } rep(i, 0, 3) succ[n][i] = n+1; for(int i = n-1; i >= 1; i--){ rep(j, 0, 3) succ[i][j] = succ[i+1][j]; succ[i][s[i+1]-'0'] = i+1; } S = 2*n+1, T = 2*n+2; rep(i, 1, n){ llint c = s[i]-'0'; if(c < 3){ if(succ[i][c+1] <= n) add_edge(i, n+succ[i][c+1], 1, -a[i]); } else add_edge(i, T, 1, -a[i]); if(s[i] == '0') add_edge(S, i, 1, 0); add_edge(n+i, i, 1, 0); if(succ[i][c] <= n) add_edge(n+i, n+succ[i][c], inf, 0); } add_edge(S, T, inf, 0); BellmanFord(); for(int i = 0; i <= T; i++) h[i] = dist[i]; llint f = n, ans = 0; while(f > 0){ Dijkstra(); if(dist[T] >= inf) break; llint p = T, flow = f; while(prevv[p] != -1){ flow = min(flow, G[prevv[p]][preve[p]].cap); p = prevv[p]; } p = T; while(prevv[p] != -1){ G[prevv[p]][preve[p]].cap -= flow; G[p][G[prevv[p]][preve[p]].rev].cap += flow; p = prevv[p]; } f -= flow; ans += (dist[T] + h[T] - h[S]) * flow; for(int i = 0; i <= T; i++) h[i] += dist[i]; //オーバーフローに注意(?) } if(f > 0) ans = -1; cout << -ans << endl; return 0; }