#include<bits/stdc++.h>usingnamespace std;// Number of vertices in the graph#defineV4/* Define Infinite as a large enough
value.This value will be used for
vertices not connected to each other */#defineINF99999// A function to print the solution matrixvoidprintSolution(int dist[][V]);// Solves the all-pairs shortest path// problem using Floyd Warshall algorithmvoidfloydWarshall(int graph[][V]){/* dist[][] will be the output matrixthat will finally have the shortestdistances between every pair of vertices */int dist[V][V], i, j, k;/* Initialize the solution matrix sameas input graph matrix. Or we can saythe initial values of shortest distancesare based on shortest paths consideringno intermediate vertex. */for(i =0; i < V; i++)for(j =0; j < V; j++)dist[i][j]= graph[i][j];/* Add all vertices one by one tothe set of intermediate vertices.---> Before start of an iteration,we have shortest distances between allpairs of vertices such that theshortest distances consider only thevertices in set {0, 1, 2, .. k-1} asintermediate vertices.----> After the end of an iteration,vertex no. k is added to the set ofintermediate vertices and the set becomes {0, 1, 2, ..k} */for(k =0; k < V; k++){// Pick all vertices as source one by onefor(i =0; i < V; i++){// Pick all vertices as destination for the// above picked sourcefor(j =0; j < V; j++){// If vertex k is on the shortest path from// i to j, then update the value of// dist[i][j]if(dist[i][j]>(dist[i][k]+ dist[k][j])&&(dist[k][j]!= INF&& dist[i][k]!= INF))dist[i][j]= dist[i][k]+ dist[k][j];}}}// Print the shortest distance matrixprintSolution(dist);}