-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcheapest_flights_within_k_stops.go
101 lines (75 loc) · 1.61 KB
/
cheapest_flights_within_k_stops.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package main
import "math"
type Connection struct {
city int
cost int
}
type queue []Connection
func (q *queue) push(c Connection) {
q1 := []Connection{c}
*q = append(*q, q1...)
}
func (q *queue) pop() (bool, Connection) {
if q.isEmpty() {
return false, Connection{}
} else {
elem := (*q)[0]
*q = (*q)[1:]
return true, elem
}
}
func (q queue) isEmpty() bool {
return q.size() <= 0
}
func (q queue) size() int {
return len(q)
}
func findCheapestPrice(n int, flights [][]int, src int, dst int, k int) int {
minCost := math.MaxInt64
fQueue := new(queue)
fQueue.push(Connection{src, 0})
maxStops := k + 1
costOfFlights := make([][]int, n)
for i := range costOfFlights {
costOfFlights[i] = make([]int, n)
}
flightsMap := make(map[int][]int)
for _, v := range flights {
if _, ok := flightsMap[v[0]]; ok {
flightsMap[v[0]] = append(flightsMap[v[0]], v[1])
} else {
flightsMap[v[0]] = []int{v[1]}
}
costOfFlights[v[0]][v[1]] = v[2]
}
cityPathCost := make([]int, n) // path cost from source
for i := range cityPathCost {
cityPathCost[i] = math.MaxInt64
}
cityPathCost[src] = 0
for !fQueue.isEmpty() && maxStops >= 0 {
size := fQueue.size()
maxStops--
for size > 0 {
_, c := fQueue.pop()
size--
if c.city == dst {
if minCost > c.cost {
minCost = c.cost
}
continue
}
for _, v := range flightsMap[c.city] {
newCost := c.cost + costOfFlights[c.city][v]
if cityPathCost[v] > newCost {
fQueue.push(Connection{v, newCost})
cityPathCost[v] = newCost
}
}
}
}
if minCost == math.MaxInt64 {
minCost = -1
}
return minCost
}