import
java.util.*;
class
Main {
public
static
int
findShortestPath(
int
n,
int
[][] edges,
int
src,
int
dst,
int
K)
{
List<List<
int
[]> > adjlist =
new
ArrayList<>();
for
(
int
i =
0
; i < n; i++) {
adjlist.add(
new
ArrayList<
int
[]>());
}
Queue<
int
[]> q =
new
LinkedList<>();
Map<Integer, Integer> mp =
new
HashMap<>();
int
ans = Integer.MIN_VALUE;
for
(
int
[] edge : edges) {
adjlist.get(edge[
0
]).add(
new
int
[] { edge[
1
], edge[
2
] });
}
q.add(
new
int
[] { src,
0
});
int
level =
0
;
while
(!q.isEmpty() && level < K +
2
) {
int
sz = q.size();
for
(
int
i =
0
; i < sz; i++) {
int
[] pr = q.poll();
if
(pr[
0
] == dst)
ans = Math.max(ans, pr[
1
]);
for
(
int
[] pr2 : adjlist.get(pr[
0
])) {
if
(!mp.containsKey(pr2[
0
])
|| mp.get(pr2[
0
])
> pr[
1
] + pr2[
1
]) {
q.add(
new
int
[] { pr2[
0
],
pr[
1
] + pr2[
1
] });
mp.put(pr2[
0
], pr[
1
] + pr2[
1
]);
}
}
}
level++;
}
return
ans != Integer.MIN_VALUE ? ans : -
1
;
}
public
static
void
main(String[] args)
{
int
n =
3
, src =
0
, dst =
2
, k =
1
;
int
[][] edges = { {
0
,
1
,
100
},
{
1
,
2
,
100
},
{
0
,
2
,
500
} };
System.out.println(
findShortestPath(n, edges, src, dst, k));
}
}