From 8f592d5767dd421c2dc41849cd8af93fb5732712 Mon Sep 17 00:00:00 2001 From: chayan das <110921638+Chayandas07@users.noreply.github.com> Date: Fri, 26 Jul 2024 23:27:38 +0530 Subject: [PATCH] Create 1334. Find the City With the Smallest Number of Neighbors at a Threshold Distance --- ...umber of Neighbors at a Threshold Distance | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 1334. Find the City With the Smallest Number of Neighbors at a Threshold Distance 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