#396 · Graphs · Advanced · 140 XP · about 25 min
n towns, numbered 0 to n - 1, are joined by two-way roads. Each road [a, b, km] in roads is km kilometres long. Seen from one town, another town is within reach if the shortest drive to it, over one road or several, is at most limit kilometres.
Write quietest_town(n, roads, limit) that returns the town with the fewest other towns within reach. If several towns tie, return the one with the smallest number.
quietest_town(4, [[0, 1, 2], [1, 2, 3], [0, 2, 4], [2, 3, 3]], 4) # returns 3 quietest_town(3, [[0, 1, 5], [1, 2, 1], [0, 2, 2]], 3) # returns 0 quietest_town(3, [[0, 1, 1]], 5) # returns 2
In the first example only town 2 is within 4 km of town 3, while every other town has at least two towns within reach. In the second, the drive from town 0 to town 1 is 3 km through town 2, shorter than the direct 5 km road, so every town reaches both others and the tie goes to town 0. In the last, no road leads to town 2.
1 <= n <= 100, with up to 2,000 roads. Two towns can be joined by several roads.1 <= km <= 10,000 and 0 <= limit <= 1,000,000.First solves only, ranked by stars and then time.