結果

問題 No.654 Air E869120
ユーザー denderaKawazudenderaKawazu
提出日時 2019-01-24 18:00:07
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 7,116 bytes
コンパイル時間 2,397 ms
コンパイル使用メモリ 88,768 KB
実行使用メモリ 58,688 KB
最終ジャッジ日時 2023-10-14 09:40:52
合計ジャッジ時間 7,826 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 52 ms
50,472 KB
testcase_01 AC 55 ms
50,912 KB
testcase_02 AC 49 ms
50,764 KB
testcase_03 AC 53 ms
50,936 KB
testcase_04 AC 51 ms
50,768 KB
testcase_05 AC 56 ms
50,928 KB
testcase_06 AC 51 ms
51,212 KB
testcase_07 AC 50 ms
49,952 KB
testcase_08 AC 53 ms
50,664 KB
testcase_09 AC 51 ms
50,992 KB
testcase_10 AC 144 ms
58,688 KB
testcase_11 AC 114 ms
56,264 KB
testcase_12 AC 129 ms
56,424 KB
testcase_13 AC 133 ms
56,000 KB
testcase_14 AC 123 ms
56,360 KB
testcase_15 AC 130 ms
55,812 KB
testcase_16 AC 122 ms
56,048 KB
testcase_17 AC 119 ms
55,940 KB
testcase_18 AC 120 ms
56,372 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 105 ms
54,428 KB
testcase_23 AC 109 ms
56,052 KB
testcase_24 WA -
testcase_25 AC 103 ms
55,124 KB
testcase_26 AC 107 ms
54,336 KB
testcase_27 AC 96 ms
53,992 KB
testcase_28 AC 105 ms
55,124 KB
testcase_29 AC 105 ms
54,600 KB
testcase_30 AC 84 ms
53,796 KB
testcase_31 AC 80 ms
53,488 KB
testcase_32 AC 98 ms
53,760 KB
testcase_33 AC 79 ms
53,440 KB
testcase_34 AC 81 ms
53,440 KB
testcase_35 AC 49 ms
50,452 KB
testcase_36 AC 53 ms
50,984 KB
testcase_37 AC 49 ms
51,876 KB
testcase_38 AC 51 ms
50,932 KB
testcase_39 AC 51 ms
51,000 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Collection;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Queue;
import java.util.Comparator;
import java.util.ArrayDeque;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        FastScanner in = new FastScanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        Task solver = new Task();
        solver.solve(1, in, out);
        out.close();
    }

    static class Task {
        public void solve(int testNumber, FastScanner in, PrintWriter out) {
            int n = in.nextInt();
            int m = in.nextInt();
            int d = in.nextInt();

            Task.Schedule[] schedules = new Task.Schedule[m];
            for (int i = 0; i < m; i++)
                schedules[i] = new Task.Schedule(in.nextInt(), in.nextInt(), in.nextInt(), in.nextInt(), in.nextLong());
            Arrays.sort(schedules, Comparator.comparing(Task.Schedule::getP));

            Dinic dinic = new Dinic(m + 2);
            for (int i = 0; i < m; i++) {
                Task.Schedule cs = schedules[i];
                int t = cs.getQ() + d;
                if (cs.getU() == 1)
                    dinic.setEdge(m, i, cs.getW());
                if (cs.getV() == n) {
                    dinic.setEdge(i, m + 1, cs.getW());
                    continue;
                }
                for (int j = m - 1; schedules[j].getP() >= t; j--) {
                    if (schedules[j].getU() == cs.getV()) {
                        dinic.setEdge(i, j, cs.getW());
                    }
                }
            }

            out.println(dinic.getMaxFlow(m, m + 1));
        }

        static class Schedule {
            int u;
            int v;
            int p;
            int q;
            long w;

            Schedule(int u, int v, int p, int q, long w) {
                this.u = u;
                this.v = v;
                this.p = p;
                this.q = q;
                this.w = w;
            }

            public int getU() {
                return u;
            }

            public int getV() {
                return v;
            }

            public int getP() {
                return p;
            }

            public int getQ() {
                return q;
            }

            public long getW() {
                return w;
            }

        }

    }

    static class FastScanner {
        private InputStream in;
        private byte[] buffer = new byte[1024];
        private int bufPointer;
        private int bufLength;

        public FastScanner(InputStream in) {
            this.in = in;
        }

        private int readByte() {
            if (bufPointer >= bufLength) {
                if (bufLength == -1)
                    throw new InputMismatchException();

                bufPointer = 0;
                try {
                    bufLength = in.read(buffer);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }

                if (bufLength <= 0)
                    return -1;
            }
            return buffer[bufPointer++];
        }

        private static boolean isSpaceChar(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        public long nextLong() {
            long n = 0;

            int b = readByte();
            while (isSpaceChar(b))
                b = readByte();

            boolean minus = (b == '-');
            if (minus)
                b = readByte();

            while (b >= '0' && b <= '9') {
                n *= 10;
                n += b - '0';
                b = readByte();
            }

            if (!isSpaceChar(b))
                throw new NumberFormatException();

            return minus ? -n : n;
        }

        public int nextInt() {
            long n = nextLong();

            if (n < Integer.MIN_VALUE || n > Integer.MAX_VALUE)
                throw new NumberFormatException();

            return (int) n;
        }

    }

    static class Dinic {
        private ArrayList<Dinic.Edge>[] graph;
        private int[] dist;
        private int[] start;

        public Dinic(int vertexNum) {
            dist = new int[vertexNum];
            start = new int[vertexNum];
            graph = new ArrayList[vertexNum];
            for (int i = 0; i < vertexNum; i++)
                graph[i] = new ArrayList<>();
        }

        public void setEdge(int from, int to, long cap) {
            Dinic.Edge e = new Dinic.Edge(to, cap);
            e.rev = new Dinic.Edge(from, 0, e);
            graph[from].add(e);
            graph[to].add(e.rev);
        }

        public long getMaxFlow(int source, int sink) {
            long flow = 0;
            while (true) {
                setDistBFS(source);
                if (dist[sink] == -1)
                    return flow;
                Arrays.fill(start, 0);
                long df = sendFlowDFS(source, sink, Long.MAX_VALUE);
                while (df > 0) {
                    flow += df;
                    df = sendFlowDFS(source, sink, Long.MAX_VALUE);
                }
            }
        }

        private void setDistBFS(int zero) {
            Arrays.fill(dist, -1);
            dist[zero] = 0;
            Queue<Integer> vq = new ArrayDeque<>();
            vq.add(zero);
            while (!vq.isEmpty()) {
                int cv = vq.poll();
                for (Dinic.Edge e : graph[cv]) {
                    if (e.cap > 0 && dist[e.to] == -1) {
                        dist[e.to] = dist[cv] + 1;
                        vq.add(e.to);
                    }
                }
            }
        }

        private long sendFlowDFS(int cv, int source, long flow) {
            if (cv == source)
                return flow;
            while (start[cv] < graph[cv].size()) {
                Dinic.Edge e = graph[cv].get(start[cv]);
                if (e.cap > 0 && dist[cv] < dist[e.to]) {
                    long d = sendFlowDFS(e.to, source, Math.min(e.cap, flow));
                    if (d > 0) {
                        e.cap -= d;
                        e.rev.cap += d;
                        return d;
                    }
                }
                start[cv]++;
            }
            return 0;
        }

        private static class Edge {
            int to;
            Dinic.Edge rev;
            long cap;

            Edge(int to, long cap) {
                this.to = to;
                this.cap = cap;
            }

            Edge(int to, long cap, Dinic.Edge rev) {
                this.to = to;
                this.cap = cap;
                this.rev = rev;
            }

        }

    }
}

0