Hie with the Pie
time limit per test 1 second memory limit per test 256 megabytes
The Pizazz Pizzeria prides itself in delivering pizzas to its customers as fast as possible. Unfortunately, due to cutbacks, they can afford to hire only one driver to do the deliveries. He will wait for 1 or more (up to 10) orders to be processed before he starts any deliveries. Needless to say, he would like to take the shortest route in delivering these goodies and returning to the pizzeria, even if it means passing the same location(s) or the pizzeria more than once on the way. He has commissioned you to write a program to help him.

传送门:POJ3311

Input

Input will consist of multiple test cases. The first line will contain a single integer n indicating the number of orders to deliver, where 1 ≤ n ≤ 10. After this will be n + 1 lines each containing n + 1 integers indicating the times to travel between the pizzeria (numbered 0) and the n locations (numbers 1 to n). The jth value on the ith line indicates the time to go directly from location i to location j without visiting any other locations along the way. Note that there may be quicker ways to go from i to j via other locations, due to different speed limits, traffic lights, etc. Also, the time values may not be symmetric, i.e., the time to go directly from location i to j may not be the same as the time to go directly from location j to i. An input value of n = 0 will terminate input.

Output

For each test case, you should output a single number indicating the minimum time to deliver all of the pizzas and return to the pizzeria.

Sample Input

1
2
3
4
5
6
3
0 1 10 10
1 0 1 2
10 1 0 10
10 2 10 0
0

Sample Output

1
8

题解

TSP问题 n<15时用状压 n大一点且是平面图用双调欧里几德 如果每个城市能走多次 那就先计算个最短路就可以了
## AC code:(不包含输入类)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import java.io.*;  
import java.util.*;
public class Main {
static double[][]dp;
static double[][]dis;
static double INF=0x3f3f3f3f;
static int n;
public static void main(String[] args) {
FastScanner sc=new FastScanner();
PrintWriter pw=new PrintWriter(System.out);
while(sc.hasNext()){
n=sc.nextInt();
if(n==0)break;
n++;
int[][]floyd=new int[n][n];
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(i!=j)floyd[i][j]=99999999;
}
}
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
floyd[i][j]=sc.nextInt();
}
}


for(int k=0;k<n;k++){
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
floyd[i][j]=Math.min(floyd[i][j], floyd[i][k]+floyd[k][j]);
}
}
}
dis=new double[n][n];
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
dis[i][j]=floyd[i][j];
}
}
dp=new double[1<<n][n];
for(int i=0;i<(1<<n);i++){
for(int j=0;j<n;j++){
dp[i][j]=-1;
}
}
dp[(1<<n)-1][0]=0;
pw.println((int)rec(0,0));
pw.flush();
}

}
static double rec(int s,int v) {
if(dp[s][v]!=-1)
return dp[s][v];
double res=INF;
for(int u=0;u<n;u++)
{
if((s&(1<<u))==0)
{
res=Math.min(res,rec((s|(1<<u)),u)+dis[v][u]);
}
}
dp[s][v]=res;
return res;
}
}