Graph Theory37 sections · 1633 units
Open in Course

LeetCode 1334 Find the City - Counting Reachable Cities

(Looping through distances)

After running Floyd-Warshall, count reachable cities for each node:

for i from 1 to n
    count := 0
    for j from 1 to n
        if dist[i][j] <= threshold then
            count := count + 1
            reachable[i] := count

Track the city with smallest count. If counts are equal, pick the largest index (or as specified by the problem).

This appears in LeetCode's "Find the City With the Smallest Number of Neighbors at a Threshold Distance."