> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Find the City With the Smallest Number of

> Tested Python solution for LeetCode 1334 with 16 pytest cases. Generate a practice environment with lcpy.

LeetCode 1334, [Medium](/catalog/medium). Topics: [Dynamic Programming](/catalog/topics/dynamic-programming), [Graph Theory](/catalog/topics/graph-theory), [Shortest Path](/catalog/topics/shortest-path), Dijkstra's Algorithm, Bellman-Ford Algorithm, Floyd-Warshall Algorithm. [View on LeetCode](https://leetcode.com/problems/find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance/description/).

Generate this problem as a practice environment: tested reference solution, 16 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 1334   # by problem number
lcpy gen -s find_the_city_with_the_smallest_number_of_neighbors_at_a_threshold_distance   # by problem name
```

## Problem

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.

### Examples

![Example 1](https://assets.leetcode.com/uploads/2024/08/23/problem1334example0.png)

```
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.
The neighboring cities at a distanceThreshold = 4 for each city are:
City 0 -> [City 1, City 2]
City 1 -> [City 0, City 2, City 3]
City 2 -> [City 0, City 1, City 3]
City 3 -> [City 1, City 2]
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](https://assets.leetcode.com/uploads/2024/08/23/problem1334example1.png)

```
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.
The neighboring cities at a distanceThreshold = 2 for each city are:
City 0 -> [City 1]
City 1 -> [City 0, City 4]
City 2 -> [City 3, City 4]
City 3 -> [City 2, City 4]
City 4 -> [City 1, City 2, City 3]
The city 0 has 1 neighboring city at a distanceThreshold = 2.
```

### Constraints

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

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/find_the_city_with_the_smallest_number_of_neighbors_at_a_threshold_distance/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/find_the_city_with_the_smallest_number_of_neighbors_at_a_threshold_distance/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    def find_the_city(self, n: int, edges: list[list[int]], distance_threshold: int) -> int:
        inf = 10**9
        dist = [[inf] * n for _ in range(n)]
        for i in range(n):
            dist[i][i] = 0
        for u, v, w in edges:
            dist[u][v] = w
            dist[v][u] = w
        for k in range(n):
            for i in range(n):
                for j in range(n):
                    if dist[i][k] + dist[k][j] < dist[i][j]:
                        dist[i][j] = dist[i][k] + dist[k][j]
        best_city, best_count = -1, n + 1
        for i in range(n):
            count = sum(1 for j in range(n) if j != i and dist[i][j] <= distance_threshold)
            if count <= best_count:
                best_count = count
                best_city = i
        return best_city
```

## Complexity

| Time | Space |
| - | - |
| - | - |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.