Language: Java


    public Main(FastScanner in, PrintWriter out) {
        N = in.nextInt();
        M = in.nextInt();
        L = in.nextInt();

        adj = new List[N];
        for (int i = 0; i < N; i++)
            adj[i] = new ArrayList<>();

        for (int i = 0; i < M; i++) {
            int a = in.nextInt() - 1;
            int b = in.nextInt() - 1;
            int c = in.nextInt();
            adj[a].add(new int[]{b, c});
            adj[b].add(new int[]{a, c});
        }

        int Q = in.nextInt();
        int[] res = new int[Q];
        for (int q = 0; q < Q; q++) {
            int s = in.nextInt() - 1;
            int t = in.nextInt() - 1;
            res[q] = query(s, t);
        }
        for (int x : res)
            out.println(x);
    }

    int query(int s, int t) {
        int[] res = new int[N];
        res[s] = 0;
        Arrays.fill(res, N);
        PriorityQueue<int[]> q = new PriorityQueue<>(Comparator.comparingInt(a -> a[1]));
        q.add(new int[]{s, 0, L});
        while (!q.isEmpty()) {
            int[] st = q.remove();
            int u = st[0], cnt = st[1], fuel = st[2];
            if (u == t) break;

            if (res[u] < cnt) continue;

            for (int[] nxt  : adj[u]) {
                int v = nxt[0], c = nxt[1];
                if (c > fuel && c <= L && res[v] > cnt + 1) {
                    int ncnt = cnt + 1;
                    res[v] = ncnt;
                    q.add(new int[]{v, ncnt, L - c});
                } else if (c <= fuel && res[v] > cnt) {
                    res[v] = cnt;
                    q.add(new int[]{v, cnt, fuel - c});
                }
            }
        }
        return res[t] == N ? -1 : res[t];
    }


    private int gcd(int a, int b) {
        while (true) {
            if (b == 0) return a;
            int tmp = a;
            a = b;
            b = tmp % b;
        }
    }

    private long gcd(long a, long b) {
        while (true) {
            if (b == 0) return a;
            long tmp = a;
            a = b;
            b = tmp % b;
        }
    }

    private long powm(long x, long p) {
        long res = 1;
        while (p > 0) {
            if (p % 2 == 1)
                res = res * x % MOD;
            x = x * x % MOD;
            p /= 2;
        }
        return res;
    }

    public static void main(String[] args) {
        PrintWriter out = new PrintWriter(System.out);
        // Scanner in = new Scanner(
                // new BufferedReader(new InputStreamReader(System.in)));
        FastScanner in = new FastScanner(System.in);

        Main sol = new Main(in, out);
        out.close();
    }
}

// Pair
class Pair<T extends Comparable<? super T>, U extends Comparable<? super U>> implements Comparable<Pair<T, U>> {
    T a;
    U b;
    Pair() { }
    Pair(T a, U b) {
        this.a = a;
        this.b = b;
    }

    @Override
    public String toString() { return "("+a.toString()+", "+b.toString()+")"; }

    @Override
    public int hashCode() { return Objects.hash(a, b); }

    @Override
    public boolean equals(Object obj) {
        if(this == obj) return true;
        if(obj == null) return false;
        if(this.getClass() != obj.getClass()) return false;
        Pair that = (Pair) obj;
        if(this.a.getClass() != that.a.getClass()) return false;
        if(this.b.getClass() != that.b.getClass()) return false;
        if(!this.a.equals(that.a)) return false;
        if(!this.b.equals(that.b)) return false;
        return true;
    }

    @Override
    public int compareTo(Pair<T, U> that) {
        int c = (this.a).compareTo(that.a);
        if(c == 0) c = (this.b).compareTo(that.b);
        return c;
    }
}


class FastScanner{
    private InputStream stream;
    private byte[] buf = new byte[1024];
    private int curChar;
    private int numChars;

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

    int read()
    {
        if (numChars == -1)
            throw new InputMismatchException();
        if (curChar >= numChars){
            curChar = 0;
            try{
                numChars = stream.read(buf);
            } catch (IOException e) {
                throw new InputMismatchException();
            }
            if (numChars <= 0)
                return -1;
        }
        return buf[curChar++];
    }

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

    boolean isEndline(int c)
    {
        return c=='\n'||c=='\r'||c==-1;
    }

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

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

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

    String next(){
        int c = read();
        while (isSpaceChar(c))
            c = read();
        StringBuilder res = new StringBuilder();
        do{
            res.appendCodePoint(c);
            c = read();
        }while(!isSpaceChar(c));
        return res.toString();
    }

    String nextLine(){
        int c = read();
        while (isEndline(c))
            c = read();
        StringBuilder res = new StringBuilder();
        do{
            res.appendCodePoint(c);
            c = read();
        }while(!isEndline(c));
        return res.toString();
    }
}


