1
+ import java .util .ArrayList ;
2
+ import java .util .Queue ;
3
+ import java .util .LinkedList ;
4
+
5
+ public class GraphsP5_CheapestFlights {
6
+ // Create Edge Class ---
7
+ static class Edge {
8
+ int src , dest , wt ;
9
+
10
+ public Edge (int src , int dest , int wt ) {
11
+ this .src = src ;
12
+ this .dest = dest ;
13
+ this .wt = wt ;
14
+ }
15
+ }
16
+
17
+ // Create Graph ---
18
+ public static void createGraph (ArrayList <Edge > graph [], int flights [][]) {
19
+ for (int i = 0 ; i < graph .length ; i ++) { // initialize ArrayList of all vertices
20
+ graph [i ] = new ArrayList <>();
21
+ }
22
+ for (int i = 0 ; i < flights .length ; i ++) {
23
+ graph [flights [i ][0 ]].add (new Edge (flights [i ][0 ], flights [i ][1 ], flights [i ][2 ]));
24
+ }
25
+ }
26
+
27
+ // Create Info Class ---
28
+ static class Info { // Already sorted based on stops
29
+ int vertex ; // vertex
30
+ int cost ; // cost/distance
31
+ int stops ; // number of stops
32
+
33
+ public Info (int vertex , int cost , int stops ) {
34
+ this .vertex = vertex ;
35
+ this .cost = cost ;
36
+ this .stops = stops ;
37
+ }
38
+ }
39
+
40
+ // Cheapest Flights With in K Stops --------------------------------
41
+ public static int cheapestFlights (int n , int flights [][], int src , int dst , int k ) {
42
+ // Create graph
43
+ @ SuppressWarnings ("unchecked" )
44
+ ArrayList <Edge > graph [] = new ArrayList [n ];
45
+ createGraph (graph , flights );
46
+ // Create cost array
47
+ int dist [] = new int [n ];
48
+ for (int i = 0 ; i < dist .length ; i ++) {
49
+ if (i != src ) {
50
+ dist [i ] = Integer .MAX_VALUE ;
51
+ }
52
+ }
53
+ // Create Queue for store Info (vertex,cost,stops)
54
+ Queue <Info > q = new LinkedList <>();
55
+ q .add (new Info (src , 0 , 0 )); // add src to src
56
+
57
+ while (!q .isEmpty ()) {
58
+ Info curr = q .remove ();
59
+ if (curr .stops > k ) {
60
+ break ;
61
+ }
62
+ for (int i = 0 ; i < graph [curr .vertex ].size (); i ++) {
63
+ Edge e = graph [curr .vertex ].get (i );
64
+ // int u = e.src;
65
+ int v = e .dest ;
66
+ int wt = e .wt ;
67
+ if (curr .cost + wt < dist [v ] && curr .stops <= k ) { // dist[u] updates path cost, but curr.cost is current path cost
68
+ dist [v ] = curr .cost + wt ;
69
+ q .add (new Info (v , dist [v ], curr .stops + 1 ));
70
+ }
71
+ }
72
+ }
73
+ // Dist[dst]
74
+ if (dist [dst ] == Integer .MAX_VALUE ) {
75
+ return -1 ;
76
+ } else {
77
+ return dist [dst ];
78
+ }
79
+ }
80
+
81
+ public static void main (String [] args ) {
82
+ // Cheapest Flights With in K Stops --------------------------------
83
+ // There are n cities connected by some numbers of flights. You are given an array flights where
84
+ // flight[i] = [from,to,price] indicates that there is a flight.
85
+ // You are also given three integers src,dst and k, return the cheapest price from src to dst with
86
+ // at most k stops. If there is no such route, return -1.
87
+ int n = 4 ;
88
+ int flights [][] = { { 0 , 1 , 100 }, { 1 , 2 , 100 }, { 2 , 0 , 100 }, { 1 , 3 , 600 }, { 2 , 3 , 200 } };
89
+ int src = 0 , dst = 3 , k = 1 ;
90
+ System .out .println ("Cheapest Flights Cost : " + cheapestFlights (n , flights , src , dst , k ));
91
+
92
+ }
93
+ }
0 commit comments