-
Notifications
You must be signed in to change notification settings - Fork 0
/
floyd_warshall.cpp
64 lines (57 loc) · 1.22 KB
/
floyd_warshall.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
// time complexity - O(N^3)
// space complexity - O(N^2)
#include <bits/stdc++.h>
using namespace std;
#define inf INT_MAX
int nodes, edges, dis[100][100], u, v, w;
void floyd_warshall() {
for (int k = 1; k <= nodes; k++) {
dis[k][k] = 0;
for (int i = 1; i <= nodes; i++) {
for (int j = 1; j <= nodes; j++) {
if (dis[i][k] != inf && dis[k][j] != inf && dis[i][k] + dis[k][j] < dis[i][j]) {
dis[i][j] = dis[i][k] + dis[k][j];
}
}
}
}
}
void print_distance_matrix() {
for (int i = 1; i <= nodes; i++) {
for (int j = 1; j <= nodes; j++) {
if (dis[i][j] == inf) printf("inf ");
else printf("%d ", dis[i][j]);
}
printf("\n");
}
}
int main() {
//freopen("in", "r", stdin);
scanf("%d %d", &nodes, &edges);
for (int i = 1; i <= nodes; i++) {
for (int j = 1; j <= nodes; j++) {
dis[i][j] = inf;
}
}
while (edges--) {
scanf("%d %d %d", &u, &v, &w);
dis[u][v] = w;
}
floyd_warshall();
print_distance_matrix();
}
/*
Input:
4 6
4 1 20
1 2 3
2 1 2
2 3 2
3 2 5
4 3 8
Output:
0 3 5 inf
2 0 2 inf
7 5 0 inf
15 13 8 0
*/