import
java.util.*;
public
class
GFG{
static
class
pair
{
int
first, second;
public
pair(
int
first,
int
second)
{
this
.first = first;
this
.second = second;
}
}
static
int
findCheapestCost(
int
n,
int
[][] graph,
int
src,
int
dst,
int
k)
{
if
(dst > n)
return
-
1
;
k = k +
1
;
Vector<pair> []adj =
new
Vector[n];
for
(
int
i =
0
; i < adj.length; i++)
adj[i] =
new
Vector<pair>();
for
(
int
it[] : graph) {
adj[it[
0
]].add(
new
pair( it[
1
], it[
2
] ));
}
int
[]prices =
new
int
[n];
Arrays.fill(prices, -
1
);
Queue<pair > q =
new
LinkedList<>();
q.add(
new
pair( src,
0
));
prices[src] =
0
;
while
(!q.isEmpty()) {
if
(k ==
0
)
break
;
int
sz = q.size();
while
(sz-- >
0
) {
int
node = q.peek().first;
int
cost = q.peek().second;
q.remove();
for
(pair it : adj[node]) {
if
(prices[it.first] == -
1
|| cost + it.second
< prices[it.first]) {
prices[it.first] = cost + it.second;
q.add(
new
pair( it.first, cost + it.second ));
}
}
}
k--;
}
return
prices[dst];
}
public
static
void
main(String[] args)
{
int
n =
6
;
int
[][] graph
= { {
0
,
1
,
10
}, {
1
,
2
,
20
}, {
2
,
5
,
30
}, {
1
,
3
,
10
}, {
3
,
4
,
10
}, {
4
,
5
,
10
} };
int
src =
0
;
int
dst =
5
;
int
k =
2
;
System.out.print(findCheapestCost(n, graph, src, dst, k)
+
"\n"
);
}
}