forked from VivekDubey9/Competitive-Programming-Algos
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfloyd_warshall.cpp
More file actions
43 lines (34 loc) · 716 Bytes
/
floyd_warshall.cpp
File metadata and controls
43 lines (34 loc) · 716 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
40
41
42
43
#include<iostream>
#include<vector>
using namespace std;
#define INF 10000
vector<vector<int>> floyd_warshall(vector<vector<int>> graph){
vector<vector<int>> dist(graph);
int V = graph.size();
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] + dist[k][j] < dist[i][j]){
dist[i][j] = dist[i][k] + dist[k][j];
}
}
}
}
return dist;
}
int main(){
vector<vector<int>> graph = {
{0,INF,-2,INF},
{4,0,3,INF},
{INF,INF,0,2},
{INF,-1,INF,0}
};
auto result = floyd_warshall(graph);
for(int i=0;i<result.size();i++){
for(int j=0;j<result.size();j++){
cout<<result[i][j]<<" ";
}
cout<<endl;
}
return 0;
}