• 状态压缩 DP


    D - 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 (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 /*
     2 dp[i][j]表示状态i时到达j点的时间,dp[i][j]=min(dp[i][j],dp[h][k]+d[k][j]),h表示的状态中没有j点
     3 d[k][j]表示k点到j点的距离,由于是求最短要用一个flody算法算出最短距离
     4 */
     5 #include<iostream>
     6 #include<string>
     7 #include<cstdio>
     8 #include<cmath>
     9 #include<cstring>
    10 #include<algorithm>
    11 #include<vector>
    12 #include<iomanip>
    13 #include<queue>
    14 #include<stack>
    15 using namespace std;
    16 int n;
    17 int d[11][11];
    18 int dp[1<<11][11];
    19 int min(int x,int y)
    20 {
    21     return x<y?x:y;
    22 }
    23 int main()
    24 {
    25     while(scanf("%d",&n))
    26     {
    27         if(n==0) break;
    28         n+=1;
    29         for(int i=0;i<n;i++)
    30         {
    31             for(int j=0;j<n;j++)
    32             scanf("%d",&d[i][j]);
    33         }
    34         for(int k=0;k<n;k++)
    35         for(int i=0;i<n;i++)
    36         for(int j=0;j<n;j++)
    37         {
    38             if(d[i][j]>d[i][k]+d[k][j])
    39             d[i][j]=d[i][k]+d[k][j];
    40         }
    41         for(int i=0;i<(1<<n);i++)
    42         {
    43             for(int j=0;j<n;j++)
    44             {
    45                 if(i==(1<<j))
    46                 dp[i][j]=d[0][j];   //从0点可以直接到达的点
    47                 else if(i&(1<<j))   //当i状态中包含j点时
    48                 {
    49                     dp[i][j]=1000000000; //初始化
    50                     for(int k=0;k<n;k++)
    51                     {
    52                         if((j!=k)&&(i&(1<<k)))  //k与j非同一个点,i状态中包含k点
    53                         dp[i][j]=min(dp[i][j],dp[i^(1<<j)][k]+d[k][j]);  //不包含j点但有k点的状态
    54                                                                         //加上d[k][j]
    55                     }
    56                 }
    57             }
    58         }
    59         int ans=100000000;
    60         for(int i=0;i<n;i++)
    61         ans=min(ans,dp[(1<<n)-1][i]+d[i][0]);
    62         cout<<ans<<endl;
    63     }
    64     return 0;
    65 }
  • 相关阅读:
    Access Token 机制详解
    Authorization Code 授权原理和实现方法
    Access Token 与 Refresh Token
    简单介绍 Oauth2.0 原理
    进行web开发时应该考虑的架构性因素
    查看linux服务器CPU数量
    Centos7 修改主机名
    Centos6 编译安装Python3.6
    Linux编译安装软件常见问题及排查
    问题列表
  • 原文地址:https://www.cnblogs.com/--ZHIYUAN/p/5743840.html
Copyright © 2020-2023  润新知