poj3311 Hie with the Pie【floyd最短路+状态压缩】

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

这个题又暴露了一堆问题==

说题意:有个人送外卖,每次选择最短的方式到下一家,最终还要回到餐厅,注意“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”!!每个点不止遍历一次,最开始就没发现这个点==

思路:看到这种裸的状态压缩转移,想到了惨不忍睹的校赛题目:(要不说还是摔的狠挺长记性的orz

zoj3471&nefu1009游戏争霸赛【状态压缩】

那就写吧。写啊写,为啥思路对了,正确答案得8,我得4呢,手动模拟,把6种情况写出来果然是8,==咦?为啥网上的都是二维,第二维表示当前状态中遍历的最后一个点,啊,发现自己思维漏洞了orz,与游戏争霸赛不同的是,那个题不需要考虑前后关系,遍历一遍到最后输出就行了,这个题需要记录最后的点,因为还得回来啊!遍历过同样的点,但是以不同的点结尾,答案是不同的==

#include <iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int dp[1<<11][20],n,num[11][11];
int min(int a,int b){if(a<b)return a;return b;}
int judge(int s)
{
    int num=0;
    while(s)
    {
        if(s&1)num++;
        s>>=1;
    }
    return num;
}
int dist[11][11];
void init()
{
    for(int i=0;i<n;i++)
    for(int j=0;j<n;j++)
    {
        dist[i][j]=num[i][j];
    }
    for(int i=0;i<n;i++)
    for(int j=0;j<n;j++)
    for(int k=0;k<n;k++)
    if(dist[j][i]+dist[i][k]<dist[j][k])
    dist[j][k]=dist[j][i]+dist[i][k];
    for(int i=0;i<n;i++)
    for(int j=0;j<n;j++);
  //  printf("i=%d,j=%d,dist=%d\n",i,j,dist[i][j]);
}
int main()
{
  //  freopen("cin.txt","r",stdin);
    while(~scanf("%d",&n)&&n)
    {
        for(int i=0;i<=n;i++)
            for(int j=0;j<=n;j++)
                scanf("%d",&num[i][j]);
        memset(dp,0x3f3f3f3f,sizeof(dp));
        dp[0][0]=0;
        n++;//have n+1 points
        init();
        for(int i=0;i<n;i++)//n points
            dp[1<<i|1][i]=dist[0][i];
       // for(int i=0;i<(1<<n)-1;i++) if(dp[i]!=0x3f3f3f3f)printf("i=%d  dp=%d   ",i,dp[i]);
        int ans=0x3f3f3f3f;
        for(int i=1;i<(1<<n)-1;i++)//now
        {
            for(int j=0;j<n;j++)//not exist
            {
                if(i&(1<<j)) continue;
                for(int k=0;k<n;k++)//exist
                {
                    if(j==k) continue;
                    if(i&(1<<k))
                    {
                        dp[1<<j|i][j]=min(dp[i][k]+dist[k][j],dp[1<<j|i][j]);
                        if(judge(1<<j|i)==n)
                            ans=min(ans,dp[1<<j|i][j]+dist[j][0]);
                    }
                }
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}


你可能感兴趣的:(poj,状态压缩dp,Floyd最短路)