-
Notifications
You must be signed in to change notification settings - Fork 77
/
floyd_warshall.cpp
55 lines (51 loc) · 1.43 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
#define MAX 1000
int dis[MAX][MAX];
int nxt[MAX][MAX];
int edges, vertices;
/*
Time: O(n^3)
Space: O(n^2)
*/
void floyd_warshall() {
memset(dis, INF, sizeof dis);
int u, v, cost;
for(int i = 0; i < edges; ++i) scanf("%d %d %d", &u, &v, &cost), dis[u][v] = cost;
// next[i][j] = k; path from i to j, k is the next node
for(int i = 0; i < vertices; ++i) {
for(int j = 0; j < vertices; ++j) {
nxt[i][j] = j;
}
}
for(int k = 0; k < vertices; ++k) {
for(int i = 0; i < vertices; ++i) {
for(int j = 0; j < vertices; ++j) {
if(dis[i][k] + dis[k][j] < dis[i][j]) {
dis[i][j] = dis[i][k] + dis[k][j];
nxt[i][j] = nxt[i][k];
}
}
}
}
}
void findPath(int src, int dest, vector<int>& path) {
path.push_back(src);
int u = src;
while(u != dest) {
u = nxt[u][dest];
path.push_back(u);
}
}
// mini-max
/*
for(int k = 0; k < vertices; ++k)
for(int i = 0; i < vertices; ++i)
for(int j = 0; j < vertices; ++j)
dis[i][j] = min(dis[i][j], max(dis[i][k], dis[k][j]));
*/
// Transitive closure: if there is a direct/indirect path between i to j. input dis[u][v] = 1 if path else 0 (no need to assign cost)
/*
for(int k = 0; k < vertices; ++k)
for(int i = 0; i < vertices; ++i)
for(int j = 0; j < vertices; ++j)
dis[i][j] = dis[i][j] | (dis[i][k] & dis[k][j]);
*/