Shortest distance between every pair of nodes, in one triple loop. The idea: allow paths to route through intermediate node k, one k at a time, and keep the best.
For every pair (i, j), ask: is going i → k → j shorter than what I have? Formally dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]).
After processing all k, the matrix holds every shortest distance.
dist[i][i] < 0void floydWarshall(vector<vector<int>>& dist) {
int n = dist.size();
// dist[i][j] preloaded: edge weight, 0 on diagonal, INF if no edge
for (int k = 0; k < n; k++) // intermediate node
for (int i = 0; i < n; i++) // source
for (int j = 0; j < n; j++) { // destination
if (dist[i][k] == INF || dist[k][j] == INF) continue;
if (dist[i][k] + dist[k][j] < dist[i][j])
dist[i][j] = dist[i][k] + dist[k][j]; // relax via k
}
// negative cycle if any node reaches itself at a cost < 0
for (int i = 0; i < n; i++)
if (dist[i][i] < 0) { /* negative cycle */ }
}
Three nested loops over all nodes. Fine for small dense graphs.
The full distance matrix. No extra structures needed.
Every source-destination distance at once, unlike single-source Dijkstra.
dist[i][i] < 0.next[i][j] matrix to reconstruct the actual paths.