1334. Find the City With the Smallest Number of Neighbors at a Threshold Distance
https://leetcode.com/problems/find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance/
Last updated
Was this helpful?
https://leetcode.com/problems/find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance/
Last updated
Was this helpful?
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.
Example 1:
Example 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.
有权无向图上每个结点最远能走阈值所设定的距离,找能访问到结点数最少的结点。找图上所有结点对的距离,然后再做筛选并找出拥有reachable最少的结点。找所有结点对距离用Floyd算法,基于DP思想:dp[i][j]存[i,j]最短距离,遍历所有节点k并把该节点k看作中转节点,看dp[i][k] + dp[j][k]是否比当前dp[i][j]更小,是就更新。