Problem 515418 · medium · Level 05 Advanced Algorithms & Graphs

Quietest Town Within a Radius

graphs · Floyd-Warshall · all-pairs shortest paths

A regional planner wants to open a quiet retreat in one of n towns 0 .. n-1. Roads are undirected: roads[i] = [u, v, km] (km >= 0, parallel roads possible). A town's crowd is the number of other towns whose shortest road distance to it is at most radius.

Return the town with the smallest crowd. If several towns tie, return the smallest label.

Examples

Input:  n = 4, roads = [[0,1,3],[1,2,1],[2,3,4],[0,3,10]], radius = 4
Output: 3
Explanation: crowds are 0 -> {1,2}, 1 -> {0,2}, 2 -> {0,1,3}, 3 -> {2}. Town 3 is quietest.

Input:  n = 3, roads = [], radius = 5
Output: 0
Explanation: every crowd is 0; the smallest label wins.

Constraints

  • 1 <= n <= 100, 0 <= len(roads) <= n * (n - 1), 0 <= km, radius <= 10**4
  • Target O(n**3) time.

Goals

  • Compute all-pairs shortest distances on a small undirected graph
  • Count neighbours within a distance limit for each node
  • Break ties by the stated rule
Starting Python…