Road Trip
InterviewTime limit1sMemory limit128 MB
Buy and resell fuel at each town's price within tank capacity to complete every leg of each journey at minimum cost.
- Level
Medium5 of 10
- Topics
- Greedy
- Solved
- No attempts yet
Problem
You are planning a road trip to visit friends who live in different towns. You do not want to spend more on fuel than you have to, but every town sells fuel at its own price, so the trip needs careful planning. Taking on extra fuel where it is cheap means buying less where it is expensive, and at some towns selling surplus fuel back is the better move. The tank holds only so much, and at every town you must take on enough fuel to reach the next one. Arriving with an empty tank is fine.
Write a program that plans the trip.
Input
The input describes a series of journeys. Each journey begins with a line holding the capacity of the fuel tank in litres () and the number of towns to visit (). A line containing two zeros ends the input.
The next lines describe the successive stages of that journey. Each line holds the price of one litre of fuel in the town at the start of the stage, written in fixed point dollars and cents with , and the number of litres needed to reach the next town, an integer with . Buying and selling use the same price.
Every stage is reachable, so never exceeds .
Output
Print one line per journey. Write the journey number in the form Journey k:, then a single space, then the minimum cost of completing that journey as a fixed point number with two decimal places. Here is the journey number, counting from 1.