結果

問題 No.177 制作進行の宮森あおいです!
ユーザー kurenai3110kurenai3110
提出日時 2017-06-04 21:19:52
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 2,425 bytes
コンパイル時間 1,985 ms
コンパイル使用メモリ 79,468 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-22 04:25:08
合計ジャッジ時間 2,003 ms
ジャッジサーバーID
(参考情報)
judge11 / judge9
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <queue>
#include <set>
using namespace std;

static const int INF = 1e9;
	
struct Dinic {
	struct E {
		//to,容量,逆辺のindex
		int to, cap, rev;
		E(int t, int c, int r) {
			to = t;
			cap = c;
			rev = r;
		}
	};

	//最大頂点数
	int MAX_V;
	vector<vector<E>>G;
	Dinic(int max_v) {
		MAX_V = max_v;
		G.resize(MAX_V);
	}

	//有向辺
	inline void add_edge(int from, int to, int cap) {
		if (from == to)return;
		G[from].push_back(E(to, cap, G[to].size()));
		G[to].push_back(E(from, 0, G[from].size()-1));
	}

	//無向辺
	inline void add_undirected_edge(int from, int to, int cap) {
		if (from == to)return;
		G[from].push_back(E(to, cap, G[to].size()));
		G[to].push_back(E(from, cap, G[from].size()-1));
	}

	//深さ
	vector<int>level;

	//深さのラベル付け
	void bfs(int s) {
		level.assign(MAX_V, -1);
		queue<int> que;
		level[s] = 0;
		que.push(s);
		while (!que.empty()) {
			int v = que.front(); que.pop();
			for (int i = 0; i < G[v].size(); i++) {
				E &e = G[v][i];
				if (e.cap > 0 && level[e.to] < 0) {
					level[e.to] = level[v] + 1;
					que.push(e.to);
				}
			}
		}
	}

	//s -- v -- u -- t
	//tから深さ優先探索
	int dfs(int s, int u, int crr) {
		if (s == u || crr == 0)return crr;

		int sum = 0;

		for (int i = 0; i < G[u].size(); i++) {
			E& e = G[u][i];				//u - v
			int v = e.to;
			E& ee = G[v][e.rev];		//v - u

			if (level[v] >= level[u] || ee.cap <= 0)continue;

			int f = dfs(s, v, min(crr - sum, ee.cap));
			if (f <= 0)continue;

			//容量の更新
			ee.cap -= f;
			e.cap += f;

			//流量の更新
			sum += f;

			if (sum == crr)break;
		}

		return sum;
	}

	int max_flow(int s, int t) {
		int flow = 0;
		//tに流せなくなるまで
		while(1){
			bfs(s);
			if (level[t] < 0)return flow;
			flow += dfs(s, t, INF);
		}
	}
};

int main() {
	int W;cin>>W;
	int N;cin>>N;
	Dinic dnc(102);
	for(int i=0;i<N;i++){
		int J;cin>>J;
		dnc.add_edge(0,i+1,J);
	}
	int M;cin>>M;
	for(int i=0;i<M;i++){
		int C;cin>>C;
		dnc.add_edge(N+i+1,N+M+1,C);
	}
	for(int i=0;i<M;i++){
		int Q;cin>>Q;
		int x=1;
		for(int j=0;j<Q;j++){
			int X;cin>>X;
			for(;x<X;x++){
				dnc.add_edge(x,N+i+1,INF);
			}
			x=X+1;
		}
		for(;x<=N;x++)dnc.add_edge(x,N+i+1,INF);
	}
	
	int ans = dnc.max_flow(0,N+M+1);

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