The problem asks: given a weighted graph with cities, roads, and queries, answer each query: what is the shortest distance from city to city ?
This is exactly the all-pairs shortest path problem. Run Floyd-Warshall once, then answer each query in . If no path exists, output . The constraints allow , which makes feasible. This problem tests whether you recognize the all-pairs pattern.
Space complexity is for the data structures used.