-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFloydWarshall.cpp
More file actions
52 lines (40 loc) · 1.14 KB
/
Copy pathFloydWarshall.cpp
File metadata and controls
52 lines (40 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <bits/stdc++.h>
using namespace std;
#define V 4
int dist[100][100];
// Finding shortest paths in a weighted directed graph with positive or negative edge weights (but with no negative cycles).
void FloydWarshall(int graph[][V]){
int dist[V][V];
for(int i=0;i<V;i++){
for(int j=0;j<V;j++){
dist[i][j]=graph[i][j];
}
}
for(int k=0;k<V;k++){
for(int i=0;i<V;i++){
for(int j=0;j<V;j++){
if(dist[i][k]!=INT_MAX && dist[k][j]!=INT_MAX && dist[i][k]+dist[k][j] < dist[i][j]){
dist[i][j]=dist[i][k]+dist[k][j];
}
}
}
}
for(int i=0;i<V;i++){
for(int j=0;j<V;j++){
if(dist[i][j]==INT_MAX){cout<<"INF ";}
else
cout<<dist[i][j]<<" ";
}
cout<<"\n";
}
return;
}
int main(){
int graph[V][V] = { {0, 5, INT_MAX, 10},
{INT_MAX, 0, 3, INT_MAX},
{INT_MAX, INT_MAX, 0, 1},
{INT_MAX, INT_MAX, INT_MAX, 0}
};
FloydWarshall(graph);
return 0;
}