-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathGFG_FloydWarshall.cpp
64 lines (55 loc) · 1.37 KB
/
GFG_FloydWarshall.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
/*
Floyd Warshall
https://practice.geeksforgeeks.org/problems/implementing-floyd-warshall2042/1
*/
//Initial template for C++
#include<bits/stdc++.h>
using namespace std;
// } Driver Code Ends
//User function template for C++
class Solution {
public:
void shortest_distance(vector<vector<int>>&matrix){
int v = matrix.size();
for(int k = 0; k<v; k++) // intermediate vertices
{
for(int i=0; i<v; i++) // source
{
for(int j=0; j<v; j++) // destination
{
if(i==k || j==k || matrix[i][k]==-1 || matrix[k][j]==-1 || i==j)
continue;
// matrix[i][j] > matrix[k][j]+matrix[i][k]
if(matrix[i][j]==-1)
matrix[i][j] = matrix[i][k]+matrix[k][j];
else
matrix[i][j] = min(matrix[i][j], matrix[i][k]+matrix[k][j]);
}
}
}
}// end;
};
// { Driver Code Starts.
int main(){
int tc;
cin >> tc;
while(tc--){
int n;
cin >> n;
vector<vector<int>>matrix(n, vector<int>(n, -1));
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
cin >> matrix[i][j];
}
}
Solution obj;
obj.shortest_distance(matrix);
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
cout << matrix[i][j] << " ";
}
cout << "\n";
}
}
return 0;
} // } Driver Code Ends