diff --git a/1334. Find the City With the Smallest Number of Neighbors at a Threshold Distance b/1334. Find the City With the Smallest Number of Neighbors at a Threshold Distance new file mode 100644 index 0000000..d9539d6 --- /dev/null +++ b/1334. Find the City With the Smallest Number of Neighbors at a Threshold Distance @@ -0,0 +1,44 @@ +class Solution { + void dijsktra(int src,vector>> &adj,vector> &dist){ + priority_queue> pq; + dist[src][src]=0; + pq.push({0,src}); + pair crr; + int d; + while(!pq.empty()){ + crr= pq.top(); + pq.pop(); + for(auto &node:adj[crr.second]){ + if((-1*crr.first) + node.second<=dist[src][node.first]){ + d= (-1*crr.first)+node.second; + dist[src][node.first]=d; + pq.push({-1*d,node.first}); + } + } + } + } +public: + int findTheCity(int n, vector>& edges, int distanceThreshold) { + vector>> adj(n); + for(auto &edge:edges){ + adj[edge[0]].push_back({edge[1],edge[2]}); + adj[edge[1]].push_back({edge[0],edge[2]}); + } + vector> dist(n,vector (n,INT_MAX)); + for(int i=0;i