結果
問題 | No.161 制限ジャンケン |
ユーザー | koyumeishi |
提出日時 | 2015-03-14 20:50:58 |
言語 | C++11 (gcc 11.4.0) |
結果 |
AC
|
実行時間 | 4 ms / 5,000 ms |
コード長 | 2,738 bytes |
コンパイル時間 | 719 ms |
コンパイル使用メモリ | 85,868 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-05-07 11:56:45 |
合計ジャッジ時間 | 1,390 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 4 ms
5,248 KB |
testcase_01 | AC | 1 ms
5,376 KB |
testcase_02 | AC | 1 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 | 4 ms
5,376 KB |
testcase_07 | AC | 1 ms
5,376 KB |
testcase_08 | AC | 4 ms
5,376 KB |
testcase_09 | AC | 1 ms
5,376 KB |
testcase_10 | AC | 2 ms
5,376 KB |
testcase_11 | AC | 2 ms
5,376 KB |
testcase_12 | AC | 2 ms
5,376 KB |
testcase_13 | AC | 4 ms
5,376 KB |
testcase_14 | AC | 2 ms
5,376 KB |
testcase_15 | AC | 2 ms
5,376 KB |
testcase_16 | AC | 1 ms
5,376 KB |
testcase_17 | AC | 2 ms
5,376 KB |
testcase_18 | AC | 2 ms
5,376 KB |
ソースコード
#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) 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); dist[s] = 0; prev_v[s] = s; queue<int> Q; auto my_push = [&](int node){ Q.push(node); cnt[node]++; }; auto my_pop = [&]() -> int{ int ret = Q.front(); cnt[ret]--; Q.pop(); return ret; }; my_push(s); while(!Q.empty()){ int pos = my_pop(); 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){ dist[E.to] = dist[pos] + E.cost; prev_v[ E.to ] = pos; prev_e[ E.to ] = i; if(cnt[ E.to ] == 0){ my_push( E.to ); } } } } //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; }