結果

問題 No.654 Air E869120
ユーザー uwiuwi
提出日時 2018-02-23 22:41:52
言語 Java21
(openjdk 21)
結果
AC  
実行時間 145 ms / 2,000 ms
コード長 8,834 bytes
コンパイル時間 5,436 ms
コンパイル使用メモリ 90,484 KB
実行使用メモリ 40,692 KB
最終ジャッジ日時 2024-04-18 02:18:24
合計ジャッジ時間 10,406 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 58 ms
37,284 KB
testcase_01 AC 60 ms
37,088 KB
testcase_02 AC 60 ms
37,196 KB
testcase_03 AC 59 ms
37,116 KB
testcase_04 AC 62 ms
37,304 KB
testcase_05 AC 60 ms
37,244 KB
testcase_06 AC 64 ms
37,360 KB
testcase_07 AC 65 ms
37,324 KB
testcase_08 AC 65 ms
37,360 KB
testcase_09 AC 65 ms
37,176 KB
testcase_10 AC 119 ms
39,528 KB
testcase_11 AC 116 ms
39,500 KB
testcase_12 AC 116 ms
39,412 KB
testcase_13 AC 121 ms
39,528 KB
testcase_14 AC 115 ms
38,984 KB
testcase_15 AC 116 ms
39,420 KB
testcase_16 AC 143 ms
40,176 KB
testcase_17 AC 145 ms
40,692 KB
testcase_18 AC 137 ms
40,432 KB
testcase_19 AC 140 ms
40,424 KB
testcase_20 AC 110 ms
39,488 KB
testcase_21 AC 112 ms
39,520 KB
testcase_22 AC 84 ms
38,060 KB
testcase_23 AC 100 ms
39,408 KB
testcase_24 AC 112 ms
39,508 KB
testcase_25 AC 79 ms
38,156 KB
testcase_26 AC 85 ms
38,196 KB
testcase_27 AC 81 ms
37,860 KB
testcase_28 AC 86 ms
38,728 KB
testcase_29 AC 85 ms
38,228 KB
testcase_30 AC 76 ms
37,544 KB
testcase_31 AC 89 ms
37,972 KB
testcase_32 AC 83 ms
37,816 KB
testcase_33 AC 77 ms
37,408 KB
testcase_34 AC 88 ms
37,544 KB
testcase_35 AC 63 ms
37,220 KB
testcase_36 AC 62 ms
37,108 KB
testcase_37 AC 62 ms
37,128 KB
testcase_38 AC 61 ms
37,148 KB
testcase_39 AC 62 ms
37,088 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package contest180223;
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 D {
	InputStream is;
	PrintWriter out;
	String INPUT = "";
	
	void solve()
	{
		int n = ni(), m = ni(), d = ni();
		int[][] air = new int[m][];
		for(int i = 0;i < m;i++)air[i] = new int[]{ni()-1, ni()-1 ,ni(), ni() + d, ni()};
		
		long[] times = new long[2*m+2];
		int p = 0;
		for(int i = 0;i < m;i++){
			times[p++] = (long)air[i][0]<<32|air[i][2];
			times[p++] = (long)air[i][1]<<32|air[i][3];
		}
		times[p++] = (long)0<<32|0;
		times[p++] = (long)(n-1)<<32|1000000000+d;
		Arrays.sort(times);
		times = uniq(times);
		
		List<Edge> es = new ArrayList<Edge>();
		for(int i = 0;i < m;i++){
			int from = Arrays.binarySearch(times, (long)air[i][0]<<32|air[i][2]);
			int to = Arrays.binarySearch(times, (long)air[i][1]<<32|air[i][3]);
			es.add(new Edge(from, to, air[i][4]));
		}
		for(int i = 0;i < times.length-1;i++){
			if(times[i]>>>32 == times[i+1]>>>32){
				es.add(new Edge(i, i+1, 1000000000000L));
			}
		}
		out.println(maximumFlowDinicNoRec(compileWD(times.length, es), 
				Arrays.binarySearch(times, (long)0<<32|0),
				Arrays.binarySearch(times, (long)(n-1)<<32|1000000000+d)
				));
	}
	
	public static long[] uniq(long[] a)
	{
		int n = a.length;
		int p = 0;
		for(int i = 0;i < n;i++) {
			if(i == 0 || a[i] != a[i-1])a[p++] = a[i];
		}
		return Arrays.copyOf(a, p);
	}

	
	public static class Edge
	{
		public int from, to;
		public long capacity;
		public long flow;
		public Edge complement;
		public boolean cloned;
		
		public Edge(int from, int to, long capacity) {
			this.from = from;
			this.to = to;
			this.capacity = capacity;
		}
	}
	
	public static void reset(Edge[][] g)
	{
		for(Edge[] row : g){
			for(Edge e : row){
				e.flow = e.cloned ? e.capacity : 0;
			}
		}
	}
	
	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;
			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;
	}
	
	public static Edge[][] compileWU(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*2);
			origin.flow = origin.capacity;
			clone.flow = origin.capacity;
			clone.complement = origin;
			clone.cloned = true;
			origin.complement = clone;
			origin.capacity *= 2;
			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 maximumFlowDinic(Edge[][] g, int source, int sink)
	{
		int n = g.length;
		int[] d = new int[n];
		int[] q = new int[n];
		long ret = 0;
		while(true){
			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;
			int[] sp = new int[n];
			for(int i = 0;i < n;i++)sp[i] = g[i].length - 1;
			ret += dfsDinic(d, g, sp, source, sink, Long.MAX_VALUE);
		}
		
		return ret;
	}
	
	private static long dfsDinic(int[] d, Edge[][] g, int[] sp, int cur, int sink, long min)
	{
		if(cur == sink)return min;
		long left = min;
		for(int i = sp[cur];i >= 0;i--){
			Edge ne = g[cur][i];
			if(d[ne.to] == d[cur]+1 && ne.capacity - ne.flow > 0){
				long fl = dfsDinic(d, g, sp, ne.to, sink, Math.min(left, ne.capacity - ne.flow));
				if(fl > 0){
					left -= fl;
					ne.flow += fl;
					ne.complement.flow -= fl;
					if(left == 0){
						sp[cur] = i;
						return min;
					}
				}
			}
		}
		sp[cur] = -1;
		return min-left;
	}
	
	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-1;
			int sp = 0;
			stack[sp] = source;
			lefts[sp] = Long.MAX_VALUE / 10;
			parflow[sp] = 0;
			sp++;
			long delta = 0;
			boolean down = true;
			while(sp > 0){
				int cur = stack[sp-1];
				long left = lefts[sp-1];
				if(cur == sink){
					delta += left;
					parflow[sp-1] = left;
					sp--;
					down = false;
					continue;
				}
				if(!down){
					if(parflow[sp] > 0){
						lefts[sp-1] -= parflow[sp];
						left = lefts[sp-1];
						Edge ne = g[cur][probe[cur]];
						ne.flow += parflow[sp];
						ne.complement.flow -= parflow[sp];
						parflow[sp-1] += parflow[sp];
					}
					if(left > 0)probe[cur]--;
				}
				if(left > 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(left, ne.capacity - ne.flow);
						stack[sp] = ne.to;
						parflow[sp] = 0;
						sp++;
					}else{
						probe[cur]--;
					}
				}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");
//		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 D().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