codekofi
← All problems

Problem 72

Medium

1 · Worked examples

0 / 3

Type an input and write what you think is the output . Each problem is converted to Web Assembly, so any possible input will show the corresponding output. Only correct predictions count.

solution()
returns

2 · Which problem is it?

Locked until you have predicted 3 outputs correctly.

The accepted solution

1int solution(int n, const vector<vector<int>>& flights,
2 int from, int to, int k) {
3
4 vector<int> best(n, INT_MAX);
5 best[from] = 0;
6
7 for (int round = 0; round <= k; round++) {
8
9 vector<int> next = best;
10
11 for (const auto& f : flights) {
12
13 if (best[f[0]] == INT_MAX) {
14 continue;
15 }
16
17 int cost = best[f[0]] + f[2];
18 next[f[1]] = min(next[f[1]], cost);
19 }
20
21 best = next;
22 }
23
24 if (best[to] == INT_MAX) {
25 return -1;
26 }
27
28 return best[to];
29}

Names have been stripped. The signature is the only clue you get for free. Compiled as C++20 with the standard headers and using namespace std; already in scope.