import
java.util.*;
class
Solution {
public
int
findMinRoute(
int
n,
int
[][] roads, List<Integer> fuelStations,
int
source,
int
destination,
int
capacity) {
List<List<Integer>> adj =
new
ArrayList<>();
for
(
int
i =
0
; i <= n; i++) {
adj.add(
new
ArrayList<>());
}
for
(
int
[] road : roads) {
int
u = road[
0
];
int
v = road[
1
];
adj.get(u).add(v);
adj.get(v).add(u);
}
int
[] caps =
new
int
[n +
1
];
for
(
int
node : fuelStations) {
caps[node] = capacity;
}
int
[] fuel =
new
int
[n +
1
];
Queue<
int
[]> queue =
new
LinkedList<>();
queue.offer(
new
int
[]{
0
, source});
fuel = capacity;
while
(!queue.isEmpty()) {
int
[] current = queue.poll();
int
dis = current[
0
];
int
node = current[
1
];
if
(node == destination) {
return
dis;
}
for
(
int
adjNode : adj.get(node)) {
int
newFuel = Math.max(caps[adjNode], fuel[node] -
1
);
if
(newFuel > fuel[adjNode]) {
fuel[adjNode] = newFuel;
queue.offer(
new
int
[]{dis +
1
, adjNode});
}
}
}
return
-
1
;
}
public
static
void
main(String[] args) {
int
n =
7
;
int
[][] roads = {{
1
,
2
}, {
2
,
3
}, {
3
,
4
}, {
4
,
5
}, {
4
,
6
}, {
6
,
7
}};
int
source =
1
;
int
destination =
7
;
int
capacity =
4
;
List<Integer> fuelStations = Arrays.asList(
5
);
System.out.println(
new
Solution().findMinRoute(n, roads, fuelStations, source, destination, capacity));
}
}