結果

問題 No.654 Air E869120
ユーザー uafr_cs
提出日時 2018-02-23 23:55:31
言語 Java
(openjdk 23)
結果
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
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 35
権限があれば一括ダウンロードができます

ソースコード

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