-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfloydWarshall.cpp
57 lines (44 loc) · 892 Bytes
/
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
#include <iostream>
#include <vector>
#define INF 987654321
using namespace std;
int n;
vector<vector<int>> board;
vector<vector<int>> floydWarshall() {
vector<vector<int>> ret;
ret = board;
for(int k = 0; k < n; k++) {
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
if(ret[i][k] + ret[k][j] < ret[i][j]) {
ret[i][j] = ret[i][k] + ret[k][j];
}
}
}
}
return ret;
}
int main() {
int m;
cin >> n >> m;
board.assign(n, vector<int>(n, INF));
int u, v, cost;
for(int i = 0; i < m; i++) {
cin >> u >> v >> cost;
if(board[u - 1][v - 1] > cost) {
board[u - 1][v - 1] = cost;
}
}
for(int i = 0; i < n; i++) {
board[i][i] = 0;
}
vector<vector<int>> temp = floydWarshall();
for(const auto& el : temp) {
for(const auto& e : el) {
if(e == INF) cout << 0 << ' ';
else cout << e << ' ';
}
cout << '\n';
}
return 0;
}