POJ 3311—Hie with the Pie(状压DP),poj3311—hie

难点链接

 

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个地方的东西送完了再回到店里,商店及这n个地方任意两两之间的距离给了,即输入的(n+1)*(n+1)的矩阵,每个点可以到多次,求走的最短路径值;

思路:先用floyd计算两点之间的最短距离,定义dp[s][i],s表示已经走过的点,i表示现在正位于的点,dp[s][i]的值表示走完剩余没到的点(及加上回商店的路径长)所需的最短路径长,从底层开始推到即s=1<<(n+1):0;

代码如下:

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cstdlib>
using namespace std;
const int inf=99999999;
int dp[2505][12];
int d[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",&d[i][j]);
        for(int k=0;k<=n;k++)
        for(int i=0;i<=n;i++)
        for(int j=0;j<=n;j++)
            d[i][j]=min(d[i][j],d[i][k]+d[k][j]);

        int t=1<<(n+1);
        for(int i=0;i<t;i++)
        for(int j=0;j<=n;j++)
          dp[i][j]=inf;
        dp[t-1][0]=0;
        for(int s=t-1;s>=0;s--)
            for(int i=0;i<=n;i++)
            {
                if(!(s&(1<<i))&&(s||i)) continue;
                ///白书上没有这条语句,加上后可以减小运算量。为什么呢?因为i表示当前所在点,那s状态集合应该包含i,这样可以减少很大一部分的计算量,但是还得考虑s=0的情况
                ///s=0时是为了计算走完所有应送东西的点后,加上从结束的点回商店的距离,总距离最小者即为结果。
                ///或者也可以直接写if(!(s&(1<<i))) continue; 这样就得在最后计算从结束点回商店总距离,即下面注释部分代码;
                for(int j=0;j<=n;j++)
                {
                    if(s&(1<<j)) continue;
                    dp[s][i]=min(dp[s][i],dp[s^(1<<j)][j]+d[j][i]);
                }
            }
        printf("%dn",dp[0][0]);
//        int tmp=inf;
//        for(int i=1;i<=n;i++)
//        {
//            tmp=min(tmp,dp[(1<<i)][i]+d[i][0]);
//        }
//        printf("%dn",tmp);
    }
    return 0;
}

 

3311—Hie with the
Pie(状压DP卡塔 尔(英语:State of Qatar),poj3311—hie 标题链接 Description The Pizazz Pizzeria
prides itself in delivering pizzas to its customers as fast as possible.
Unfo…

Hie with the Pie
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
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 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

相关文章