-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path08- Floyd-Warshall Algorithm.cpp
More file actions
54 lines (47 loc) · 1.58 KB
/
Copy path08- Floyd-Warshall Algorithm.cpp
File metadata and controls
54 lines (47 loc) · 1.58 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
// In The Name of Allah
// Floyd-Warshall (All-Pairs Shortest Paths)
//
// Problem: Compute the shortest distance between every pair of vertices.
// Works with negative edges too (but not negative cycles).
// Approach: dp[k][i][j] = min(dp[k-1][i][j], dp[k-1][i][k] + dp[k-1][k][j])
// collapsed into an in-place 2D matrix. After processing all k,
// dp[i][j] is the shortest path length from i to j.
// Time: O(V^3)
// Space: O(V^2)
//
// Right choice for dense graphs or when you need every pair's distance.
// For sparse graphs, running Dijkstra V times is asymptotically better.
#include <iostream>
#include <vector>
using namespace std;
const long long INF = (long long)1e18;
void floyd_warshall(vector<vector<long long>>& d) {
int n = (int)d.size();
for (int k = 0; k < n; k++) {
for (int i = 0; i < n; i++) {
if (d[i][k] >= INF) continue;
for (int j = 0; j < n; j++) {
if (d[k][j] >= INF) continue;
if (d[i][k] + d[k][j] < d[i][j]) d[i][j] = d[i][k] + d[k][j];
}
}
}
}
int main() {
int n = 4;
vector<vector<long long>> d(n, vector<long long>(n, INF));
for (int i = 0; i < n; i++) d[i][i] = 0;
d[0][1] = 5; d[0][3] = 10;
d[1][2] = 3;
d[2][3] = 1;
floyd_warshall(d);
cout << "All-pairs shortest distances:" << endl;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (d[i][j] >= INF) cout << " INF";
else cout << " " << d[i][j];
}
cout << endl;
}
return 0;
}