In
for (k = 0; k < n; ++k)
for (i = 0; i < n; ++i)
for (j = 0; j < n; ++j)
if (d[i][k] + d[k][j] < d[i][j])
d[i][j] = d[i][k] + d[k][j]
The outermost loop k is referring to vertices that may be on the path between Vi and Vj. So when k=1, for example, you are considering all paths between vertices Vi and Vj that include vertex V1 as in
Vi .... V1 .... Vj
More importantly, from among those paths you are choosing the best with the relaxation
if (d[i][k] + d[k][j] < d[i][j])
d[i][j] = d[i][k] + d[k][j]
Again, each iteration is focussed on two vertices Vi and Vj and in chooses the best path between them.
In your other instance, the one that fails, you are not choosing the best among paths between two fixed vertices Vi and Vj, instead you are relaxing all over the place, never waiting long enough to find out which path between two set vertices is the best.
On Geekviewpoint, a site which I rely on a lot, they distinctively use x and v as vertices and t for the outermost loop, which makes it easy to remember that t is temporary and so not one of the endpoints. (I wish they had actually explained it, since it's not obvious to everyone.)
//dynamically find the shortest distance between each pair.
for (int t = 0; t < n; t++) {
for (int v = 0; v < n; v++) {
for (int u = 0; u < n; u++) {
if (dist[v][u] > (long) dist[v][t] + dist[t][u]) {
dist[v][u] = dist[v][t] + dist[t][u];
pred[v][u] = pred[t][u];
}
}
}
}