POJ 3311 Hie with the Pie 状压DP

题目描述:

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

题目分析:

题意是从0点出发到其它n个点至少一遍,最终回到0点最短路径。其中(n+1)*(n+1)的矩阵a[i][j]表示从i-1点到j-1点直达距离。(不一定最短)。并且从j-1到i-1直达距离与从i-1到j-1直达距离不一定相同。
TSP问题。
这道题好像可以不用DP做~ 搜索啊 启发式解法都可以。启发式解法我不懂0.0,搜索解法有许多重复运算,DP解法我比较喜欢,这里给出DP解法。
这里还需要用Floyd算法来更新两点间最短距离(因为直达不一定最短),其实我刚开始不知道这个算法,结果发现这个其实很简单0.0,很好理解。

代码如下:

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <string>
#include <math.h>
#include <stdlib.h>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;

int n;
int mp[12][12];
int dp[12][1<<12];//dp[i][j]表示j状态,到达i点最优解
//状态是由二进制意义转化为十进制表示的 若到i点经过则那一位为1,否则为0
void DP()
{
   for(int t=0; t<(1<<n); t++)//遍历所有状态
     for(int i=1; i<=n; i++)//从1点到n点遍历
     {
         if (t & (1<<(i-1)))//走到了i这个点
         {
             if (t == (1<<(i-1))) dp[i][t]=mp[0][i];//初始状态,仅仅走了i这个点
             else
             {
                 dp[i][t]=INF;
                 for(int j=1; j<=n; j++)//遍历中间有可能经过的点
                 {
                     if (i!=j && (t & 1<<(j-1)))//经过的点与终点不同
                       dp[i][t]=min(dp[i][t],mp[j][i]+dp[j][t^(1<<(i-1))]);
     //比较从中间点j走到i点t状态还是原来的方法,取其中小者
     //t是目标状态,1<<(i-1)是完全状态,需更新的状态为两者亦或
                 }
             }
         }
     }
}

int main()
{
    while(~scanf("%d",&n) && n)
    {
        int ans=0;
        for(int i=0; i<=n; i++)
            for(int j=0; j<=n; j++)
               scanf("%d",&mp[i][j]);
        for(int k=0; k<=n; k++)
            for(int i=0; i<=n; i++)
               for(int j=0; j<=n; j++)
                  mp[i][j]=min(mp[i][j],mp[i][k]+mp[k][j]);//Floyd算法
         DP();
         ans=dp[1][(1<<n)-1]+mp[1][0];//回到原点前的位置是1的情况
         for(int i=2; i<=n; i++)
         {
             if (ans > dp[i][(1<<n)-1]+mp[i][0]) ans=dp[i][(1<<n)-1]+mp[i][0];
             //寻找回到原点前的位置,更新更小的dp值
         }
         printf("%d\n",ans);
    }
    return 0;
}

你可能感兴趣的:(dp,poj,TSP问题)