public: intfindCheapestPrice(int n, vector<vector<int>>& flights, int src, int dst, int k){ vector<vector<int>> f(k + 2, vector<int>(n, INF)); f[0][src] = 0; for (int t = 1; t <= k + 1; ++t) { for (auto&& flight: flights) { int j = flight[0], i = flight[1], cost = flight[2]; f[t][i] = min(f[t][i], f[t - 1][j] + cost); } } int ans = INF; for (int t = 1; t <= k + 1; ++t) { ans = min(ans, f[t][dst]); } return (ans == INF ? -1 : ans); } };
publicclassSolution { publicintFindCheapestPrice(int n, int[][] flights, int src, int dst, int k) { constint INF = 10000 * 101 + 1; int[,] f = newint[k + 2, n]; for (int i = 0; i < k + 2; ++i) { for (int j = 0; j < n; ++j) { f[i, j] = INF; } } f[0, src] = 0; for (int t = 1; t <= k + 1; ++t) { foreach (int[] flight in flights) { int j = flight[0], i = flight[1], cost = flight[2]; f[t, i] = Math.Min(f[t, i], f[t - 1, j] + cost); } } int ans = INF; for (int t = 1; t <= k + 1; ++t) { ans = Math.Min(ans, f[t, dst]); } return ans == INF ? -1 : ans; } }
[sol11-Python3]
1 2 3 4 5 6 7 8 9 10
classSolution: deffindCheapestPrice(self, n: int, flights: List[List[int]], src: int, dst: int, k: int) -> int: f = [[float("inf")] * n for _ inrange(k + 2)] f[0][src] = 0 for t inrange(1, k + 2): for j, i, cost in flights: f[t][i] = min(f[t][i], f[t - 1][j] + cost) ans = min(f[t][dst] for t inrange(1, k + 2)) return -1if ans == float("inf") else ans
[sol11-JavaScript]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
var findCheapestPrice = function(n, flights, src, dst, k) { constINF = 10000 * 101 + 1; const f = newArray(k + 2).fill(0).map(() =>newArray(n).fill(INF)); f[0][src] = 0; for (let t = 1; t <= k + 1; ++t) { for (const flight of flights) { const j = flight[0], i = flight[1], cost = flight[2]; f[t][i] = Math.min(f[t][i], f[t - 1][j] + cost); } } let ans = INF; for (let t = 1; t <= k + 1; ++t) { ans = Math.min(ans, f[t][dst]); } return ans == INF ? -1 : ans; };
funcfindCheapestPrice(n int, flights [][]int, src int, dst int, k int)int { const inf = 10000*101 + 1 f := make([][]int, k+2) for i := range f { f[i] = make([]int, n) for j := range f[i] { f[i][j] = inf } } f[0][src] = 0 for t := 1; t <= k+1; t++ { for _, flight := range flights { j, i, cost := flight[0], flight[1], flight[2] f[t][i] = min(f[t][i], f[t-1][j]+cost) } } ans := inf for t := 1; t <= k+1; t++ { ans = min(ans, f[t][dst]) } if ans == inf { ans = -1 } return ans }
funcmin(a, b int)int { if a < b { return a } return b }
[sol11-C]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
intfindCheapestPrice(int n, int** flights, int flightsSize, int* flightsColSize, int src, int dst, int k) { int f[k + 2][n]; memset(f, 0x3f, sizeof(f)); f[0][src] = 0; for (int t = 1; t <= k + 1; ++t) { for (int k = 0; k < flightsSize; k++) { int j = flights[k][0], i = flights[k][1], cost = flights[k][2]; f[t][i] = fmin(f[t][i], f[t - 1][j] + cost); } } int ans = 0x3f3f3f3f; for (int t = 1; t <= k + 1; ++t) { ans = fmin(ans, f[t][dst]); } return (ans == 0x3f3f3f3f ? -1 : ans); }
publicclassSolution { publicintFindCheapestPrice(int n, int[][] flights, int src, int dst, int k) { constint INF = 10000 * 101 + 1; int[] f = newint[n]; Array.Fill(f, INF); f[src] = 0; int ans = INF; for (int t = 1; t <= k + 1; ++t) { int[] g = newint[n]; Array.Fill(g, INF); foreach (int[] flight in flights) { int j = flight[0], i = flight[1], cost = flight[2]; g[i] = Math.Min(g[i], f[j] + cost); } f = g; ans = Math.Min(ans, f[dst]); } return ans == INF ? -1 : ans; } }
[sol12-Python3]
1 2 3 4 5 6 7 8 9 10 11 12 13
classSolution: deffindCheapestPrice(self, n: int, flights: List[List[int]], src: int, dst: int, k: int) -> int: f = [float("inf")] * n f[src] = 0 ans = float("inf") for t inrange(1, k + 2): g = [float("inf")] * n for j, i, cost in flights: g[i] = min(g[i], f[j] + cost) f = g ans = min(ans, f[dst]) return -1if ans == float("inf") else ans
[sol12-JavaScript]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
var findCheapestPrice = function(n, flights, src, dst, k) { constINF = 10000 * 101 + 1; let f = newArray(n).fill(INF); f[src] = 0; let ans = INF; for (let t = 1; t <= k + 1; ++t) { const g = newArray(n).fill(INF); for (const flight of flights) { const j = flight[0], i = flight[1], cost = flight[2]; g[i] = Math.min(g[i], f[j] + cost); } f = g; ans = Math.min(ans, f[dst]); } return ans == INF ? -1 : ans; };
funcfindCheapestPrice(n int, flights [][]int, src int, dst int, k int)int { const inf = 10000*101 + 1 f := make([]int, n) for i := range f { f[i] = inf } f[src] = 0 ans := inf for t := 1; t <= k+1; t++ { g := make([]int, n) for i := range g { g[i] = inf } for _, flight := range flights { j, i, cost := flight[0], flight[1], flight[2] g[i] = min(g[i], f[j]+cost) } f = g ans = min(ans, f[dst]) } if ans == inf { ans = -1 } return ans }
funcmin(a, b int)int { if a < b { return a } return b }
[sol12-C]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
intfindCheapestPrice(int n, int** flights, int flightsSize, int* flightsColSize, int src, int dst, int k) { int f[n]; memset(f, 0x3f, sizeof(f)); f[src] = 0; int ans = 0x3f3f3f3f; for (int t = 1; t <= k + 1; ++t) { int g[n]; memset(g, 0x3f, sizeof(g)); for (int k = 0; k < flightsSize; k++) { int j = flights[k][0], i = flights[k][1], cost = flights[k][2]; g[i] = fmin(g[i], f[j] + cost); } memcpy(f, g, sizeof(f)); ans = fmin(ans, f[dst]); } return (ans == 0x3f3f3f3f ? -1 : ans); }