結果

問題 No.177 制作進行の宮森あおいです!
ユーザー uwiuwi
提出日時 2015-04-02 23:30:51
言語 Java21
(openjdk 21)
結果
AC  
実行時間 50 ms / 2,000 ms
コード長 6,067 bytes
コンパイル時間 4,202 ms
コンパイル使用メモリ 87,996 KB
実行使用メモリ 49,632 KB
最終ジャッジ日時 2023-09-17 03:45:11
合計ジャッジ時間 5,839 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
49,104 KB
testcase_01 AC 44 ms
49,500 KB
testcase_02 AC 44 ms
49,344 KB
testcase_03 AC 44 ms
49,188 KB
testcase_04 AC 45 ms
49,160 KB
testcase_05 AC 46 ms
49,316 KB
testcase_06 AC 48 ms
49,564 KB
testcase_07 AC 44 ms
49,324 KB
testcase_08 AC 46 ms
49,192 KB
testcase_09 AC 49 ms
49,632 KB
testcase_10 AC 50 ms
49,404 KB
testcase_11 AC 48 ms
49,220 KB
testcase_12 AC 49 ms
47,412 KB
testcase_13 AC 42 ms
47,588 KB
testcase_14 AC 43 ms
49,108 KB
testcase_15 AC 42 ms
49,200 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package contest;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.List;

// 万策尽きてません!
public class Q177 {
	InputStream is;
	PrintWriter out;
	String INPUT = "";
	
	void solve()
	{
		int w = ni();
		int n = ni();
		int[] J = na(n);
		int m = ni();
		int[] C = na(m);
		long[] ng = new long[m];
		for(int i = 0;i < m;i++){
			int L = ni();
			for(int j = 0;j < L;j++){
				ng[i] |= 1L<<ni()-1;
			}
		}
		
		List<Edge> es = new ArrayList<Edge>();
		int src = n+m, sink = n+m+1;
		for(int i = 0;i < n;i++){ // えまたそ
			es.add(new Edge(src, i, J[i]));
		}
		for(int i = 0;i < m;i++){
			es.add(new Edge(i+n, sink, C[i]));
		}
		for(int i = 0;i < n;i++){
			for(int j = 0;j < m;j++){
				if(ng[j]<<~i>=0){
					es.add(new Edge(i, j+n, 10000));
				}
			}
		}
		if(maximumFlowDinicNoRec(compileWD(sink+1, es), src, sink) >= w){
			out.println("SHIROBAKO");
		}else{
			out.println("BANSAKUTSUKITA");
		}
	}
	
	public static class Edge
	{
		public int from, to;
		public int capacity;
		public int flow;
		public Edge complement;
		// public int iniflow;
		
		public Edge(int from, int to, int capacity) {
			this.from = from;
			this.to = to;
			this.capacity = capacity;
		}
	}
	
	public static Edge[][] compileWD(int n, List<Edge> edges)
	{
		Edge[][] g = new Edge[n][];
		// cloning
		for(int i = edges.size()-1;i >= 0;i--){
			Edge origin = edges.get(i);
			Edge clone = new Edge(origin.to, origin.from, origin.capacity);
			clone.flow = origin.capacity;
			clone.complement = origin;
			origin.complement = clone;
			edges.add(clone);
		}
		
		int[] p = new int[n];
		for(Edge e : edges)p[e.from]++;
		for(int i = 0;i < n;i++)g[i] = new Edge[p[i]];
		for(Edge e : edges)g[e.from][--p[e.from]] = e;
		return g;
	}
	
