@@ -25,7 +25,7 @@ class Solution:
25
25
dist[nei][nextStops+ 1 ]= nextCst
26
26
heapq.heappush(minHeap, (nextCst, nei, nextStops))
27
27
28
- return - 1
28
+ return - 1
29
29
```
30
30
31
31
``` java
@@ -46,7 +46,7 @@ public class Solution {
46
46
Comparator . comparingInt(a- > a[0 ])
47
47
);
48
48
minHeap. offer(new int []{0 , src,- 1 });
49
-
49
+
50
50
while (! minHeap. isEmpty()) {
51
51
int [] top= minHeap. poll();
52
52
int cst= top[0 ], node= top[1 ], stops= top[2 ];
@@ -186,6 +186,78 @@ public class Solution {
186
186
}
187
187
```
188
188
189
+ ``` go
190
+ func findCheapestPrice (n int ,flights [][]int ,src int ,dst int ,k int )int {
191
+ INF := 1000000000
192
+ adj := make ([][]struct { to, costint }, n)
193
+ dist := make ([][]int , n)
194
+ for i := range dist {
195
+ dist[i] =make ([]int , k+5 )
196
+ for j := range dist[i] {
197
+ dist[i][j] = INF
198
+ }
199
+ }
200
+ for _ ,flight := range flights {
201
+ from ,to ,cost := flight[0 ], flight[1 ], flight[2 ]
202
+ adj[from] =append (adj[from],struct { to, costint }{to, cost})
203
+ }
204
+ dist[src][0 ] =0
205
+ minHeap := priorityqueue.NewWith (func (a, binterface {})int {
206
+ return utils.IntComparator (a.([3 ]int )[0 ], b.([3 ]int )[0 ])
207
+ })
208
+ minHeap.Enqueue ([3 ]int {0 , src, -1 })
209
+ for !minHeap.Empty () {
210
+ value ,_ := minHeap.Dequeue ()
211
+ cst ,node ,stops := value.([3 ]int )[0 ], value.([3 ]int )[1 ], value.([3 ]int )[2 ]
212
+ if node == dst {
213
+ return cst
214
+ }
215
+ if stops == k || dist[node][stops+1 ] < cst {
216
+ continue
217
+ }
218
+ for _ ,nei := range adj[node] {
219
+ nextCst := cst + nei.cost
220
+ nextStops := stops +1
221
+ if dist[nei.to ][nextStops+1 ] > nextCst {
222
+ dist[nei.to ][nextStops+1 ] = nextCst
223
+ minHeap.Enqueue ([3 ]int {nextCst, nei.to , nextStops})
224
+ }
225
+ }
226
+ }
227
+ return -1
228
+ }
229
+ ```
230
+
231
+ ``` kotlin
232
+ class Solution {
233
+ fun findCheapestPrice (n : Int ,flights : Array <IntArray >,src : Int ,dst : Int ,k : Int ):Int {
234
+ val INF = 1_000_000_000
235
+ val adj= Array (n) { mutableListOf<Pair <Int ,Int >>() }
236
+ val dist= Array (n) {IntArray (k+ 5 ) {INF } }
237
+ for (flightin flights) {
238
+ adj[flight[0 ]].add(Pair (flight[1 ], flight[2 ]))
239
+ }
240
+ dist[src][0 ]= 0
241
+ val minHeap= PriorityQueue (compareBy<Triple <Int ,Int ,Int >> { it.first })
242
+ minHeap.add(Triple (0 , src,- 1 ))
243
+ while (minHeap.isNotEmpty()) {
244
+ val (cst, node, stops)= minHeap.poll()
245
+ if (node== dst)return cst
246
+ if (stops== k|| dist[node][stops+ 1 ]< cst)continue
247
+ for ((nei, w)in adj[node]) {
248
+ val nextCst= cst+ w
249
+ val nextStops= stops+ 1
250
+ if (dist[nei][nextStops+ 1 ]> nextCst) {
251
+ dist[nei][nextStops+ 1 ]= nextCst
252
+ minHeap.add(Triple (nextCst, nei, nextStops))
253
+ }
254
+ }
255
+ }
256
+ return - 1
257
+ }
258
+ }
259
+ ```
260
+
189
261
:: tabs-end
190
262
191
263
###Time & Space Complexity
@@ -346,6 +418,59 @@ public class Solution {
346
418
}
347
419
```
348
420
421
+ ``` go
422
+ func findCheapestPrice (n int ,flights [][]int ,src int ,dst int ,k int )int {
423
+ prices := make ([]int , n)
424
+ for i := range prices {
425
+ prices[i] = math.MaxInt32
426
+ }
427
+ prices[src] =0
428
+
429
+ for i := 0 ; i <= k; i++ {
430
+ tmpPrices := make ([]int , n)
431
+ copy (tmpPrices, prices)
432
+
433
+ for _ ,flight := range flights {
434
+ s ,d ,p := flight[0 ], flight[1 ], flight[2 ]
435
+ if prices[s] == math.MaxInt32 {
436
+ continue
437
+ }
438
+ if prices[s] + p < tmpPrices[d] {
439
+ tmpPrices[d] = prices[s] + p
440
+ }
441
+ }
442
+ prices = tmpPrices
443
+ }
444
+
445
+ if prices[dst] == math.MaxInt32 {
446
+ return -1
447
+ }
448
+ return prices[dst]
449
+ }
450
+ ```
451
+
452
+ ``` kotlin
453
+ class Solution {
454
+ fun findCheapestPrice (n : Int ,flights : Array <IntArray >,src : Int ,dst : Int ,k : Int ):Int {
455
+ val prices= IntArray (n) {Int .MAX_VALUE }
456
+ prices[src]= 0
457
+
458
+ repeat(k+ 1 ) {
459
+ val tmpPrices= prices.copyOf()
460
+ for ((s, d, p)in flights) {
461
+ if (prices[s]== Int .MAX_VALUE )continue
462
+ if (prices[s]+ p< tmpPrices[d]) {
463
+ tmpPrices[d]= prices[s]+ p
464
+ }
465
+ }
466
+ tmpPrices.copyInto(prices)
467
+ }
468
+
469
+ return if (prices[dst]== Int .MAX_VALUE )- 1 else prices[dst]
470
+ }
471
+ }
472
+ ```
473
+
349
474
:: tabs-end
350
475
351
476
###Time & Space Complexity
@@ -527,6 +652,81 @@ public class Solution {
527
652
}
528
653
```
529
654
655
+ ``` go
656
+ func findCheapestPrice (n int ,flights [][]int ,src int ,dst int ,k int )int {
657
+ prices := make ([]int , n)
658
+ for i := range prices {
659
+ prices[i] = math.MaxInt32
660
+ }
661
+ prices[src] =0
662
+
663
+ adj := make ([][][2 ]int , n)
664
+ for _ ,flight := range flights {
665
+ from ,to ,cost := flight[0 ], flight[1 ], flight[2 ]
666
+ adj[from] =append (adj[from], [2 ]int {to, cost})
667
+ }
668
+
669
+ q := [][3 ]int {{0 , src,0 }}
670
+
671
+ for len (q) >0 {
672
+ curr := q[0 ]
673
+ q = q[1 :]
674
+ cst ,node ,stops := curr[0 ], curr[1 ], curr[2 ]
675
+
676
+ if stops > k {
677
+ continue
678
+ }
679
+
680
+ for _ ,neighbor := range adj[node] {
681
+ nei ,w := neighbor[0 ], neighbor[1 ]
682
+ nextCost := cst + w
683
+ if nextCost < prices[nei] {
684
+ prices[nei] = nextCost
685
+ q =append (q, [3 ]int {nextCost, nei, stops +1 })
686
+ }
687
+ }
688
+ }
689
+
690
+ if prices[dst] == math.MaxInt32 {
691
+ return -1
692
+ }
693
+ return prices[dst]
694
+ }
695
+ ```
696
+
697
+ ``` kotlin
698
+ class Solution {
699
+ fun findCheapestPrice (n : Int ,flights : Array <IntArray >,src : Int ,dst : Int ,k : Int ):Int {
700
+ val prices= IntArray (n) {Int .MAX_VALUE }
701
+ prices[src]= 0
702
+
703
+ val adj= Array (n) { mutableListOf<Pair <Int ,Int >>() }
704
+ for (flightin flights) {
705
+ val (from, to, cost)= flight
706
+ adj[from].add(Pair (to, cost))
707
+ }
708
+
709
+ val q: Queue <Triple <Int ,Int ,Int >>= LinkedList ()
710
+ q.offer(Triple (0 , src,0 ))
711
+
712
+ while (q.isNotEmpty()) {
713
+ val (cst, node, stops)= q.poll()
714
+ if (stops> k)continue
715
+
716
+ for ((nei, w)in adj[node]) {
717
+ val nextCost= cst+ w
718
+ if (nextCost< prices[nei]) {
719
+ prices[nei]= nextCost
720
+ q.offer(Triple (nextCost, nei, stops+ 1 ))
721
+ }
722
+ }
723
+ }
724
+
725
+ return if (prices[dst]== Int .MAX_VALUE )- 1 else prices[dst]
726
+ }
727
+ }
728
+ ```
729
+
530
730
:: tabs-end
531
731
532
732
###Time & Space Complexity