Submission #119683

#TimeUsernameProblemLanguageResultExecution timeMemory
119683DS007Jakarta Skyscrapers (APIO15_skyscraper)Java
0 / 100
122 ms9972 KiB
import java.util.*;
import java.io.*;
import java.math.*;

class Skyscraper {

    static class FastReader {
        StringTokenizer str;
        BufferedReader br;

        FastReader() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        String next() {
            if (str == null || !str.hasMoreTokens()) {
                try {
                    str = new StringTokenizer(br.readLine());
                } catch (Exception e) {
                    return "";
                }
            }
            return str.nextToken();
        }

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

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

        double nextDouble() {
            return Double.parseDouble(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;
        }
    }

    static class Node {
        int vertex, dist;

        Node(int a, int b) {
            vertex = a;
            dist = b;
        }

        int getDist() {
            return dist;
        }
    }

    static class Graph {

        LinkedList<Node> adj[];
        int n;
        int distance[];
        HashSet<Integer> settled;
        PriorityQueue<Node> pq;

        Graph(int n) {
            this.n = n;
            adj = new LinkedList[n];
            distance = new int[n];
            Arrays.fill(distance, Integer.MAX_VALUE);
            settled = new HashSet<>(n);
            pq = new PriorityQueue<>(Comparator.comparing(Node::getDist));
            for (int i = 0; i < n; i++)
                adj[i] = new LinkedList<>();
        }

        void addEdge(int a, int b, int dist) {
            adj[a].add(new Node(b, dist));
        }

        int solve(int u, int v) {
            distance[u] = 0;
            pq.add(new Node(u, 0));

            while (!settled.contains(v)) {
                int p = pq.remove().vertex;
                settled.add(p);
                check_neighbours(p);
            }

            if (distance[v] == Integer.MAX_VALUE)
                return -1;
            else
                return distance[v];
        }

        void check_neighbours(int v) {
            for (Node i : adj[v]) {
                if (!settled.contains(i.vertex)) {
                    int newDist = distance[v] + i.dist;
                    distance[i.vertex] = Math.min(distance[i.vertex], newDist);
                    pq.add(new Node(i.vertex, distance[i.vertex]));
                }
            }
        }
    }

    public static void main(String args[]) throws Exception {

        FastReader fr = new FastReader();
        int n = fr.nextInt(), m = fr.nextInt(), p0 = -1, p1 = -1;
        Graph g = new Graph(n);

        for (int i = 0; i < m; i++) {
            int b = fr.nextInt(), p = fr.nextInt();
            if (i == 0)
                p0 = b;
            if (i == 1)
                p1 = b;
            for (int j = b + p, k = 1; j < n; j += p, k++)
                g.addEdge(b, j, k);
            for (int j = b - p, k = 1; j >= 0; j -= p, k++)
                g.addEdge(b, j, k);
        }

        System.out.print(g.solve(p0, p1));
    }
}

Compilation message (stderr)

Note: skyscraper.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...