-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfloydWarshal.cpp
More file actions
90 lines (82 loc) · 1.85 KB
/
floydWarshal.cpp
File metadata and controls
90 lines (82 loc) · 1.85 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <bits/stdc++.h>
using namespace std;
int V = 7;
void floydWarshal(vector<vector<int>> graph)
{
vector<vector<int>> dist(V, vector<int>(V, 0));
for (int i = 0; i < V; i++)
{
for (int j = 0; j < V; j++)
{
dist[i][j] = graph[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] != INT_MAX && dist[k][j] != INT_MAX && (dist[i][j] > (dist[i][k] + dist[k][j])))
{
dist[i][j] = dist[i][k] + dist[k][j];
}
}
}
for (auto el : dist)
{
for (auto c : el)
{
if(c==INT_MAX){
cout << -1 << " ";
}
else{
cout << c << " ";
}
}
cout << endl;
}
cout<<"BREAK HERE"<<endl;
}
// for (auto el : dist)
// {
// for (auto c : el)
// {
// if(c==INT_MAX){
// cout << -1 << " ";
// }
// else{
// cout << c << " ";
// }
// }
// cout << endl;
// }
}
int main()
{
vector<vector<int>> graph = {
{
0,5,2,INT_MAX,INT_MAX,INT_MAX,INT_MAX
},
{
5,0,2,4,18,INT_MAX,INT_MAX
},
{
2,2,0,8,INT_MAX,19,INT_MAX
},
{
INT_MAX,4,8,0,INT_MAX,INT_MAX,9
},
{
INT_MAX,18,INT_MAX,INT_MAX,0,11,14
},
{
INT_MAX,INT_MAX,19,INT_MAX,11,0,15,
},
{
INT_MAX,INT_MAX,INT_MAX,9,14,15,0
}
};
floydWarshal(graph);
return 0;
}