Problem Statement | |||||||||||||
Alice is about to set out in her car on a long journey. Her car's fuel tank can only carry fuelTank units of fuel, so she may have to stop at gas stations along the way to refuel. Prices vary across different stations, so she needs to plan ahead to minimize the total cost of the journey.
The network of roads Alice is driving on is represented by an undirected graph with N nodes, in which she starts at node 0 and wishes to get to node 1. Initially, there is no fuel in her car's fuel tank. There is a fuel station located at each node and the cost per unit fuel at node i is fuelCost[i]. The graph is described by a String[] roads. The concatenation of the elements of roads forms a space-separated list of edges. Each edge is formatted "i,j,fuel" (quotes for clarity), in which i, j and fuel are integers formatted without leading zeros. This denotes that there is a bidirectional road connecting nodes i and j and fuel units of fuel will be consumed from the fuel tank in traversing this road. Alice doesn't want to end up stranded, so she cannot traverse a road with less than fuel units of fuel in the tank (although she can safely drive the road with exactly enough fuel). Return the minimum cost of completing the journey or -1 if it is impossible to get from node 0 to node 1. | |||||||||||||
Definition | |||||||||||||
| |||||||||||||
Notes | |||||||||||||
- | There is no limit to the amount of fuel that Alice can buy at each node. | ||||||||||||
Constraints | |||||||||||||
- | fuelPrices will contain between 2 and 50 elements, inclusive. | ||||||||||||
- | Each element of fuelPrices will be between 1 and 1000000 (10^6), inclusive. | ||||||||||||
- | fuelTank will be between 1 and 1000000 (10^6), inclusive. | ||||||||||||
- | roads will contain between 1 and 50 elements, inclusive. | ||||||||||||
- | Each element of roads will contain between 1 and 50 characters, inclusive. | ||||||||||||
- | The concatenation of the elements of roads will be a single-space-separated list of edges (as described in the problem statement), without leading or trailing spaces. | ||||||||||||
- | In each edge in roads, i and j will be between 0 and N-1, inclusive, where N is the number of elements in fuelTank. | ||||||||||||
- | In each edge in roads, i will be strictly less than j. | ||||||||||||
- | In each edge in roads, fuel will be between 1 and fuelTank, inclusive. | ||||||||||||
- | The i, j pairs of the edges in roads will be distinct. | ||||||||||||
Examples | |||||||||||||
0) | |||||||||||||
| |||||||||||||
1) | |||||||||||||
| |||||||||||||
2) | |||||||||||||
| |||||||||||||
3) | |||||||||||||
| |||||||||||||
4) | |||||||||||||
|