結果
問題 |
No.161 制限ジャンケン
|
ユーザー |
![]() |
提出日時 | 2015-03-14 20:54:35 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 6 ms / 5,000 ms |
コード長 | 3,227 bytes |
コンパイル時間 | 930 ms |
コンパイル使用メモリ | 86,380 KB |
実行使用メモリ | 6,820 KB |
最終ジャッジ日時 | 2024-11-30 04:30:55 |
合計ジャッジ時間 | 1,697 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 16 |
ソースコード
#include <iostream> #include <vector> #include <cstdio> #include <sstream> #include <map> #include <string> #include <algorithm> #include <queue> #include <cmath> #include <set> using namespace std; #define INF 1e9 typedef struct{ int to; int cap; int cost; int rev; }edge; //min cost : s->t (flow:f) //SPFA_fast version using SLF and LLL int min_cost_flow(vector<vector<edge> > &G, int s, int t, int f){ const int N = G.size(); int cost = 0; vector<int> prev_v(N,-1); vector<int> prev_e(N,-1); while(f>0){ //min distance(cost based) search with SPFA vector<int> dist(N, INF); vector<int> cnt(dist.size(), 0); long long sum = 0; dist[s] = 0; prev_v[s] = s; deque<int> Q; auto my_push = [&](int node){ //SLF if(Q.size() == 0 || dist[Q.front()] > dist[node]){ Q.push_front(node); }else{ Q.push_back(node); } cnt[node]++; }; auto my_pop = [&]() -> int{ int ret = Q.front(); cnt[ret]--; Q.pop_front(); return ret; }; auto adjust = [&](){ //LLL double ave = 1.0*sum / Q.size(); if(Q.size() <= 1) return; while( dist[Q.front()] > ave ){ Q.push_back( Q.front() ); Q.pop_front(); } }; my_push(s); while(!Q.empty()){ int pos = my_pop(); sum -= dist[pos]; for(int i=0; i<G[pos].size(); i++){ edge& E = G[pos][i]; if(dist[E.to] > dist[pos] + E.cost && E.cap > 0){ if(cnt[E.to] != 0){ sum -= dist[ E.to ]; } dist[E.to] = dist[pos] + E.cost; prev_v[ E.to ] = pos; prev_e[ E.to ] = i; sum += dist[E.to]; if(cnt[ E.to ] == 0){ my_push( E.to ); adjust(); } } } } //cannot achieved to "t" return -1 if(dist[t]>=INF) return -1; //add cost of s->t with flow=d int pos=t; int d=INF; while(pos!=s){ int i=prev_v[pos]; int j=prev_e[pos]; pos = i; d = min(d, G[i][j].cap); } pos = t; //cout << t ; while(pos!=s){ int i=prev_v[pos]; int j=prev_e[pos]; G[i][j].cap -= d; G[ G[i][j].to ][ G[i][j].rev ].cap += d; //cost += G[i][j].cost * d; pos = i; //cout << " <- " << pos; } //cout << endl; cost += d * dist[t]; f -= d; //f==0 then end } return cost; } void add_edge(vector<vector<edge> > &G, int from, int to, int cap, int cost){ G[from].push_back((edge){to, cap, cost, (int)G[to].size()}); G[to].push_back((edge){from, 0, -cost, (int)G[from].size()-1}); } int main(){ int G,C,P; cin >> G >> C >> P; string S; cin >> S; int n = S.size(); vector<vector<edge>> Graph(n + 3 + 2); int s = n+3+0; int t = n+3+1; int g = n+0; int c = n+1; int p = n+2; for(int i=0; i<n; i++){ if(S[i] == 'G'){ add_edge(Graph, g, i, 1, 3-1); add_edge(Graph, c, i, 1, 3-0); add_edge(Graph, p, i, 1, 3-3); }else if(S[i] == 'C'){ add_edge(Graph, g, i, 1, 3-3); add_edge(Graph, c, i, 1, 3-1); add_edge(Graph, p, i, 1, 3-0); }else if(S[i] == 'P'){ add_edge(Graph, g, i, 1, 3-0); add_edge(Graph, c, i, 1, 3-3); add_edge(Graph, p, i, 1, 3-1); } add_edge(Graph, i, t, 1, 0); } add_edge(Graph, s, g, G, 0); add_edge(Graph, s, c, C, 0); add_edge(Graph, s, p, P, 0); int ans = 3*n - min_cost_flow(Graph, s, t, n); cout << ans << endl; return 0; }