Skip to content
AI360Xpert
Beta
Difficulty: MediumAdvanced Graphs

Find the City With the Smallest Number of Neighbors at a Threshold Distance

Problem in Plain English

Find the city with the fewest other cities reachable within a distance threshold in an undirected weighted graph. Break ties by choosing the largest city number.

Problem Statement

There are n cities numbered from 0 to n - 1. Each entry [u, v, weight] in edges is a bidirectional road. A path’s distance is the sum of its road weights.

Return the city with the smallest count of other cities reachable by a path of total distance at most distanceThreshold. If several cities share the minimum count, return the largest index. A city does not count itself; unreachable cities never count.

Constraints

  • 2 <= n <= 100
  • 1 <= edges.length <= n * (n - 1) / 2; each edge has three integers.
  • 0 <= u < v < n; all endpoint pairs are distinct.
  • 1 <= weight, distanceThreshold <= 10^4

Examples

Example 1

Input
n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4
Output
3

The reachable sets excluding self are {1,2}, {0,2,3}, {0,1,3}, {1,2}. Cities 0 and 3 each have two neighbors; the larger index 3 wins.

Example 2

Input
n = 2, edges = [[0,1,7]], distanceThreshold = 6
Output
1

The only road exceeds the threshold. Both cities count zero neighbors, so the tie chooses 1.

Example 3

Input
n = 4, edges = [[0,1,1],[1,2,1]], distanceThreshold = 2
Output
3

Cities 0, 1 and 2 each reach the other two within distance 2. Isolated city 3 reaches nobody, so it uniquely minimizes the count.

Intuition

A road may be expensive even when a cheaper indirect route exists. In Example 1, the road from 1 to 3 costs 4, but traveling through 2 costs 2. Floyd-Warshall stores distances for every pair and allows one more intermediate city in each phase. Once all intermediates are allowed, count each row’s distances within 4: the counts are [2,3,3,2], so the larger tied index 3 wins.

Approaches

Floyd-Warshall

Optimal

Solution Details

Reveal Floyd-Warshall: intuition, complexity, and code

Hints

Hint 1
Count reachable cities by shortest-path distance, not by direct roads.
Hint 2
For each potential intermediate k, compare the current i-to-j distance with i-to-k plus k-to-j.
Hint 3
Keep k outermost, exclude self when counting, and replace the winner on ties while scanning upward.

Edge Cases

  • Disconnected cities keep infinite distances; an isolated city can win.
  • A shortest distance equal to the threshold counts.
  • If every count ties, return n - 1, even when every count is zero.

Common Mistakes and Interview Tips

  • Putting the intermediate loop inside the source/destination loops breaks the phase invariant.
  • Counting roads rather than shortest routes misses cheaper indirect paths.
  • Initializing missing roads to zero makes unreachable cities appear nearby.
  • Replacing only on strictly smaller counts keeps the smallest tied index.

Key Takeaway

When answers depend on many pairs in a small graph, dynamic programming over allowed intermediates computes all shortest routes in one matrix.