Pizza Delivery Minimum Time
InterviewTime limit1sMemory limit128 MB
Given directed travel times between a pizzeria and up to 10 stops, find the shortest round trip from the pizzeria visiting every stop.
- Level
Medium6 of 10
- Topics
- Shortest path, Dynamic programming, Bit manipulation, Graph
- Solved
- No attempts yet
Problem
A pizzeria wants to deliver its pizzas as fast as possible, but it can afford only one driver. Before setting out, the driver waits until between and orders have been placed. He wants the shortest possible route that starts at the pizzeria, delivers to every ordered location, and returns to the pizzeria. He is allowed to pass through any location, or the pizzeria itself, more than once along the way if that makes the route shorter. Write a program that computes the minimum total travel time.
Input
The input contains multiple test cases. Each test case begins with a line holding a single integer , the number of locations to deliver to, with . The next lines each contain integers describing the direct travel times among the pizzeria (numbered ) and the delivery locations (numbered to ). The -th value on the -th of these lines is the time to travel directly from location to location without stopping anywhere else. Because of one-way streets, speed limits, and traffic, a detour through other locations may be faster than the listed direct time, and the direct time from to need not equal the direct time from to . A line containing marks the end of the input and is not a test case.
Output
For each test case, print a single line containing one integer: the minimum total time needed to start at the pizzeria, deliver to all locations, and return to the pizzeria.