poj3311

Hie with the Pie
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 8467   Accepted: 4628

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

题意:有N个地点(1~N)和一个PIZZA店(0),要求一条回路,从0出发,又回到0,且经过这N个点(每个点可经过多次),而且距离最短。
分析:先用floyd求出任意两点的最短距离,再枚举所有状态,用二进制数S表示,
S中第i位为1表示经过点i,为0则表示不经过点i,状态(S,i)表示在状态S下到达点i的最短路程,
状态转移方程为dp[S][i]=min(dp[S][i],dp[S^(1<<(i-1))][k]+dis[k][i]),0<k<=N且k!=i,与floyd类似,
最后对于S中全是1的状态,ans=min(dp[S][i]+dis[i][0]),0<i<=N。
#include<cstdio>
#include<cstring>
#include<algorithm>
#define INF 999999999
using namespace std;
int dis[11][11];
int dp[1<<11][11];
int main()
{
    int N;
    while(scanf("%d",&N)&&N)
    {
        for(int i=0;i<=N;i++)
        for(int j=0;j<=N;j++)
            scanf("%d",&dis[i][j]);
        
        for(int k=0;k<=N;k++)
        for(int i=0;i<=N;i++)
        for(int j=0;j<=N;j++)
        dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]);
//dp[S][i]表示从点0经过状态S(数S第i位为0表示不经过点i,为1表示过点i),最后到达点i的最短路程
        int S=0;
        for(;S<(1<<N);S++)//枚举集合S,数S中的1表示经过第i个点 
            for(int i=1;i<=N;i++)//枚举每个点 
                if(S&(1<<(i-1)))//状态S经过点i 
                {
                    if(S==(1<<(i-1)))//状态S只经过点i 
                        dp[S][i]=dis[0][i];//相当于DP的边界条件 
                    else
                    {
                        dp[S][i]=INF;
                        for(int k=1;k<=N;k++)
                        {
                            if(k!=i&&(S&(1<<(k-1))))//枚举状态S中除去点0与点i的其他点,跟floyd相似 
                            dp[S][i]=min(dp[S][i],dp[S^(1<<(i-1))][k]+dis[k][i]);
                            //S^(1<<(i-1))通过异或把状态S中的点i去掉 
                        }
                    }
                }
        int ans=dp[--S][1]+dis[1][0]; 
        for(int i=2;i<=N;i++)
        ans=min(dp[S][i]+dis[i][0],ans);
        printf("%d\n",ans);
    }
    return 0;
}
View Code

 







posted @ 2018-01-31 16:31  ACRykl  阅读(177)  评论(0编辑  收藏  举报