Hie with the Pie POJ

来源:互联网 发布:oracle数据库log文件 编辑:程序博客网 时间:2024/05/17 06:40

Hie with the Pie
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 7637 Accepted: 4115

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 integern indicating the number of orders to deliver, where 1 ≤ n ≤ 10. After this will ben + 1 lines each containing n + 1 integers indicating the times to travel between the pizzeria (numbered 0) and then locations (numbers 1 to n). The jth value on the ith line indicates the time to go directly from location i to locationj without visiting any other locations along the way. Note that there may be quicker ways to go fromi 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 locationi to j may not be the same as the time to go directly from locationj 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

30 1 10 101 0 1 210 1 0 1010 2 10 00

Sample Output

8


Floyd + 状态压缩DP  
题意是有N个城市(1~N)和一个PIZZA店(0),要求一条回路,从0出发,又回到0,而且距离最短  
也就是TSP(旅行商)问题,首先不难想到用FLOYD先求出任意2点的距离dis[i][j]  
接着枚举所有状态,用11位二进制表示10个城市和pizza店,1表示经过,0表示没有经过  

定义状态DP(S,i)表示在S状态下,到达城市I的最优值  
接着状态转移方程:DP(S,i) = min{DP(S^(1<<i-1),j) + dis[j][i],DP(S,i)},
    其中S^(1<<i-1)表示未到达城市i的所有状态  
对于全1的状态,即S = (1<<n)-1则表示经过所有城市的状态,最终还需要回到PIZZA店0  
那么最终答案就是min{DP(S,i) + dis[i][0]} 


#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>#include <queue>#include <vector>#include <cmath>#include <stack>#include <string>#include <map>#include <set>#define pi acos(-1)#define LL long long#define INF 0x3f3f3f3fusing namespace std;const int maxn = 1e5 + 5;int dp[1<<11][12];int dis[12][12];int main(void){//freopen("C:\\Users\\wave\\Desktop\\NULL.exe\\NULL\\in.txt","r", stdin);int n, i, j, k, ans, S;while (cin >> n && n){for (i = 0; i <= n; i++)for (j = 0; j <= n; j++)cin >> dis[i][j];for (k = 0; k <= n; k++)for (i = 0; i <= n; i++)for (j = 0; j <= n; j++)dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);for (S = 0; S < (1<<n); S++){ // 枚举所有状态 用位运算表示 for (i = 1; i <= n; i++){if (!(S & (1<<(i-1)))) // 如果当前状态中没有i continue;//  说明当前状态没有经过i城市 if (S == (1<<(i-1)))dp[S][i] = dis[0][i];// 状态S只经过城市I,最优解自然是从0出发到i的dis,这也是DP的边界 else {dp[S][i] = INF;for (j = 1; j <= n; j++){if (S & (1<<(j-1)) && j != i)dp[S][i] = min(dp[S][i], dp[S^(1<<(i-1))][j] + dis[j][i]);//在没经过城市I的所有状态中,寻找合适的中间点J使得距离更短,和FLOYD一样// 异或运算 ^   S^(1<<(i-1)) 作用是在状态中去掉i城市//  S|(1<<(i-1)) 作用是在状态中加上i城市 }}}}ans = INF;for (i = 1; i <= n; i++) // 找出最小的 ans = min(ans, dp[(1<<n)-1][i] + dis[i][0]);cout << ans << endl;}return 0;}