Submission #3566122


Source Code Expand

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
import java.util.function.Predicate;

public class Main {

    static int N, M, R, T;
    static Edge[] E;

    public static void main(String[] args) {
        FastScanner sc = new FastScanner(System.in);
        N = sc.nextInt();
        M = sc.nextInt();
        R = sc.nextInt();
        T = sc.nextInt();
        E = new Edge[M];
        for (int i = 0; i < M; i++) {
            E[i] = new Edge(sc.nextInt()-1, sc.nextInt()-1, sc.nextInt());
        }

        System.out.println(solve());
    }

    static long solve() {
        Edge[][] G = adjB(N, E);
        State[] dist = new State[N];
        PriorityQueue<State> q = new PriorityQueue<>();

        long ans = 0;
        for (int i = 0; i < N; i++) {
            ans += count(i, G, dist, q);
        }
        return ans;
    }

    static int count(int goal, Edge[][] G, State[] dist, PriorityQueue<State> q) {
        Arrays.fill(dist, null);

        State start = new State(goal, 0);
        q.add( start );
        dist[goal] = start;

        while(!q.isEmpty()) {
            State s = q.poll();
            if( dist[s.a].d != s.d ) continue;

            for (Edge e : G[s.a]) {
                State next = new State(e.opposite(s.a), s.d + e.c);
                if( dist[next.a] == null || dist[next.a].d > next.d ) {
                    dist[next.a] = next;
                    q.add( next );
                }
            }
        }

        // td/T < rd/R となる個数を数える
        Arrays.sort(dist);
        int cnt = 0;
        if( R >= T ) {
            for (int ti = 1; ti < N; ti++) {
                int td = dist[ti].d;

                // td/T < rd/R => td*R < rd*T
                int rLoseMin = findMinimum(dist, ti+1, N, r -> td * R < r.d * T );
                cnt += N - rLoseMin;
            }

        } else {
            for (int ri = 1; ri < N; ri++) {
                int rd = dist[ri].d;
                // 遠すぎて亀が負ける場所を数える
                int tWinMax = findMaximum(dist, ri+1, N, t -> t.d * R < rd * T );
                cnt += N - tWinMax - 1;
            }
        }
        return cnt;
    }

    static <A> int findMaximum(A[] array, int lo, int hi, Predicate<A> p) {
        while( lo < hi ) {
            int mid = ((hi - lo) >>> 1) + lo;
            if( p.test(array[mid]) ) {
                lo = mid + 1;
            } else {
                hi = mid;
            }
        }
        return lo - 1;
    }

    static <A> int findMinimum(A[] array, int lo, int hi, Predicate<A> p) {
        while( lo < hi ) {
            int mid = ((hi - lo) >>> 1) + lo;
            if( p.test(array[mid]) ) {
                hi = mid;
            } else {
                lo = mid + 1;
            }
        }
        return lo;
    }

    static Edge[][] adjB(int n, Edge[] E) {
        Edge[][] adj = new Edge[n][];
        int[] cnt = new int[n];
        for (Edge e : E) {
            cnt[e.a]++;
            cnt[e.b]++;
        }
        for (int i = 0; i < n; i++) {
            adj[i] = new Edge[cnt[i]];
        }
        for (Edge e : E) {
            adj[e.a][--cnt[e.a]] = e;
            adj[e.b][--cnt[e.b]] = e;
        }
        return adj;
    }

    static class State implements Comparable<State> {
        int a, d;

        public State(int a, int d) {
            this.a = a;
            this.d = d;
        }

        @Override
        public int compareTo(State o) {
            return Integer.compare(d, o.d);
        }
    }

    static class Edge {
        int a, b, c;

        public Edge(int a, int b, int c) {
            this.a = a;
            this.b = b;
            this.c = c;
        }

        public int opposite(int n) {
            return n == a ? b : a;
        }
    }

    @SuppressWarnings("unused")
    static class FastScanner {
        private BufferedReader reader;
        private StringTokenizer tokenizer;

        FastScanner(InputStream in) {
            reader = new BufferedReader(new InputStreamReader(in));
            tokenizer = null;
        }

        String next() {
            if (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        String nextLine() {
            if (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    return reader.readLine();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken("\n");
        }

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

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

        int[] nextIntArray(int n) {
            int[] a = new int[n];
            for (int i = 0; i < n; i++)
                a[i] = nextInt();
            return a;
        }

        long[] nextLongArray(int n) {
            long[] a = new long[n];
            for (int i = 0; i < n; i++)
                a[i] = nextLong();
            return a;
        }
    }
}

Submission Info

Submission Time
Task C - ウサギとカメ
User kusomushi
Language Java8 (OpenJDK 1.8.0)
Score 0
Code Size 5689 Byte
Status WA
Exec Time 2846 ms
Memory 94108 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 100
Status
AC × 2
AC × 15
WA × 3
Set Name Test Cases
Sample subtask0_sample-01.txt, subtask0_sample-02.txt
All subtask0_sample-01.txt, subtask0_sample-02.txt, subtask1_01.txt, subtask1_02.txt, subtask1_03.txt, subtask1_04.txt, subtask1_05.txt, subtask1_06.txt, subtask1_07.txt, subtask1_08.txt, subtask1_09.txt, subtask1_10.txt, subtask1_11.txt, subtask1_12.txt, subtask1_13.txt, subtask1_14.txt, subtask1_15.txt, subtask1_16.txt
Case Name Status Exec Time Memory
subtask0_sample-01.txt AC 148 ms 25172 KB
subtask0_sample-02.txt AC 155 ms 26448 KB
subtask1_01.txt AC 171 ms 24788 KB
subtask1_02.txt AC 160 ms 24148 KB
subtask1_03.txt AC 160 ms 25044 KB
subtask1_04.txt AC 146 ms 26196 KB
subtask1_05.txt AC 221 ms 27476 KB
subtask1_06.txt WA 233 ms 30020 KB
subtask1_07.txt AC 358 ms 46600 KB
subtask1_08.txt WA 397 ms 44168 KB
subtask1_09.txt AC 1305 ms 68340 KB
subtask1_10.txt AC 1247 ms 46160 KB
subtask1_11.txt AC 870 ms 53672 KB
subtask1_12.txt WA 2678 ms 59092 KB
subtask1_13.txt AC 2606 ms 46904 KB
subtask1_14.txt AC 2846 ms 59108 KB
subtask1_15.txt AC 2047 ms 94108 KB
subtask1_16.txt AC 2785 ms 55636 KB