Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // C++ Program for Floyd Warshall Algorithm
- #include <bits/stdc++.h>
- using namespace std;
- #define V 7
- #define E 12
- #define INF 99999
- int graph[V][V];
- void initial(){
- for (int i = 0; i < V; i++)
- {
- for (int j = 0; j < V; j++)
- {
- graph[i][j]=INF;
- }
- }
- }
- void printSolution(int dist[][V])
- {
- for (int i = 0; i < V; i++)
- {
- for (int j = 0; j < V; j++)
- {
- if (dist[i][j] == INF)
- cout<<"INF"<<" ";
- else
- cout<<dist[i][j]<<" ";
- }
- cout<<endl;
- }
- }
- void floydWarshall (int graph[][V])
- {
- int dist[V][V], i, j, k;
- for (i = 0; i < V; i++)
- for (j = 0; j < V; j++)
- dist[i][j] = graph[i][j];
- for (k = 0; k < V; k++)
- {
- for (i = 0; i < V; i++)
- {
- for (j = 0; j < V; j++)
- {
- if (dist[i][k] + dist[k][j] < dist[i][j])
- dist[i][j] = dist[i][k] + dist[k][j];
- //dist[i][j] = min(dist[i][j],dist[i][k] + dist[k][j]);
- }
- }
- }
- printSolution(dist);
- }
- int main()
- {
- initial();
- // int graph[V][V];
- // { {0, 5, INF, 10},
- // {INF, 0, 3, INF},
- // {INF, INF, 0, 1},
- // {INF, INF, INF, 0}
- // };
- int u,v,w;
- for(int i=0;i<E;i++){
- cin>>u>>v>>w;
- graph[u][v]=w;
- }
- floydWarshall(graph);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement