結果

問題 No.654 Air E869120
ユーザー uafr_csuafr_cs
提出日時 2018-02-23 23:55:31
言語 Java21
(openjdk 21)
結果
AC  
実行時間 225 ms / 2,000 ms
コード長 4,206 bytes
コンパイル時間 2,354 ms
コンパイル使用メモリ 79,040 KB
実行使用メモリ 48,168 KB
最終ジャッジ日時 2024-04-18 11:48:27
合計ジャッジ時間 8,150 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 54 ms
37,280 KB
testcase_01 AC 53 ms
37,180 KB
testcase_02 AC 55 ms
37,320 KB
testcase_03 AC 55 ms
37,436 KB
testcase_04 AC 53 ms
37,308 KB
testcase_05 AC 54 ms
37,364 KB
testcase_06 AC 55 ms
37,448 KB
testcase_07 AC 54 ms
37,360 KB
testcase_08 AC 53 ms
37,088 KB
testcase_09 AC 53 ms
37,288 KB
testcase_10 AC 225 ms
48,168 KB
testcase_11 AC 204 ms
48,136 KB
testcase_12 AC 205 ms
47,580 KB
testcase_13 AC 202 ms
47,508 KB
testcase_14 AC 217 ms
46,956 KB
testcase_15 AC 192 ms
47,824 KB
testcase_16 AC 161 ms
43,264 KB
testcase_17 AC 169 ms
43,904 KB
testcase_18 AC 161 ms
43,084 KB
testcase_19 AC 176 ms
43,204 KB
testcase_20 AC 151 ms
41,104 KB
testcase_21 AC 140 ms
41,304 KB
testcase_22 AC 129 ms
41,200 KB
testcase_23 AC 137 ms
41,208 KB
testcase_24 AC 148 ms
41,308 KB
testcase_25 AC 137 ms
40,548 KB
testcase_26 AC 145 ms
40,760 KB
testcase_27 AC 137 ms
40,232 KB
testcase_28 AC 121 ms
40,568 KB
testcase_29 AC 136 ms
40,356 KB
testcase_30 AC 117 ms
40,216 KB
testcase_31 AC 118 ms
40,088 KB
testcase_32 AC 118 ms
40,156 KB
testcase_33 AC 118 ms
40,216 KB
testcase_34 AC 114 ms
40,048 KB
testcase_35 AC 54 ms
37,412 KB
testcase_36 AC 54 ms
37,412 KB
testcase_37 AC 50 ms
37,176 KB
testcase_38 AC 54 ms
37,372 KB
testcase_39 AC 52 ms
36,984 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;

public class Main {

	public static final long INF = Long.MAX_VALUE / 2 - 1;
	
	public static long dinic(Vertex s, Vertex t) {
		long flow = 0;
		for (int p = 1;; p++) {
			Queue<Vertex> que = new LinkedList<Vertex>();
			s.level = 0;
			s.p = p;
			que.offer(s);

			while (!que.isEmpty()) {
				Vertex v = que.poll();
				v.iter = v.es.size() - 1;
				for (Edge e : v.es)
					if (e.to.p < p && e.cap > 0) {
						e.to.level = v.level + 1;
						e.to.p = p;
						que.offer(e.to);
					}
			}
			
			if (t.p < p) {
				return flow;
			}

			for (long f; (f = dfs(s, t, INF)) > 0;) {
				flow += f;
			}
		}
	}

	public static long dfs(Vertex v, Vertex t, long f) {
		if (v == t) {
			return f;
		}

		for (; v.iter >= 0; v.iter--) {
			Edge e = v.es.get(v.iter);
			if (v.level < e.to.level && e.cap > 0) {
				long d = dfs(e.to, t, Math.min(f, e.cap));
				if (d > 0) {
					e.cap -= d;
					e.rev.cap += d;
					return d;
				}
			}
		}

		return 0;
	}

	public static class Vertex{
		ArrayList<Edge> es = new ArrayList<Edge>();
		int level, p, iter;
		long time;

		void add(Vertex to, long cap) {
			Edge e = new Edge(to, cap), rev = new Edge(this, 0);
			e.rev = rev;
			rev.rev = e;
			es.add(e);
			to.es.add(rev);
		}
	}

	public static class Edge {
		Vertex to;
		Edge rev;
		long cap;

		Edge(Vertex to, long cap) {
			this.to = to;
			this.cap = cap;
		}
	}
	
	public static void main(String[] args) throws IOException {
		Scanner sc = new Scanner(System.in);

		final int N = sc.nextInt();
		final int M = sc.nextInt();
		final int d = sc.nextInt();
		
		Vertex S = new Vertex();
		Vertex T = new Vertex();
		
		ArrayList<ArrayList<Vertex>> from_cities = new ArrayList<ArrayList<Vertex>>();
		ArrayList<ArrayList<Vertex>> to_cities = new ArrayList<ArrayList<Vertex>>();
		for(int i = 0; i < N; i++){ 
			from_cities.add(new ArrayList<Vertex>());
			to_cities.add(new ArrayList<Vertex>());
		}
		
		for (int i = 0; i < M; i++) {
			final int u = sc.nextInt() - 1;
			final int v = sc.nextInt() - 1;
			final long p = sc.nextLong();
			final long q = sc.nextLong();
			final long w = sc.nextLong();
			
			Vertex from = new Vertex();
			Vertex to = new Vertex();
			from.time = p;
			to.time = q;
			
			from.add(to, w);
			
			from_cities.get(u).add(from);
			to_cities.get(v).add(to);
		}
		
		for(final Vertex v : from_cities.get(0)){ S.add(v, INF); }
		for(final Vertex v : to_cities.get(N - 1)){ v.add(T, INF); }
		
		for(int i = 0; i < N; i++){
			for(final Vertex from : from_cities.get(i)){
				for(final Vertex to : to_cities.get(i)){
					if(to.time + d <= from.time){
						to.add(from, INF);
					}
				}
			}
		}
		
		System.out.println(dinic(S, T));
	}

	public static class Scanner implements Closeable {
		private BufferedReader br;
		private StringTokenizer tok;

		public Scanner(InputStream is) throws IOException {
			br = new BufferedReader(new InputStreamReader(is));
		}

		private void getLine() throws IOException {
			while (!hasNext()) {
				tok = new StringTokenizer(br.readLine());
			}
		}

		private boolean hasNext() {
			return tok != null && tok.hasMoreTokens();
		}

		public String next() throws IOException {
			getLine();
			return tok.nextToken();
		}

		public int nextInt() throws IOException {
			return Integer.parseInt(next());
		}

		public long nextLong() throws IOException {
			return Long.parseLong(next());
		}

		public double nextDouble() throws IOException {
			return Double.parseDouble(next());
		}

		public int[] nextIntArray(int n) throws IOException {
			final int[] ret = new int[n];
			for (int i = 0; i < n; i++) {
				ret[i] = this.nextInt();
			}
			return ret;
		}

		public long[] nextLongArray(int n) throws IOException {
			final long[] ret = new long[n];
			for (int i = 0; i < n; i++) {
				ret[i] = this.nextLong();
			}
			return ret;
		}

		public void close() throws IOException {
			br.close();
		}
	}
}
0