This page is still under construction.

Parts of this page are still being built. What you see may change.

Pizza Delivery Minimum Time

Interview

Time limit1sMemory limit128 MB

Summary
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 11 and 1010 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 nn, the number of locations to deliver to, with 1≤n≤101 \le n \le 10. The next n+1n + 1 lines each contain n+1n + 1 integers describing the direct travel times among the pizzeria (numbered 00) and the nn delivery locations (numbered 11 to nn). The jj-th value on the ii-th of these lines is the time to travel directly from location ii to location jj 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 ii to jj need not equal the direct time from jj to ii. A line containing n=0n = 0 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 nn locations, and return to the pizzeria.

Examples1

  1. Example 1

    Input
    3
    0 1 10 10
    1 0 1 2
    10 1 0 10
    10 2 10 0
    0
    
    Expected output
    8