【 狀壓dp 】 POJ 3311 Hie with the Pie

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 integern indicating the number of orders to deliver, where 1 ≤n ≤ 10. After this will ben + 1 lines each containing n + 1 integers indicating the times to travel between the pizzeria (numbered 0) and then locations (numbers 1 ton). The jth value on the ith line indicates the time to go directly from locationi to locationj without visiting any other locations along the way. Note that there may be quicker ways to go fromi toj 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 locationi toj may not be the same as the time to go directly from locationj toi. 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點,用矩陣的形式存儲每兩個點之間話費的時間。求最小時間。

思路:可以用Floyed處理一下,計算出兩點之間的最短時間,然後用二進制來儲存狀態。就是傳說中的狀壓dp。dp[i][j] 表示從j是到達i狀態經過的最後一個點。。。剩下的看代碼吧。

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define inf 0x3f3f3f3f
int d[1<<12][12];
int m[12][12];

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",&m[i][j]);
        memset(d,inf,sizeof(d));
        for(int k=0; k<=n; k++)
            for(int i=0; i<=n; i++)
                for(int j=0; j<=n; j++)
                    m[i][j]=min(m[i][j],m[i][k]+m[k][j]);//用Floyed找到最短
        d[0][0]=0;
        for(int i=1; i<(1<<n); i++)//遍歷狀態
        {
            for(int j=1; j<=n; j++)//遍歷店鋪 記得從1開始的
            {
                if(i&(1<<(j-1)))//如果到過這個店鋪
                {
                    int t = i - (1<<(j-1));
                    if(t == 0){
                        d[i][j]=m[0][j];//從0到達這個店鋪
                        continue;
                    }
                    for(int k=1; k<=n; k++)//遍歷店鋪
                    {
                        if(t&(1<<(k-1)))
                        {
                            d[i][j]=min(d[i][j],d[t][k]+m[k][j]);//從j到k店鋪
                        }
                    }
                }
            }
        }
        int res=inf;
        for(int i=1; i<=n; i++)
            res = min(res, d[(1<<n)-1][i]+m[i][0]);//加上從最後一個店鋪回去0店鋪..
        printf("%d\n",res);
    }
}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章