結果

問題 No.177 制作進行の宮森あおいです!
ユーザー koyumeishikoyumeishi
提出日時 2015-04-02 23:58:12
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,654 bytes
コンパイル時間 816 ms
コンパイル使用メモリ 90,644 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-17 03:47:23
合計ジャッジ時間 1,427 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 1 ms
4,376 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;

typedef struct{
	int to;
	int cap;
	int rev;
}edge;

// node v : distance from s => level[v]
void bfs(vector<vector<edge> > &G, vector<int> &level, int s){
	fill(level.begin(), level.end(), -1);
	queue<int> q;
	q.push(s);
	level[s] = 0;
	while(!q.empty()){
		int e=q.front(); q.pop();
		for(int i=0; i<G[e].size(); i++){
			if(G[e][i].cap > 0 && level[G[e][i].to] < 0){
				level[G[e][i].to] = level[e] + 1;
				q.push(G[e][i].to);
			}
		}
	}
}

int dfs(vector<vector<edge> > &G, vector<int> &level, vector<bool> &used, vector<int> &iter, int s, int f,  int t){
	if(s==t) return f;
	else{
		//iter[e] : done searching from v[0] to v[ iter[e]-1 ]
		for(int &i=iter[s]; i<G[s].size(); i++){
			//distance from s to v[e][i].to must be longer than dist from s to v
			if(G[s][i].cap > 0 && level[s] < level[ G[s][i].to ]){
				int d = dfs(G, level, used, iter, G[s][i].to, min(f, G[s][i].cap), t);
				if(d>0){
					G[s][i].cap -= d;
					G[ G[s][i].to ][ G[s][i].rev ].cap += d;
					return d;
				}
			}
		}
		return 0;
	}
}

constexpr int INF = 1000000000;

int dinic_maxflow(vector<vector<edge> > &G, int s, int t){
	int flow=0;
	while(true){
		vector<int> level(G.size(), -1);
		vector<int> iter(G.size(), 0);
		vector<bool> used(G.size(), false);
		bfs(G, level, s);
		if(level[t] < 0) return flow;	//unable to achieve to t
		while(true){
			int f = dfs(G, level, used, iter, s, INF, t);
			if(f==0) break;
			else flow += f;
		}
	}
}

void add_edge(vector<vector<edge> > &G, int from, int to, int cap){
	G[from].push_back((edge){to, cap, (int)G[to].size()});
	G[to].push_back((edge){from, 0, (int)G[from].size() - 1});
}

int main(){
	int W;
	cin >> W;
	int N;
	cin >> N;
	vector<int> J(N);
	for(int i=0; i<N; i++){
		cin >> J[i];
	}

	int M;
	cin >> M;
	vector<int> C(M);
	for(int i=0; i<M; i++){
		cin >> C[i];
	}

	int s = 2*N+2*M + 0;
	int t = 2*N+2*M + 1;
	vector<vector<edge>> G(2*N+2*M+2);


	for(int i=0; i<M; i++){
		set<int> tmp;
		int Q;
		cin >> Q;
		for(int j=0; j<Q; j++){
			int x;
			cin >> x;
			x--;
			tmp.insert(x);
		}
		for(int j=0; j<N; j++){
			if(tmp.count(j) == 0){
				// j -> i
				add_edge(G, N+j, 2*N+i, INF);
			}
		}
	}

	for(int i=0; i<N; i++){
		add_edge(G, s, i, INF);
		add_edge(G, i, N+i, J[i]);
	}
	for(int i=0; i<M; i++){
		add_edge(G, 2*N + i, 2*N + M + i, C[i]);
		add_edge(G, 2*N + M + i, t, INF);
	}

	int ans = dinic_maxflow(G, s, t);

	cout << (ans>=W?"SHIROBAKO":"BANSAKUTSUKITA") << endl;


	return 0;
}
0