Java Solution
First solution is a BFS solution which gives TLE: \n\nclass Solution {\n    public boolean[] distanceLimitedPathsExist(int n, int[][] edgeList, int[][] queries)
**First solution is a BFS solution which gives TLE: **\n\nclass Solution {\n    public boolean[] distanceLimitedPathsExist(int n, int[][] edgeList, int[][] queries) {\n        boolean[] output = new boolean[queries.length];\n        \n        Graph g = new Graph(n);\n        \n        for (int i=0; i<edgeList.length; i++) {\n            g.addEdge(edgeList[i][0], edgeList[i][1], edgeList[i][2]);\n        }\n        \n        for (int i=0; i<queries.length; i++) {\n            int source = queries[i][0];\n            int dest = queries[i][1];\n            int limit = queries[i][2];\n            boolean[] visited = new boolean[n];\n            \n            PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> a[1] - b[1]);\n            \n            visited[source] = true;\n            for (int j=0; j<g.arr.get(source).size(); j++) {\n                int v = g.arr.get(source).get(j).v;\n                int w = g.arr.get(source).get(j).weight;\n                \n                if (w < limit) {\n                    pq.add(new int[]{v, w});\n                }\n                \n            }\n            \n            pq.add(new int[]{source, 0});\n            \n            while (!pq.isEmpty()) {\n                int[] current = pq.remove();\n                \n                int u = current[0];\n                int dist = current[1];\n                \n                if (u == dest) {\n                    output[i] = true;\n                    break;\n                }\n                visited[u] = true;\n                \n                \n               // System.out.println(u);\n                for (int j=0; j<g.arr.get(u).size(); j++) {\n                    int v = g.arr.get(u).get(j).v;\n                    int w = g.arr.get(u).get(j).weight;\n                \n                    \n                    \n                    if (!visited[v] && w < limit) {\n                       // System.out.println(v + " " + w);\n                        pq.add(new int[]{v, w});\n                    }\n                } \n            }\n        }\n        \n        return output;\n    }\n}\n\nclass Node {\n    int v;\n    int weight;\n    \n    public Node (int v, int weight) {\n        this.v = v;\n        this.weight = weight;\n    } \n}\n\nclass Graph{\n    int V;\n    List<List<Node>> arr;\n    \n    public Graph(int v) {\n        V = v;\n        \n        arr = new ArrayList<>();\n        \n        for (int i=0; i<v; i++) {\n            arr.add(new ArrayList<>());\n        }\n    }\n    \n    public void addEdge(int u, int v, int w) {\n        arr.get(u).add(new Node(v, w));\n        arr.get(v).add(new Node(u, w));\n    }\n}\n\n**Next solution uses Union Find and DSU algorthm which is clean and accepted - **\n\nclass Solution {\n    public boolean[] distanceLimitedPathsExist(int n, int[][] edgeList, int[][] queries) {\n        UnionFind uf = new UnionFind(n);\n        \n        Arrays.sort(edgeList, (a, b) -> a[2] - b[2]);\n        \n        int[][] sortedQueries = new int[queries.length][4];\n        \n        for (int i=0; i<queries.length; i++) {\n            sortedQueries[i][0] = queries[i][0];\n            sortedQueries[i][1] = queries[i][1];\n            sortedQueries[i][2] = queries[i][2];\n            sortedQueries[i][3] = i;\n        }\n        \n        Arrays.sort(sortedQueries, (a, b) -> a[2] - b[2]);\n        \n        int start = 0;\n        boolean[] output = new boolean[queries.length];\n        \n        for (int i=0; i<sortedQueries.length; i++) {\n            int[] query = sortedQueries[i];\n            \n            int w = query[2];\n            \n            while (start < edgeList.length && edgeList[start][2] < w) {\n                uf.union(edgeList[start][0], edgeList[start][1]);\n                start++;\n            }\n            \n            if (uf.find(query[0]) == uf.find(query[1])) {\n                output[query[3]] = true;\n            }\n        }\n        \n        return output;\n    }\n}\n\nclass UnionFind {\n    int[] parent;\n    \n    public UnionFind(int n) {\n        parent = new int[n];\n        \n        for (int i=0; i<n; i++) {\n            parent[i] = i;\n        }\n    }\n    \n    public int find(int x) {\n        while (x != parent[x]) {\n            x = parent[x];\n        }\n        \n        return x;\n    }\n    \n    public void union(int x, int y) {\n        int xParent = find(x);\n        int yParent = find(y);\n        \n        parent[xParent] = yParent;\n    }\n}\n\n
['Breadth-First Search', 'Union Find', 'Heap (Priority Queue)']