結果

問題 No.177 制作進行の宮森あおいです!
ユーザー kenkooookenkoooo
提出日時 2015-04-03 14:44:30
言語 Java21
(openjdk 21)
結果
RE  
実行時間 -
コード長 3,051 bytes
コンパイル時間 1,789 ms
コンパイル使用メモリ 74,580 KB
実行使用メモリ 49,804 KB
最終ジャッジ日時 2023-09-17 03:59:40
合計ジャッジ時間 3,094 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;

public class Main {

	public static void main(String[] args) {
		int W = nextInt();
		int N = nextInt();
		int[] J = new int[N];
		for (int i = 0; i < J.length; i++) {
			J[i] = nextInt();
		}
		int M = nextInt();
		int[] C = new int[M];
		for (int i = 0; i < C.length; i++) {
			C[i] = nextInt();
		}

		/*
		 * 原画マンiを頂点i、作画監督jを頂点N+j、始点をN+M、終点をN+M+1とした最大フロー問題に帰着する
		 */
		MaximumFlow maximumFlow = new MaximumFlow(M + N + 2, N + M, N + M + 1);
		for (int i = 0; i < N; i++) {
			maximumFlow.addEdge(N + M, i, J[i]);
		}
		for (int j = 0; j < M; j++) {
			maximumFlow.addEdge(N + j, N + M + 1, C[j]);
		}

		for (int j = 0; j < M; j++) {
			// 作画監督jについて考える
			int Q = nextInt();
			boolean[] hate = new boolean[Q];
			for (int q = 0; q < Q; q++) {
				hate[nextInt() - 1] = true;
			}

			for (int i = 0; i < N; i++) {
				if (!hate[i]) {
					// 原画マンiが作画監督jに嫌われていなければ、iからjにフロー
					maximumFlow.addEdge(i, N + j, J[i]);
				}
			}
		}

		int maximum = maximumFlow.fordFulkerson();
		if (maximum >= W) {
			System.out.println("SHIROBAKO");
		} else {
			System.out.println("BANSAKUTSUKITA");
		}

	}

	static int nextInt() {
		int c;
		try {
			c = System.in.read();
			while (c != '-' && (c < '0' || c > '9'))
				c = System.in.read();
			if (c == '-')
				return -nextInt();
			int res = 0;
			while (c >= '0' && c <= '9') {
				res = res * 10 + c - '0';
				c = System.in.read();
			}
			return res;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return -1;
	}
}

class MaximumFlow {
	public static final int INF = 100000000;

	int V;
	int source;
	int sink;

	ArrayList<Edge>[] graph;
	boolean[] used;

	@SuppressWarnings("unchecked")
	public MaximumFlow(int V, int source, int sink) {
		this.V = V;
		this.source = source;
		this.sink = sink;
		graph = new ArrayList[V];
		for (int i = 0; i < V; i++) {
			graph[i] = new ArrayList<Edge>();
		}
		used = new boolean[V];
	}

	public void addEdge(int from, int to, int cap) {
		graph[from].add(new Edge(to, cap, graph[to].size()));
		graph[to].add(new Edge(from, 0, graph[from].size() - 1));
	}

	public int fordFulkerson() {
		int flow = 0;
		while (true) {
			Arrays.fill(used, false);
			int f = dfsFF(source, sink, INF);
			if (f == 0) {
				break;
			}
			flow += f;
		}
		return flow;
	}

	private int dfsFF(int v, int t, int f) {
		if (v == t) {
			return f;
		}
		used[v] = true;
		for (int i = 0; i < graph[v].size(); i++) {
			Edge e = graph[v].get(i);
			if (!used[e.to] && e.cap > 0) {
				int d = dfsFF(e.to, t, Math.min(f, e.cap));
				if (d > 0) {
					e.cap -= d;
					graph[e.to].get(e.rev).cap += d;
					return d;
				}
			}
		}
		return 0;
	}

	class Edge {
		int to;
		int cap;
		int rev;

		public Edge(int to, int cap, int rev) {
			this.to = to;
			this.cap = cap;
			this.rev = rev;
		}
	}

}
0