|
| 1 | +// Floyd–Warshall Algorithm — All-Pairs Shortest Path |
| 2 | +// Problem |
| 3 | +// Compute shortest distances between every pair of vertices in a weighted graph. |
| 4 | +// |
| 5 | +// Approach |
| 6 | +// 1. Initialize dist[i][j]: |
| 7 | +// - 0 if i==j |
| 8 | +// - weight(u,v) if edge exists |
| 9 | +// - INF otherwise |
| 10 | +// 2. For k in [0..V-1] (intermediate vertex): |
| 11 | +// for i in [0..V-1] (source): |
| 12 | +// for j in [0..V-1] (destination): |
| 13 | +// dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]) |
| 14 | +// 3. After all iterations, dist[i][j] is the shortest distance. |
| 15 | +// |
| 16 | +// Complexity |
| 17 | +// Time : O(V^3) |
| 18 | +// Space : O(V^2) |
| 19 | + |
| 20 | +#include <iostream> |
| 21 | +#include <vector> |
| 22 | +#include <algorithm> |
| 23 | +using namespace std; |
| 24 | + |
| 25 | +int main() |
| 26 | +{ |
| 27 | + ios::sync_with_stdio(false); |
| 28 | + cin.tie(nullptr); |
| 29 | + |
| 30 | + int V, E; |
| 31 | + cin >> V >> E; |
| 32 | + const int INF = 1e9; |
| 33 | + |
| 34 | + vector<vector<int>> dist(V, vector<int>(V, INF)); |
| 35 | + for (int i = 0; i < V; i++) |
| 36 | + dist[i][i] = 0; |
| 37 | + |
| 38 | + for (int i = 0; i < E; i++) |
| 39 | + { |
| 40 | + int u, v, w; |
| 41 | + cin >> u >> v >> w; |
| 42 | + dist[u][v] = min(dist[u][v], w); |
| 43 | + dist[v][u] = min(dist[v][u], w); // remove if directed |
| 44 | + } |
| 45 | + |
| 46 | + cout << "Initial distance matrix:\n"; |
| 47 | + for (auto &row : dist) |
| 48 | + { |
| 49 | + for (auto x : row) |
| 50 | + cout << (x == INF ? -1 : x) << " "; |
| 51 | + cout << "\n"; |
| 52 | + } |
| 53 | + cout << "\n"; |
| 54 | + |
| 55 | + // Floyd–Warshall dry run |
| 56 | + for (int k = 0; k < V; k++) |
| 57 | + { |
| 58 | + cout << "Considering intermediate vertex k=" << k << ":\n"; |
| 59 | + for (int i = 0; i < V; i++) |
| 60 | + for (int j = 0; j < V; j++) |
| 61 | + if (dist[i][k] < INF && dist[k][j] < INF) |
| 62 | + dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]); |
| 63 | + |
| 64 | + // print matrix after this k |
| 65 | + for (auto &row : dist) |
| 66 | + { |
| 67 | + for (auto x : row) |
| 68 | + cout << (x == INF ? -1 : x) << " "; |
| 69 | + cout << "\n"; |
| 70 | + } |
| 71 | + cout << "\n"; |
| 72 | + } |
| 73 | + |
| 74 | + cout << "Final All-Pairs Shortest Distances:\n"; |
| 75 | + for (auto &row : dist) |
| 76 | + { |
| 77 | + for (auto x : row) |
| 78 | + cout << (x == INF ? -1 : x) << " "; |
| 79 | + cout << "\n"; |
| 80 | + } |
| 81 | + |
| 82 | + return 0; |
| 83 | +} |
| 84 | + |
| 85 | +/* |
| 86 | +Example Input: |
| 87 | +4 5 |
| 88 | +0 1 5 |
| 89 | +0 3 10 |
| 90 | +1 2 3 |
| 91 | +2 3 1 |
| 92 | +0 2 100 |
| 93 | +
|
| 94 | +Visualization: |
| 95 | +Graph: |
| 96 | + (5) (3) (1) |
| 97 | +0 ----- 1 -------- 2 -------- 3 |
| 98 | + \ ^ |
| 99 | + \____________ (10) ___________/ |
| 100 | +
|
| 101 | +Dry Run Steps: |
| 102 | +
|
| 103 | +Initial dist: |
| 104 | +0 5 100 10 |
| 105 | +5 0 3 -1 |
| 106 | +100 3 0 1 |
| 107 | +10 -1 1 0 |
| 108 | +
|
| 109 | +k = 0 (using vertex 0 as intermediate): |
| 110 | +- dist[2][3] = min(1, dist[2][0]+dist[0][3]) = min(1,100+10)=1 (no change) |
| 111 | +- dist[2][0] = min(100, dist[2][0]+dist[0][0]) = 100 (no change) |
| 112 | +... print matrix |
| 113 | +
|
| 114 | +k = 1 (vertex 1 intermediate): |
| 115 | +- dist[0][2] = min(100, dist[0][1]+dist[1][2]) = min(100,5+3)=8 |
| 116 | +- dist[3][0] = min(10, dist[3][1]+dist[1][0]) = min(10, INF+5)=10 (no change) |
| 117 | +... print matrix |
| 118 | +
|
| 119 | +k = 2 (vertex 2 intermediate): |
| 120 | +- dist[0][3] = min(10, dist[0][2]+dist[2][3]) = min(10,8+1)=9 |
| 121 | +- dist[1][3] = min(INF, 3+1)=4 |
| 122 | +... print matrix |
| 123 | +
|
| 124 | +k = 3 (vertex 3 intermediate): |
| 125 | +- no further updates |
| 126 | +
|
| 127 | +Final dist: |
| 128 | +0 5 8 9 |
| 129 | +5 0 3 4 |
| 130 | +8 3 0 1 |
| 131 | +9 4 1 0 |
| 132 | +*/ |
| 133 | + |
| 134 | +// prepare PR |
0 commit comments