poj3311 Hie with the Pie (状态压缩dp,旅行商)

时间:2022-03-23 12:11:57

Hie with the Pie

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 3160   Accepted: 1613

Description

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.

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

3
0 1 10 10
1 0 1 2
10 1 0 10
10 2 10 0
0

Sample Output

8

Source

 
用二进制位表示某一点是否走过,1表示走过,0表示没走过,例如如果有四个点,3的二进制是(0011)2,就表示1点和2点走过,3点和4点没走,用dp(i,j)表示状态为i时,走到j点的最短距离,状态i就是前面所说的二进制标记,那么在i点j为1,2时才有效(通过 i & (1<<(j - 1))来检测在状态i下哪一位是1),根据此状态,能更新dp(7,3)和dp(11,4),即更新二进制位下为0的点,7是(0111)2,11是(1011)2
 
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
const int N = ;
const int INF = 0x7f7f7f7f;
int graph[N][N];
int dp[(<<)+][];
void floyd(int n) {
int i,j,k;
for (k = ; k <= n; k++) {
for (i = ; i <= n; i++) {
for (j = ; j <= n; j++) {
if (graph[i][k] + graph[k][j] < graph[i][j])
graph[i][j] = graph[i][k] + graph[k][j];
}
}
}
}
int main() {
// freopen("in.txt","r",stdin);
int n;
int i,j,k;
while (scanf("%d",&n) && n) {
for (i = ; i <= n; i++) {
for (j = ; j <= n; j++)
scanf("%d",&graph[i][j]);
}
floyd(n);
memset(dp, 0x7f, sizeof(dp));
for (i = ; i <= n; i++) {
dp[<<(i - )][i] = graph[][i];
}
for (i = ; i < (<<n); i++) {
for (j = ; j <= n; j++) {
if ((i & ( << (j - ))) == ) {
for (k = ; k <= n; k++) {
if (i & ( << (k - ))) {
dp[(i|( << (j - )))][j] = min(dp[(i|( << (j - )))][j], dp[i][k] + graph[k][j]);
}
}
}
}
}
int mini = INF;
for(i = ; i <= n; i++) {
if (dp[(<<n) - ][i] + graph[i][] < mini)
mini = dp[(<<n) - ][i] + graph[i][];
}
printf("%d\n",mini);
}
return ;
}