Compiler output:
Main.java:2: error: class, interface, enum, or record expected
    public Main(FastScanner in, PrintWriter out) {
           ^
Main.java:4: error: class, interface, enum, or record expected
        M = in.nextInt();
        ^
Main.java:5: error: class, interface, enum, or record expected
        L = in.nextInt();
        ^
Main.java:7: error: class, interface, enum, or record expected
        adj = new List[N];
        ^
Main.java:8: error: class, interface, enum, or record expected
        for (int i = 0; i < N; i++)
        ^
Main.java:8: error: class, interface, enum, or record expected
        for (int i = 0; i < N; i++)
                        ^
Main.java:8: error: class, interface, enum, or record expected
        for (int i = 0; i < N; i++)
                               ^
Main.java:11: error: class, interface, enum, or record expected
        for (int i = 0; i < M; i++) {
        ^
Main.java:11: error: class, interface, enum, or record expected
        for (int i = 0; i < M; i++) {
                        ^
Main.java:11: error: class, interface, enum, or record expected
        for (int i = 0; i < M; i++) {
                               ^
Main.java:13: error: unnamed classes are a preview feature and are disabled by default.
            int b = in.nextInt() - 1;
            ^
  (use --enable-preview to enable unnamed classes)
Main.java:15: error: class, interface, enum, or record expected
            adj[a].add(new int[]{b, c});
            ^
Main.java:16: error: class, interface, enum, or record expected
            adj[b].add(new int[]{a, c});
            ^
Main.java:17: error: class, interface, enum, or record expected
        }
        ^
Main.java:21: error: class, interface, enum, or record expected
        for (int q = 0; q < Q; q++) {
        ^
Main.java:21: error: class, interface, enum, or record expected
        for (int q = 0; q < Q; q++) {
                        ^
Main.java:21: error: class, interface, enum, or record expected
        for (int q = 0; q < Q; q++) {
                               ^
Main.java:24: error: class, interface, enum, or record expected
            res[q] = query(s, t);
            ^
Main.java:25: error: class, interface, enum, or record expected
        }
        ^
Main.java:28: error: class, interface, enum, or record expected
    }
    ^
Main.java:32: error: class, interface, enum, or record expected
        res[s] = 0;
        ^
Main.java:33: error: class, interface, enum, or record expected
        Arrays.fill(res, N);
        ^
Main.java:35: error: class, interface, enum, or record expected
        q.add(new int[]{s, 0, L});
        ^
Main.java:36: error: class, interface, enum, or record expected
        while (!q.isEmpty()) {
        ^
Main.java:39: error: class, interface, enum, or record expected
            if (u == t) break;
            ^
Main.java:41: error: class, interface, enum, or record expected
            if (res[u] < cnt) continue;
            ^
Main.java:43: error: class, interface, enum, or record expected
            for (int[] nxt  : adj[u]) {
            ^
Main.java:45: error: class, interface, enum, or record expected
                if (c > fuel && c <= L && res[v] > cnt + 1) {
                ^
Main.java:47: error: class, interface, enum, or record expected
                    res[v] = ncnt;
                    ^
Main.java:48: error: class, interface, enum, or record expected
                    q.add(new int[]{v, ncnt, L - c});
                    ^
Main.java:49: error: class, interface, enum, or record expected
                } else if (c <= fuel && res[v] > cnt) {
                ^
Main.java:51: error: class, interface, enum, or record expected
                    q.add(new int[]{v, cnt, fuel - c});
                    ^
Main.java:52: error: class, interface, enum, or record expected
                }
                ^
Main.java:56: error: class, interface, enum, or record expected
    }
    ^
Main.java:63: error: class, interface, enum, or record expected
            a = b;
            ^
Main.java:64: error: class, interface, enum, or record expected
            b = tmp % b;
            ^
Main.java:65: error: class, interface, enum, or record expected
        }
        ^
Main.java:72: error: class, interface, enum, or record expected
            a = b;
            ^
Main.java:73: error: class, interface, enum, or record expected
            b = tmp % b;
            ^
Main.java:74: error: class, interface, enum, or record expected
        }
        ^
Main.java:79: error: class, interface, enum, or record expected
        while (p > 0) {
        ^
Main.java:82: error: class, interface, enum, or record expected
            x = x * x % MOD;
            ^
Main.java:83: error: class, interface, enum, or record expected
            p /= 2;
            ^
Main.java:84: error: class, interface, enum, or record expected
        }
        ^
Main.java:86: error: class, interface, enum, or record expected
    }
    ^
Main.java:97: error: class, interface, enum, or record expected
}
^
46 errors