	public static long maximumFlowDinicNoRec(Edge[][] g, int source, int sink)
	{
		int n = g.length;
		int[] d = new int[n]; // distance
		int[] q = new int[n]; // queue for BFS
		long ret = 0;
		int[] stack = new int[n];
		long[] lefts = new long[n]; // left to flow
		long[] parflow = new long[n];
		int[] probe = new int[n]; // search pointer
		while(true){
			// BFS
			Arrays.fill(d, -1);
			q[0] = source;
			int r = 1;
			d[source] = 0;
			for(int v = 0;v < r;v++){
				int cur = q[v];
				for(Edge ne : g[cur]){
					if(d[ne.to] == -1 && ne.capacity - ne.flow > 0) {
						q[r++] = ne.to;
						d[ne.to] = d[cur] + 1;
					}
				}
			}
			if(d[sink] == -1)break;
			
			// DFS
			for(int i = 0;i < n;i++)probe[i] = g[i].length;
			int sp = 0;
			stack[sp] = source;
			lefts[sp] = Long.MAX_VALUE;
			parflow[sp] = 0;
			sp++;
			long delta = 0;
			boolean down = true;
			while(sp > 0){
				int cur = stack[sp-1];
				long fl = lefts[sp-1];
				if(cur == sink){
					delta += fl;
					parflow[sp-1] = fl;
					sp--;
					down = false;
					continue;
				}
				if(!down && parflow[sp] > 0){
					lefts[sp-1] -= parflow[sp];
					fl = lefts[sp-1];
					Edge ne = g[cur][probe[cur]];
					ne.flow += parflow[sp];
					ne.complement.flow -= parflow[sp];
					parflow[sp-1] += parflow[sp];
				}
				if(fl > 0 && probe[cur] > 0){
					down = true;
					Edge ne = g[cur][--probe[cur]];
					if(d[ne.to] == d[cur]+1 && ne.capacity - ne.flow > 0){
						lefts[sp] = Math.min(lefts[sp-1], ne.capacity - ne.flow);
						stack[sp] = ne.to;
						parflow[sp] = 0;
						sp++;
					}
				}else{
					down = false;
					sp--;
				}
			}
			ret += delta;
		}
		return ret;
	}
	
	void run() throws Exception
	{
		is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
		out = new PrintWriter(System.out);
		
		long s = System.currentTimeMillis();
		solve();
		out.flush();
		if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms");
	}
	
	public static void main(String[] args) throws Exception { new Q177().run(); }
	
	private byte[] inbuf = new byte[1024];
	private int lenbuf = 0, ptrbuf = 0;
	
	private int readByte()
	{
		if(lenbuf == -1)throw new InputMismatchException();
		if(ptrbuf >= lenbuf){
			ptrbuf = 0;
			try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }
			if(lenbuf <= 0)return -1;
		}
		return inbuf[ptrbuf++];
	}
	
	private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
	private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }
	
	private double nd() { return Double.parseDouble(ns()); }
	private char nc() { return (char)skip(); }
	
	private String ns()
	{
		int b = skip();
		StringBuilder sb = new StringBuilder();
		while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ')
			sb.appendCodePoint(b);
			b = readByte();
		}
		return sb.toString();
	}
	
	private char[] ns(int n)
	{
		char[] buf = new char[n];
		int b = skip(), p = 0;
		while(p < n && !(isSpaceChar(b))){
			buf[p++] = (char)b;
			b = readByte();
		}
		return n == p ? buf : Arrays.copyOf(buf, p);
	}
	
	private char[][] nm(int n, int m)
	{
		char[][] map = new char[n][];
		for(int i = 0;i < n;i++)map[i] = ns(m);
		return map;
	}
	
	private int[] na(int n)
	{
		int[] a = new int[n];
		for(int i = 0;i < n;i++)a[i] = ni();
		return a;
	}
	
	private int ni()
	{
		int num = 0, b;
		boolean minus = false;
		while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
		if(b == '-'){
			minus = true;
			b = readByte();
		}
		
		while(true){
			if(b >= '0' && b <= '9'){
				num = num * 10 + (b - '0');
			}else{
				return minus ? -num : num;
			}
			b = readByte();
		}
	}
	
	private long nl()
	{
		long num = 0;
		int b;
		boolean minus = false;
		while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
		if(b == '-'){
			minus = true;
			b = readByte();
		}
		
		while(true){
			if(b >= '0' && b <= '9'){
				num = num * 10 + (b - '0');
			}else{
				return minus ? -num : num;
			}
			b = readByte();
		}
	}
	
	private static void tr(Object... o) { System.out.println(Arrays.deepToString(o)); }
}
0