k + 1 rounds of Bellman-Ford
Time O(k × E) Space O(n)dist[src] = 0. Repeat k + 1 times: next = copy of dist; relax every flight from dist into next; dist = next.
import java.util.Arrays;
class Solution {
public int findCheapestPrice(int n, int[][] flights, int src, int dst, int k) {
int INF = Integer.MAX_VALUE;
int[] dist = new int[n];
Arrays.fill(dist, INF);
dist[src] = 0;
for (int round = 0; round <= k; round++) {
int[] next = dist.clone();
for (int[] f : flights) {
if (dist[f[0]] == INF) continue;
next[f[1]] = Math.min(next[f[1]], dist[f[0]] + f[2]);
}
dist = next;
}
return dist[dst] == INF ? -1 : dist[dst];
}
}Verdict: The copy enforces the flight limit.