二进制状态压缩dp(旅行商TSP)POJ3311

http://poj.org/problem?id=3311

Hie with the Pie
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 4456   Accepted: 2355

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出发,要求经过1-n的所有点,可能会经过多次;问最后回到0点的最少时间是多少?

分析:首先用floyd求出两两点之间的最短有向路,这就抽象的形成了每个点只经过一次的旅行商问题,用状态压缩枚举每种状态,初始化的时候可以从1-n的任意点出发;dp[1<<i][i]=dis[0][i];代表已经经过了0,所以最后回到0 的时候0也是经过了一次;

#include"stdio.h"
#include"string.h"
#include"iostream"
#include"map"
#include"string"
#include"queue"
#include"stdlib.h"
#include"algorithm"
#include"math.h"
#define M 60001
#define eps 1e-10
#define inf 100000000
#define mod 100000000
#define INF 0x3f3f3f3f
using namespace std;
int dp[1<<13][13],px[13];
int G[13][13],dis[13][13];
void floyd(int n)
{
    int i,j,k;
    for(i=0;i<=n;i++)
        for(j=0;j<=n;j++)
            dis[i][j]=G[i][j];
    for(k=0;k<=n;k++)
    {
        for(i=0;i<=n;i++)
        {
            for(j=0;j<=n;j++)
            {
                if(dis[i][k]>=INF||dis[k][j]>=INF)continue;
                if(dis[i][j]>dis[i][k]+dis[k][j])
                    dis[i][j]=dis[i][k]+dis[k][j];
            }
        }
    }
}
int main()
{
    int i,j,k,n;
    px[0]=1;
    for(i=1;i<=11;i++)
        px[i]=px[i-1]*2;
    while(scanf("%d",&n),n)
    {
        for(i=0;i<=n;i++)
            for(j=0;j<=n;j++)
                scanf("%d",&G[i][j]);
        floyd(n);
        memset(dp,INF,sizeof(dp));
        for(i=1;i<=n;i++)
        dp[1<<i][i]=dis[0][i];//从第i个点开始已经走了多远;
        for(i=1;i<px[n+1];i++)
        {
            for(j=0;j<=n;j++)
            {
                int tep=i&(1<<j);
                if(tep==0)continue;
                int cur=i^(1<<j);
                for(k=0;k<=n;k++)
                {
                    if(dp[cur][k]>=INF)continue;
                    if(k==j)continue;
                    tep=cur&(1<<k);
                    if(tep==0)continue;
                    if(dp[i][j]>dp[cur][k]+dis[k][j])
                        dp[i][j]=dp[cur][k]+dis[k][j];
                }
            }
        }
        printf("%d\n",dp[px[n+1]-1][0]);
    }
}


你可能感兴趣的:(二进制)