class Solution:
    def findTheCity(self, n: int, edges: List[List[int]], distanceThreshold: int) -> int:

        matrix = [[float('inf')] * n for _ in range(n)]
        
        # initializing the matrix
        for u, v, w in edges:
            matrix[u][v] = w
            matrix[v][u] = w

        for u in range(n):
            matrix[u][u] = 0
        
        for k in range(n):
            for i in range(n):
                for j in range(n):
                    if matrix[i][j] > matrix[i][k] + matrix[k][j]:
                        matrix[i][j] = matrix[i][k] + matrix[k][j]
        
        # counting reachable cities(neighbor) for every single city 
        res = [0] * n
        for i in range(len(matrix)):
            for j in range(len(matrix[0])):
                if 0 < matrix[i][j] <= distanceThreshold:
                    res[j] += 1
        return [i for i, x in enumerate(res) if x == min(res)][-1]
class Solution {
    public int findTheCity(int n, int[][] edges, int distanceThreshold) {
        int[][] graph = new int[n][n];
        for(int[] row: graph)
            Arrays.fill(row, Integer.MAX_VALUE);
        
        for(int i = 0; i < n; i++)
            graph[i][i] = 0;
        
        for(int[] edge: edges)
        {
            graph[edge[0]][edge[1]] = edge[2];
            graph[edge[1]][edge[0]] = edge[2];
        }
        
        for(int k = 0; k < n; k++)
        {
            for(int i = 0; i < n; i++)
            {
                for(int j = 0; j < n; j++)
                {
                    if(graph[i][k] != Integer.MAX_VALUE && graph[k][j] != Integer.MAX_VALUE)
                    {
                        if(graph[i][k] + graph[k][j] < graph[i][j])
                            graph[i][j] = graph[i][k] + graph[k][j];
                    }
                }
            }
        }
        
        int ans = 0;
        int city = Integer.MAX_VALUE;;
        
        for(int i = 0; i < n; i++)
        {
            int current = 0;
            for(int j = 0; j < n; j++)
            {
                if(i != j && graph[i][j] <= distanceThreshold)
                    current++;
            }
            if(current <= city)
            {
                ans = i;
                city = current;
            }
        }
        
        return ans;
    }
}
Find the City With the Smallest Number of Neighbors at a Threshold Distance
There are n cities numbered from 0 to n-1. Given the array edges where edges[i] = [fromi, toi, weighti] represents a bidirectional and weighted edge between cities fromi and toi, and given the integer distanceThreshold.

Return the city with the smallest number of cities that are reachable through some path and whose distance is at most distanceThreshold, If there are multiple such cities, return the city with the greatest number.

Notice that the distance of a path connecting cities i and j is equal to the sum of the edges' weights along that path.

&nbsp;
Example 1:

Input: n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4
Output: 3
Explanation: The figure above describes the graph.&nbsp;
The neighboring cities at a distanceThreshold = 4 for each city are:
City 0 -&gt; [City 1, City 2]&nbsp;
City 1 -&gt; [City 0, City 2, City 3]&nbsp;
City 2 -&gt; [City 0, City 1, City 3]&nbsp;
City 3 -&gt; [City 1, City 2]&nbsp;
Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number.


Example 2:

Input: n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2
Output: 0
Explanation: The figure above describes the graph.&nbsp;
The neighboring cities at a distanceThreshold = 2 for each city are:
City 0 -&gt; [City 1]&nbsp;
City 1 -&gt; [City 0, City 4]&nbsp;
City 2 -&gt; [City 3, City 4]&nbsp;
City 3 -&gt; [City 2, City 4]
City 4 -&gt; [City 1, City 2, City 3]&nbsp;
The city 0 has 1 neighboring city at a distanceThreshold = 2.


&nbsp;
Constraints:


	2 &lt;= n &lt;= 100
	1 &lt;= edges.length &lt;= n * (n - 1) / 2
	edges[i].length == 3
	0 &lt;= fromi &lt; toi &lt; n
	1 &lt;= weighti,&nbsp;distanceThreshold &lt;= 10^4
	All pairs (fromi, toi) are distinct.


var findTheCity = function(n, edges, distanceThreshold) {
    const dp = Array.from({length: n}, (_, i) => {
        return Array.from({length: n}, (_, j) => i == j ? 0 : Infinity)
    });

    for(let edge of edges) {
        const [a, b, w] = edge;
        dp[a][b] = dp[b][a] = w;
    }

    // k is intermediate node
    for(let k = 0; k < n; k++) {
        // fix distances from i to other nodes with k as intermediate
        for(let i = 0; i < n; i++) {
            if(i == k || dp[i][k] == Infinity) continue;
            for(let j = i + 1; j < n; j++) {
                dp[i][j] = dp[j][i] = Math.min(dp[i][j], dp[i][k] + dp[k][j]);
            }
        }
    };

    let city = -1, minCity = n;
    for(let i = 0; i < n; i++) {
        let count = 0;
        for(let j = 0; j < n; j++) {
            if(i == j) continue;
            if(dp[i][j] <= distanceThreshold) count++;
        }
        if(count <= minCity) {
            minCity = count;
            city = i;
        }
    }

    return city;
};
class Solution {
public:
unordered_map<int,int>m;
    int findTheCity(int n, vector<vector<int>>& edges, int threshold) {
        vector<vector<pair<int,int>>>adj(n);
        for(auto x:edges)
        {
            adj[x[0]].push_back({x[1],x[2]});
            adj[x[1]].push_back({x[0],x[2]});
        }
        int ind=-1,ans=1e8;
       for(int i=0;i<n;i++)
       {
           queue<pair<int,int>>q;
           q.push({i,0});
           vector<int>dis(n,1e8);
           dis[i]=0;
           while(!q.empty())
           {
               int node=q.front().first;
               int wt=q.front().second;
               q.pop();
               for(auto x:adj[node])
               {
                   int node1=x.first;
                   int wt1=x.second;
                   if(wt+wt1<dis[node1])
                   {
                       dis[node1]=wt+wt1;
                       q.push({node1,wt+wt1});
                   }
               }
           }
           int c=0;
           for(int i=0;i<dis.size();i++)
           {
               if(dis[i]<=threshold)
               {
                   c++;
               }
           }
           if(c<=ans)
           {
               ans=c;
               ind=max(ind,i);
           }
           
       }
       return ind;


    }
};