-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFloyd Warshall Algorithm.cpp
More file actions
39 lines (34 loc) · 911 Bytes
/
Floyd Warshall Algorithm.cpp
File metadata and controls
39 lines (34 loc) · 911 Bytes
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
#include<bits/stdc++.h>
#define INF 10000000
using namespace std;
void floydWarshall(int);
int main(){
int t, v;
cin >> t;
while(t--){
cin >> v;
floydWarshall(v);
}
return 0;
}
void floydWarshall(int v){
int arr[v][v], dist[v][v];
for(int i = 0; i < v; i++)
for(int j = 0; j < v; j++){
cin >> arr[i][j];
dist[i][j] = arr[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] != INF && dist[k][j] != INF && 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] == INF)
cout << "INF" << " ";
else
cout << dist[i][j] << " ";
cout << endl;
}
}