結果

問題 No.654 Air E869120
ユーザー uafr_csuafr_cs
提出日時 2018-02-23 23:55:31
言語 Java21
(openjdk 21)
結果
AC  
実行時間 239 ms / 2,000 ms
コード長 4,206 bytes
コンパイル時間 2,341 ms
コンパイル使用メモリ 79,392 KB
実行使用メモリ 59,960 KB
最終ジャッジ日時 2024-10-10 05:00:24
合計ジャッジ時間 8,532 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 54 ms
50,460 KB
testcase_01 AC 54 ms
50,252 KB
testcase_02 AC 54 ms
50,172 KB
testcase_03 AC 55 ms
50,384 KB
testcase_04 AC 56 ms
50,672 KB
testcase_05 AC 55 ms
50,280 KB
testcase_06 AC 57 ms
50,156 KB
testcase_07 AC 54 ms
50,128 KB
testcase_08 AC 55 ms
50,240 KB
testcase_09 AC 55 ms
50,520 KB
testcase_10 AC 216 ms
59,708 KB
testcase_11 AC 191 ms
59,960 KB
testcase_12 AC 239 ms
59,840 KB
testcase_13 AC 228 ms
59,016 KB
testcase_14 AC 237 ms
59,596 KB
testcase_15 AC 223 ms
59,584 KB
testcase_16 AC 186 ms
56,512 KB
testcase_17 AC 188 ms
55,856 KB
testcase_18 AC 185 ms
56,476 KB
testcase_19 AC 194 ms
56,288 KB
testcase_20 AC 159 ms
52,668 KB
testcase_21 AC 163 ms
52,780 KB
testcase_22 AC 152 ms
52,836 KB
testcase_23 AC 155 ms
52,980 KB
testcase_24 AC 157 ms
53,240 KB
testcase_25 AC 148 ms
52,952 KB
testcase_26 AC 145 ms
53,140 KB
testcase_27 AC 140 ms
52,804 KB
testcase_28 AC 131 ms
52,676 KB
testcase_29 AC 126 ms
52,452 KB
testcase_30 AC 111 ms
52,280 KB
testcase_31 AC 125 ms
52,684 KB
testcase_32 AC 124 ms
52,324 KB
testcase_33 AC 123 ms
52,560 KB
testcase_34 AC 115 ms
52,516 KB
testcase_35 AC 58 ms
50,148 KB
testcase_36 AC 53 ms
50,448 KB
testcase_37 AC 53 ms
50,476 KB
testcase_38 AC 53 ms
50,236 KB
testcase_39 AC 55 ms
50,532 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