Hie with the Pie POJ - 3311 (状压dp+最短路)

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点出发,走遍所有点后,回到0点的最短距离

思路:先用floyd求出任意两点的最短距离,再考虑将要走的城市进行状压。

影响路径长度的一定是先到哪里,后到哪里,所以考虑利用状压来枚举先后顺序。

dp[s][i]表示已经走了s的状态,且最后位于i点的最小值,状态转移应该是在s中选点j,看dp[s^(1<<i)][j]+dist[j][i]的大小.

代码:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <map>
#include <vector>
#include <set>
#include <string>
#include <math.h>
#include <stack>
#include <bitset>
typedef long long ll;
#define INF 0x3f3f3f3f
const int maxn=1e5+10;
const int MAXN=1e3+10;
const long long mod=100000000;
using namespace std;
int n;
int dist[30][30];
int dp[1<<11][11];
int main(int argc, char const *argv[])
{
    #ifndef ONLINE_JUDGE
        freopen("in.txt","r",stdin);
        freopen("out.txt","w",stdout);
    #endif
    while(scanf("%d",&n)!=EOF&&n)
    {
        memset(dist,INF,sizeof(dist));
        for(int i=0;i<=n;i++)
        {
            for(int j=0;j<=n;j++)
            {
                scanf("%d",&dist[i][j]);
            }
        }
        for(int i=0;i<=n;i++)
        {
            for(int k=0;k<=n;k++)
            {
                for(int j=0;j<=n;j++)
                {
                    dist[i][j]=min(dist[i][j],dist[i][k]+dist[k][j]);
                }
            }
        }
        int up=(1<<n);
        for(int s=0;s<up;s++)
        {
            for(int i=1;i<=n;i++)
            {
                if(s&(1<<(i-1)))
                {
                    if(s==(1<<(i-1)))
                    {
                        dp[s][i]=dist[0][i];
                    }
                    else
                    {
                        dp[s][i]=INF;
                        for(int j=1;j<=n;j++)
                        {
                            if(s&(1<<(j-1))&&j!=i)
                            {
                                dp[s][i]=min(dp[s^(1<<(i-1))][j]+dist[j][i],dp[s][i]);
                            }
                        }
                    }
                }
            }
        }
        int ans=INF;
        for(int i=1;i<=n;i++)
        {
            ans=min(ans,dp[up-1][i]+dist[i][0]);
        }
        printf("%d\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40774175/article/details/81744196