結果
問題 | No.161 制限ジャンケン |
ユーザー |
![]() |
提出日時 | 2015-03-05 23:30:09 |
言語 | C++11 (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 8 ms / 5,000 ms |
コード長 | 2,571 bytes |
コンパイル時間 | 754 ms |
コンパイル使用メモリ | 81,252 KB |
実行使用メモリ | 5,248 KB |
最終ジャッジ日時 | 2024-11-30 04:24:23 |
合計ジャッジ時間 | 1,302 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
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 1e9typedef struct{int to;int cap;int cost;int rev;}edge;//min cost : s->t (flow:f)int min_cost_flow(vector<vector<edge> > &G, int s, int t, int f){int cost = 0;vector<int> prev_v(G.size(),-1);vector<int> prev_e(G.size(),-1);while(f>0){//min distance(cost based) search with Belman-Fordvector<int> dist(G.size(), INF);dist[s] = 0;prev_v[s] = s;bool update=true;while(update){update = false;for(int i=0; i<G.size(); i++){if(dist[i] >= INF) continue;vector<edge> &e = G[i];for(int j=0; j<e.size(); j++){if(dist[e[j].to] > dist[i] + e[j].cost && e[j].cap>0){dist[e[j].to] = dist[i] + e[j].cost;update = true;prev_v[ e[j].to ] = i;prev_e[ e[j].to ] = j;}}}}//cannot achieved to "t" return -1if(dist[t]>=INF) return -1;//add cost of s->t with flow=dint 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;}