Does Floyd–Warshall work on all graphs?
The way to answer this kind of question is to simulate the algorithm with pencil and paper to see what it actually does.

Initially, we have dist[i][i] = 0 for all i, dist[0][2] = dist[2][3] = dist[3][1] = 1 and every other entry is infinite.

When we consider k = 0, nothing changes, since dist[i][k] = ∞ for all&nbsp;i except i = 0.

When we consider k = 1, nothing changes, since dist[k][j] = ∞ for all&nbsp;j except j = 1.

When we consider k = 2, we have dist[0][2] + dist[2][3] = 2 &lt; dist[0,3] = ∞, so we set dist[0,3] = 2; nothing else changes.

When we consider k = 3, we have dist[0][3] + dist[3][1] = 3 &lt; dist[0,1] = ∞, so we set dist[0,1] = 3; nothing else changes.

So the algorithm has correctly computed the shortest path from&nbsp;0 to&nbsp;1.

The point is that we're not really using dynamic programming based on the length of the intermediate path but on the highest-numbered vertex along that path.  Suppose you want to compute the shortest path from a to&nbsp;b. That path must pass through some highest-numbered vertex&nbsp;&ndash; call it&nbsp;c. Because that's the highest-numbered vertex on the path, we know that all the vertices on the paths a-c and c-b have lower numbers. So, in particular, by the time we reach the k = c iteration of the outer loop, we've already calculated all shortest paths between all vertices that use only vertices 0...c-1 as intermediate vertices. And that's enough information to calculate the shortest paths a-c and c-b and, hence, the shortest path a-b.