Fastest Route
InterviewTime limit8sMemory limit512 MB
Given N stages and N pieces of equipment, where clearing stage i yields equipment i and each stage can be done in any order with at most one equipment, find the minimum total time to clear all stages.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Bit manipulation, Greedy
- Solved
- No attempts yet
Problem
Your intellectual programming circle (known as the Intelligent Clever Programming Circle, ICPC) is in the middle of a big cleanup. The club room is crammed with all kinds of items left behind by generations of seniors. While organizing a shelf, you find a large stash of retro games sealed away in the back. Some of them look familiar, so you decide to play one again after a long time.
The details of the game you found are as follows.
The game has N stages numbered 1 through N, and you can clear them in any order. There are also pieces of equipment numbered 1 through N, and using them shortens the time needed to clear a stage. You start the game with no equipment, but clearing stage i gives you equipment i, and once obtained it can be used any number of times. You can use only one piece of equipment on a stage, but you can use the same equipment on different stages.
You have cleared this game before, so for each piece of equipment you know the time it takes to clear each stage when that equipment is used. Clearing it normally would be boring, so you decide to minimize the total time until all stages are cleared. Drawing on your ICPC experience, you decide to write a program that, given this information, computes the minimum time needed to clear all the stages.
Input
The input consists of multiple datasets. The end of the input is given by a line containing a single zero. Each dataset describes one game, in the following format.
N
t10 t11 ... t1N
t20 t21 ... t2N
...
tN0 tN1 ... tNN
The first line of a dataset contains a single integer N, the number of stages. The following N lines contain N+1 integers describing the clearing times of the stages. ti0 is the time needed to clear stage i without equipment. ti j (j > 0) is the time needed to clear stage i with equipment j.
Each value satisfies the following constraints.
- 1 ≤ N ≤ 16
- 1 ≤ ti j ≤ 100,000
Output
For each dataset, output on one line an integer representing the minimum time needed to clear all the stages. The output line must not contain any characters other than this number.