(S) The Floyd-Warshall algorithm runs in O(n³) time on graphs with n vertices and m edges, whether or not the input graph contains a negative cycle. Modify the algorithm so that it solves the all-pairs shortest path problem in O(mn) time for input graphs with a negative cycle and O(n³) time otherwise.
Modify the input graph G = (V,E) by adding a new source vertex s and a new zero-length edge from s to each vertex v ∈ V . The new graph G^{\prime} has a negative cycle reachable from s if and only if G has a negative cycle. Run the Bellman-Ford algorithm on G^{\prime} with source vertex s to check whether G contains a negative cycle. If not, run the Floyd-Warshall algorithm on G.