Algo-Sim › Advanced Graphs
Find the City (Floyd-Warshall)
Medium · Advanced Graphs · step through it in Java, Python and C++ on your own input
Which city reaches the fewest others within a distance threshold? All-pairs shortest paths with Floyd-Warshall, then count per city.
Problem
There are n cities joined by weighted two-way roads. For each city, count how many other cities it can reach with a shortest path of at most distanceThreshold. Return the city with the smallest count; on a tie, return the one with the largest number.
Constraints
- 2 ≤ n ≤ 100
- 1 ≤ edges.length ≤ n · (n − 1) / 2
- 1 ≤ weight, distanceThreshold ≤ 10⁴
- n ≤ 100 makes O(n³) all-pairs shortest paths cheap
Edge cases to test
- Ties: the largest index wins
- A threshold of 0
- Disconnected cities
Follow-up questions
- Why Floyd–Warshall?
- n is small and every pair is needed: O(n³) with three short loops. Dijkstra from each node is O(n·E log n), better for sparse graphs.
- Why is k the outer loop?
- After round k, distances use only nodes 0..k as stops; putting k inside breaks that invariant.
Solution
Java
public int findTheCity(int n, int[][] edges, int distanceThreshold) {
int INF = 1_000_000;
int[][] dist = new int[n][n];
for (int[] row : dist) Arrays.fill(row, INF);
for (int i = 0; i < n; i++) dist[i][i] = 0;
for (int[] e : edges) {
dist[e[0]][e[1]] = e[2];
dist[e[1]][e[0]] = e[2];
}
for (int k = 0; k < n; k++) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (dist[i][k] + dist[k][j] < dist[i][j]) {
dist[i][j] = dist[i][k] + dist[k][j];
}
}
}
}
int best = -1, fewest = n + 1;
for (int i = 0; i < n; i++) {
int reach = 0;
for (int j = 0; j < n; j++) {
if (i != j && dist[i][j] <= distanceThreshold) reach++;
}
if (reach <= fewest) {
fewest = reach;
best = i;
}
}
return best;
}Python
def find_the_city(n, edges, distance_threshold):
INF = float("inf")
dist = [[INF] * n for _ in range(n)]
for i in range(n):
dist[i][i] = 0
for u, v, w in edges:
dist[u][v] = dist[v][u] = w
for k in range(n):
for i in range(n):
for j in range(n):
if dist[i][k] + dist[k][j] < dist[i][j]:
dist[i][j] = dist[i][k] + dist[k][j]
best, fewest = -1, n + 1
for i in range(n):
reach = sum(1 for j in range(n) if i != j and dist[i][j] <= distance_threshold)
if reach <= fewest:
fewest, best = reach, i
return bestC++
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
int findTheCity(int n, vector<vector<int>> &edges, int distanceThreshold) {
int INF = 1'000'000;
vector<vector<int>> dist = vector<vector<int>>(n, vector<int>(n));
for (vector<int> &row : dist)
fill(row.begin(), row.end(), INF);
for (int i = 0; i < n; i++)
dist[i][i] = 0;
for (vector<int> &e : edges) {
dist[e[0]][e[1]] = e[2];
dist[e[1]][e[0]] = e[2];
}
for (int k = 0; k < n; k++) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (dist[i][k] + dist[k][j] < dist[i][j]) {
dist[i][j] = dist[i][k] + dist[k][j];
}
}
}
}
int best = -1, fewest = n + 1;
for (int i = 0; i < n; i++) {
int reach = 0;
for (int j = 0; j < n; j++) {
if (i != j && dist[i][j] <= distanceThreshold)
reach++;
}
if (reach <= fewest) {
fewest = reach;
best = i;
}
}
return best;
}
};
More Advanced Graphs problems
- Network Delay Time (Medium)
- Evaluate Division (Medium)
- Course Schedule II (Medium)
- Alien Dictionary (Hard)
- Redundant Connection (Medium)
- Number of Connected Components (Union-Find) (Medium)
- Accounts Merge (Medium)
- Minimum Spanning Tree (Kruskal) (Medium)
- Minimum Spanning Tree (Prim) (Medium)
- Min Cost to Connect All Points (Medium)
- Cheapest Flights Within K Stops (Medium)
- Bellman-Ford (negative weights) (Medium)
- Critical Connections in a Network (Hard)
- Articulation Points (Cut Vertices) (Hard)
- Strongly Connected Components (Hard)
- Reconstruct Itinerary (Hard)
- Minimum Height Trees (Medium)