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

Edge cases to test

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 best

C++

#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