Find the City With the Smallest Number of Neighbors at a Threshold Distance Problem
Description
LeetCode Problem 1334.
There are n cities numbered from 0 to n-1. Given the array edges where edges[i] = [from_i, to_i, weight_i] represents a bidirectional and weighted edge between cities from_i and to_i, 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.
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.
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:

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 <= 1001 <= edges.length <= n * (n - 1) / 2edges[i].length == 30 <= from_i < to_i < n1 <= weight_i, distanceThreshold <= 10^4- All pairs
(from_i, to_i)are distinct.
Solution
Approach: Floyd-Warshall
To count, for every city, how many others are within distanceThreshold, we need the shortest distance between every pair of cities. With at most 100 cities, the Floyd-Warshall algorithm does this simply.
Start with dist[i][j] equal to the weight of the edge between i and j, 0 when i == j, and infinity when there is no edge. Then, for each city k in turn, allow paths to pass through k: for every pair (i, j), set dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]). After the round for k, dist[i][j] is the shortest path that only uses cities 0..k along the way, so after the last round it is the true shortest distance.
Note that a city can be reachable within the threshold through a path of several edges even when a direct edge is longer, which is why we cannot just look at the edges.
Then, for each city i, count the cities j != i with dist[i][j] <= distanceThreshold. To break ties in favour of the larger city number, scan the cities in increasing order and replace the best city whenever the count is smaller or equal.
Complexity: O(n3) time and O(n2) space for the distance table.
class Solution:
def findTheCity(self, n: int, edges: List[List[int]], distanceThreshold: int) -> int:
INF = float("inf")
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] = dist[v][u] = w
# Floyd-Warshall: after round k, paths may pass through cities 0..k.
for k in range(n):
dk = dist[k]
for i in range(n):
dik = dist[i][k]
if dik == INF:
continue
di = dist[i]
for j in range(n):
if dik + dk[j] < di[j]:
di[j] = dik + dk[j]
best_city, best_count = -1, INF
for i in range(n):
count = sum(1 for j in range(n) if j != i and dist[i][j] <= distanceThreshold)
if count <= best_count: # "<=" so that a tie goes to the larger city
best_city, best_count = i, count
return best_city