結果

問題 No.161 制限ジャンケン
ユーザー koyumeishikoyumeishi
提出日時 2015-03-05 23:30:09
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 7 ms / 5,000 ms
コード長 2,571 bytes
コンパイル時間 665 ms
コンパイル使用メモリ 80,380 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-20 04:20:26
合計ジャッジ時間 1,551 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,384 KB
testcase_06 AC 5 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 7 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 3 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 3 ms
4,380 KB
testcase_13 AC 6 ms
4,376 KB
testcase_14 AC 3 ms
4,380 KB
testcase_15 AC 4 ms
4,380 KB
testcase_16 AC 3 ms
4,376 KB
testcase_17 AC 3 ms
4,376 KB
testcase_18 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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){
	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-Ford
		vector<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 -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;
}
0