結果

問題 No.957 植林
ユーザー uwiuwi
提出日時 2019-12-20 12:52:58
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 8,742 bytes
コンパイル時間 3,627 ms
コンパイル使用メモリ 88,312 KB
実行使用メモリ 132,780 KB
最終ジャッジ日時 2024-07-08 06:01:10
合計ジャッジ時間 18,927 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 47 ms
42,468 KB
testcase_01 AC 47 ms
37,084 KB
testcase_02 AC 47 ms
36,900 KB
testcase_03 AC 905 ms
95,396 KB
testcase_04 AC 742 ms
88,212 KB
testcase_05 AC 855 ms
94,772 KB
testcase_06 AC 640 ms
90,852 KB
testcase_07 AC 908 ms
90,272 KB
testcase_08 AC 725 ms
88,964 KB
testcase_09 AC 703 ms
89,132 KB
testcase_10 AC 692 ms
89,520 KB
testcase_11 AC 627 ms
90,096 KB
testcase_12 AC 703 ms
88,724 KB
testcase_13 AC 631 ms
87,772 KB
testcase_14 AC 621 ms
90,536 KB
testcase_15 AC 731 ms
88,764 KB
testcase_16 AC 683 ms
87,588 KB
testcase_17 AC 594 ms
88,632 KB
testcase_18 TLE -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

package adv2019;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Queue;

public class D20_4 {
	InputStream is;
	PrintWriter out;
	String INPUT = "";
	
	void solve()
	{
		int n = ni(), m = ni();
		int[][] g = new int[n][m];
		for(int i = 0;i < n;i++) {
			g[i] = na(m);
		}
		int[] R = na(n);
		int[] C = na(m);
		
		List<Edge> es = new ArrayList<>();
		int src = n*m+n+m, sink = src + 1;
		for(int i = 0;i < n;i++) {
			for(int j = 0;j < m;j++) {
//				es.add(new Edge(src, i*m+j, 0L));
				es.add(new Edge(i*m+j, sink, g[i][j]));
			}
		}
		for(int i = 0;i < n;i++) {
			es.add(new Edge(src, n*m+i, R[i]));
//			es.add(new Edge(n*m+i, sink, 0));
			
			for(int j = 0;j < m;j++) {
				es.add(new Edge(n*m+i, i*m+j, R[i]));
			}
		}
		for(int i = 0;i < m;i++) {
			es.add(new Edge(src, n*m+n+i, C[i]));
//			es.add(new Edge(n*m+n+i, sink, 0));
			for(int j = 0;j < n;j++) {
				es.add(new Edge(n*m+n+i, j*m+i, C[i]));
			}
		}
		
		long ans = 0;
		for(int v : R)ans += v;
		for(int v : C)ans += v;
//		ans -= maximumFlowDinicNoRec(compileWD(sink+1, es), src, sink);
		Edge[][] G = compileWD(sink+1, es);
		
		D20_4.g = G;
		initialize(N = sink+1);
		ans -= pushRelabel(src, sink, n*m+n+m);
		out.println(ans);
	}
	
	public static class Edge
	{
		public int from, to;
		public long flow;
		public Edge complement;
		public boolean cloned;
		
