Dijkstra's Algorithm || C++ Easy Solution || Beginner Friendly
\n\n# Code\n\nclass Solution {\npublic:\n    vector<bool> distanceLimitedPathsExist(int n, vector<vector<int>>& edges, vector<vector<int>>& queries) {\n        
\n\n# Code\n```\nclass Solution {\npublic:\n    vector<bool> distanceLimitedPathsExist(int n, vector<vector<int>>& edges, vector<vector<int>>& queries) {\n        for(int i = 0; i<=n; i++) parent[i] = i;\n        \n\t\t// Sorting the edges array based on dist of each pair of node. \n        sort(edges.begin(),edges.end(),[](vector<int>&a, vector<int>&b){\n            if(a[2]<b[2]) return true;\n            return false;\n        });\n\n\t\t// We will need the indices of query elements coz after sorting, the order will change. \n\t\t// So we push the index in the same element vector of query.\n        for(int i = 0; i<queries.size(); i++) queries[i].push_back(i);\n\t\t\t\n\t\t// Sorting queries based on limits. \n        sort(queries.begin(),queries.end(),[](vector<int>&a,vector<int>&b){\n            if(a[2]<b[2]) return true;\n            return false;\n        });\n        \n        vector <bool> ans(queries.size(),false);\n        int idx = 0;\n        for(int i = 0; i<queries.size(); i++){\n            // Here we loop on edges vector and join the two nodes having dist < curr_limit.\n\t\t\twhile(idx<edges.size() and edges[idx][2]<queries[i][2]){\n                join(edges[idx][0],edges[idx][1]);\n                idx++;\n            }\n\t\t\t// If the two nodes of current query has same godfather, we set this queries ans as true\n            if(find(parent[queries[i][0]]) == find(parent[queries[i][1]])) ans[queries[i][3]] = true;\n        }\n        return ans;\n    }\nprotected:\n    int find(int x){\n        if(parent[x]==x) return x;\n        return parent[x] = find(parent[x]);\n    }\n    void join(int a, int b){\n        a = find(a);\n        b = find(b);\n        \n        if(a!=b) parent[b] = a;\n    }\n};\n```
['Array', 'Math', 'Union Find', 'Sorting', 'C++']