-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathfloyd_warshal.cpp
64 lines (59 loc) · 1.21 KB
/
floyd_warshal.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// this algorithm is used to find the shortest distance between all pair of vertices
#include<iostream>
#include<vector>
using namespace std;
const int INF = 1e9+1;
const int N = 100;
int dist[N][N];
void floyd_warshal(int n){
for(int k=1; k<=n; k++){
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++){
dist[i][j] = min(dist[i][j], dist[i][k]+dist[k][j]);
}
}
}
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++){
if(dist[i][j] == INF){
cout<<"I"<<" ";
}
else{
cout<<dist[i][j]<<" ";}
}
cout<<endl;
}
}
int main()
{
for(int i=0; i<N; i++){
for(int j=0; j<N; j++){
if(i==j){
dist[i][j] = 0;
}
else{
dist[i][j] = INF;
}
}
}
// graph input;
int n,m;
cin>>n>>m;
for(int i=0; i<m; i++){
int x,y, wt;
cin>>x>>y>>wt;
dist[x][y] = wt;
}
floyd_warshal(n);
return 0;
}
// 6 9
// 1 2 1
// 1 3 5
// 2 3 2
// 3 5 2
// 2 5 1
// 2 4 2
// 4 5 3
// 4 6 1
// 5 6 2