		public Edge(int from, int to, long f) {
			this.from = from;
			this.to = to;
			this.flow = f;
		}
	}
	
	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, 0);
			clone.complement = origin;
			clone.cloned = true;
			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;
	}
	
	static long INF = Long.MAX_VALUE / 100;
	
	static int SRC, SINK;
	
	static long[] excess;
	
	public static long pushRelabel(int source, int sink, int heuristicN)
	{
		SRC = source; SINK = sink;
		N = g.length;
		excess = new long[N];
		excess[source] = INF; excess[sink] = -INF;
		globalRelabel();
		for(Edge e : g[source]) {
			push(e);
		}
		for(;highest >= 0;highest--) {
			while(!list.get(highest).isEmpty()) {
				int v = list.get(highest).poll();
				discharge(v);
				if(work > 4 * heuristicN) {
					globalRelabel();
				}
			}
		}
		return excess[sink] + INF;
	}
	
	static void discharge(int v)
	{
		int nh = N;
		for(Edge e : g[v]) {
			if(e.flow > 0) {
				if(height[v] == height[e.to] + 1) {
					push(e);
					if(excess[v] <= 0)return;
				}else {
					nh = Math.min(nh, height[e.to] + 1);
				}
			}
		}
		if(count[height[v]] > 1) {
			updateHeight(v, nh);
		}else {
			for(int i = height[v];i <= highest;i++) {
				EQ q = gap.get(i);
				int f = q.ph;
				int t = q.pt;
				for(int k = t;k < f;k++) {
					updateHeight(q.q[k], N);
				}
//				gap.get(i).clear();
				gap.set(i, new EQ());
//				gap.get(i).clear();
			}
		}
	}
	
	static void push(Edge e)
	{
		if(excess[e.to] == 0) {
			list.get(height[e.to]).add(e.to);
		}
		long df = Math.min(excess[e.from], e.flow);
		e.flow -= df;
		e.complement.flow += df;
		excess[e.from] -= df;
		excess[e.to] += df;
	}
	
	static int N;
	static int work;
	static int[] height;
	static int[] count;
	static int highest;
	static Edge[][] g;
	static List<ES> list;
	static List<EQ> gap;
	
	static void initialize(int N)
	{
		height = new int[N+1];
		count = new int[N+1];
		list = new ArrayList<>();
		gap = new ArrayList<>();
		for(int i = 0;i < N+1;i++) {
			list.add(new ES());
			gap.add(new EQ());
		}
	}
	
	public static class EQ {
		public int[] q;
		protected int pt, ph;
		public EQ(){ q = new int[2]; pt = ph = 0; }
		public void add(int x){
			if(ph == q.length)scale();
			q[ph++] = x;
		}
		public void scale()
		{
			int n = q.length;
			int[] nq = new int[n*3/2];
			System.arraycopy(q, pt, nq, 0, ph-pt);
			q = nq;
			ph = ph-pt;
			pt = 0;
		}
		public void clear(){ pt = ph = 0; }
		public int poll(){ return q[pt++]; }
		public int size(){ return ph-pt; }
		public boolean isEmpty(){ return ph==pt; }
	}
	
	public static class ES {
		public int[] s;
		protected int ph;
		public ES(){ s = new int[2]; ph = 0; }
		public void add(int x){
			if(ph == s.length)scale();
			s[ph++] = x;
		}
		public void scale()
		{
			int n = s.length;
			s = Arrays.copyOf(s, n*3/2);
		}
		public void clear(){ ph = 0; }
		public int poll(){ return s[--ph]; }
		public int size(){ return ph; }
		public boolean isEmpty(){ return ph==0; }
	}
	
	static void globalRelabel()
	{
		work = 0;
		Arrays.fill(height, N);
		Arrays.fill(count, 0);
		for(int i = 0;i < highest;i++) {
			list.set(i, new ES());
			gap.set(i, new EQ());
		}
		height[SINK] = 0;
		Queue<Integer> q = new ArrayDeque<>();
		q.add(SINK);
		while(!q.isEmpty()) {
			int v = q.poll();
			for(Edge e : g[v]) {
				if(height[e.to] == N && e.complement.flow > 0) {
					q.add(e.to);
					updateHeight(e.to, height[v] + 1);
				}
			}
//			tr(v, height[v], height);
			highest = height[v];
		}
	}
	
	static void updateHeight(int v, int nh)
	{
		work++;
		if(height[v] != N) {
			count[height[v]]--;
		}
		height[v] = nh;
		if(nh == N)return;
		count[nh]++;
		highest = nh;
		gap.get(nh).add(v);
		if(excess[v] > 0) {
			list.get(nh).add(v);
		}
	}
	
	void run() throws Exception
	{
//		int n = 300, m = 300;
//		Random gen = new Random(1114);
//		StringBuilder sb = new StringBuilder();
//		sb.append(n + " ");
//		sb.append(m + " ");
//		for (int i = 0; i < n*m+n+m; i++) {
//			if(i < n*m) {
//				sb.append(gen.nextInt(5000000) + " ");
//			}else {
//				sb.append(gen.nextInt(1000000000) + " ");
//			}
//		}
//		INPUT = sb.toString();

		
		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");
//		Thread t = new Thread(null, null, "~", Runtime.getRuntime().maxMemory()){
//			@Override
//			public void run() {
//				long s = System.currentTimeMillis();
//				solve();
//				out.flush();
//				if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms");
//			}
//		};
//		t.start();
//		t.join();
	}
	
	public static void main(String[] args) throws Exception { new D20_4().run(); }
	
	private byte[] inbuf = new byte[1024];
	public 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 int[] na(int n)
	{
		int[] a = new int[n];
		for(int i = 0;i < n;i++)a[i] = ni();
		return a;
	}
	
	private long[] nal(int n)
	{
		long[] a = new long[n];
		for(int i = 0;i < n;i++)a[i] = nl();
		return a;
	}
	
	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[][] nmi(int n, int m) {
		int[][] map = new int[n][];
		for(int i = 0;i < n;i++)map[i] = na(m);
		return map;
	}
	
	private int ni() { return (int)nl(); }
	
	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