-
Notifications
You must be signed in to change notification settings - Fork 0
/
GraphFloydWarshall.cpp
53 lines (47 loc) · 1.35 KB
/
GraphFloydWarshall.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
#include <iostream>
#include <cstring>
#include <queue>
#include <limits.h>
using namespace std;
const int MAXN = 1e3;
class GraphFloydWarshall {
public:
int distances[MAXN][MAXN]{};
int numberOfVertex{};
int numberOfEdges{};
GraphFloydWarshall() {
cin >> numberOfVertex >> numberOfEdges;
for (int i = 0; i < numberOfVertex; i++) {
for (int j = 0; j < numberOfVertex; j++) {
distances[i][j] = 1e8;
}
distances[i][i] = 0;
}
for (int i = 0; i < numberOfEdges; i++) {
int v1, v2, w;
cin >> v1 >> v2 >> w;
distances[v1][v2] = w;
}
}
void floydWarshall() {
for (int x = 0; x < numberOfVertex; x++) {
for (int i = 0; i < numberOfVertex; i++) {
for (int j = 0; j < numberOfVertex; j++ ){
distances[i][j] = min(distances[i][j], distances[i][x] + distances[x][j]);
}
}
}
for (int i = 0; i < numberOfVertex; ++i) {
cout << "Vertice " << i << ": ";
for (int j = 0; j < numberOfVertex; ++j) {
cout << distances[i][j] << " ";
}
cout << endl;
}
}
};
int main() {
GraphFloydWarshall graphFloydWarshall;
graphFloydWarshall.floydWarshall();
return 0;
}