atcoder#ABC143E. [ABC143E] Travel by Car
[ABC143E] Travel by Car
Score : points
Problem Statement
There are towns numbered to and roads. The -th road connects Town and Town bidirectionally and has a length of .
Takahashi will travel between these towns by car, passing through these roads. The fuel tank of his car can contain at most liters of fuel, and one liter of fuel is consumed for each unit distance traveled. When visiting a town while traveling, he can full the tank (or choose not to do so). Travel that results in the tank becoming empty halfway on the road cannot be done.
Process the following queries:
- The tank is now full. Find the minimum number of times he needs to full his tank while traveling from Town to Town . If Town is unreachable, print .
Constraints
- All values in input are integers.
- (if )
- (if )
- (if )
Input
Input is given from Standard Input in the following format:
Output
Print lines.
The -th line should contain the minimum number of times the tank needs to be fulled while traveling from Town to Town . If Town is unreachable, the line should contain instead.
3 2 5
1 2 3
2 3 3
2
3 2
1 3
0
1
To travel from Town to Town , we can use the second road to reach Town without fueling the tank on the way.
To travel from Town to Town , we can first use the first road to get to Town , full the tank, and use the second road to reach Town .
4 0 1
1
2 1
-1
There may be no road at all.
5 4 4
1 2 2
2 3 2
3 4 3
4 5 2
20
2 1
3 1
4 1
5 1
1 2
3 2
4 2
5 2
1 3
2 3
4 3
5 3
1 4
2 4
3 4
5 4
1 5
2 5
3 5
4 5
0
0
1
2
0
0
1
2
0
0
0
1
1
1
0
0
2
2
1
0