-
Notifications
You must be signed in to change notification settings - Fork 0
/
fw.cpp
49 lines (37 loc) · 939 Bytes
/
fw.cpp
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
#include<bits/stdc++.h>
using namespace std;
#define inf 10000
void print(int g[][4]){
for(int i=0; i<4; i++){
for(int j=0; j<4; j++){
if(g[i][j]==inf){
cout<<"Inf"<<' ';
}
else{
cout<<g[i][j]<<' ';
}
}
cout<<endl;
}
}
void floyd_warshall(int g[][4]){
for(int k=0; k<4; k++){ //k as intermediate vertex
for(int i=0; i<4; i++){ //row
for(int j=0; j<4; j++){ //column
g[i][j]=min(g[i][j], g[i][k]+g[k][j]);
}
}
}
}
int main(){
int g[4][4]={{0,3,inf,7},
{8,0,2,inf},
{5,inf,0,1},
{2,inf,inf,0}};
cout<<"The matrix before applying Floyd-Warshall Algo: "<<endl;
print(g);
cout<<"The matrix after applying Floyd-Warshall Algo: "<<endl;
floyd_warshall(g);
print(g);
return 0;
}