zoukankan      html  css  js  c++  java
  • POJ3311 Hie with the Pie

    Hie with the Pie
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 7624   Accepted: 4106

    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

    Source

     
     
    大意如下:
    给定一张有n + 1个节点的有向图,每两个点之间均有两条方向不一的边,权值不一定相同。问你从0号节点出发,走过所有1...n个节点并回到0号节点所花费的时间。
     
    首先Floyd预处理多源最短路。
     
    状压方程:
    f[S][i]表示从0号点出发,走过集合S中的点并到达i点的最短路长度。
     
    转移:
    f[S][i] = f[S/i][j] + g[j][i];
     
    初始状态:
    f[(1 << (i - 1))][i] = g[0][i]
     
    答案:
    min(f[(1 << n) - 1][i] + g[i][0]),i为1....n
     
    Code
     1 #include <cstdio>
     2 #include <cstring>
     3 #include <cstdlib>
     4 #include <cstring>
     5 #include <algorithm>
     6 #include <vector>
     7 #include <queue>
     8 #include <stack>
     9 inline void read(int &x){x = 0;char ch = getchar();char c = ch;while(ch < '0' || ch > '9')c = ch, ch = getchar();while(ch <= '9' && ch >= '0')x = x * 10 + ch - '0', ch = getchar();if(c == '-')x = -x;}
    10 inline int max(int a, int b){return a > b ? a : b;}
    11 inline int min(int a, int b){return a < b ? a : b;}
    12 
    13 const int INF = 0x3f3f3f3f;
    14 const int MAXN = 10 + 4;
    15 
    16 int n,g[MAXN][MAXN],dp[1 << MAXN][MAXN],ans;
    17 
    18 inline void init()
    19 {
    20     //清空
    21     memset(g, 0, sizeof(g));
    22     memset(dp, 0, sizeof(dp));
    23     //读入
    24     for(int i = 0;i <= n;++ i)
    25         for(int j = 0;j <= n;j ++)
    26             read(g[i][j]);
    27 }
    28 
    29 inline void Floyd()
    30 {
    31     for(int k = 0;k <= n;++ k)
    32         for(int i = 0;i <= n;++ i)
    33             for(int j = 0;j <= n;++ j)
    34                 if(g[i][j] > g[i][k] + g[k][j])
    35                     g[i][j] = g[i][k] + g[k][j];
    36 }
    37 
    38 //dp[S][j]  表示已经经过点集i到达j点的最短时间   1表示经过,0表示未经过
    39 //dp[S][j] = min(dp[S/k][k] + g[k][j])  k∈S  iS/k表示i去掉k 
    40 
    41 inline void DP()
    42 {    
    43     //枚举点集 
    44     for (int S = 1;S < (1 << n); ++ S)
    45         //枚举出发点 
    46         for (int i = 1;i <= n;++ i)
    47         {
    48             if(!(S & (1 << (i - 1))))continue; 
    49             //如果集合中只有出发点一个点  到达它需要g[0][i]的时间 
    50             if (S == (1 << (i - 1)))
    51             {
    52                 dp[S][i] = g[0][i];
    53                 break;
    54             }
    55             dp[S][i] = INF;
    56             for (int j = 1;j <= n;++ j)
    57             {
    58                 if (j == i)continue;
    59                 if (!(S & (1 << (j - 1))))continue;
    60                 dp[S][i] = min(dp[S][i], dp[S ^ (1 << (i - 1))][j] + g[j][i]);
    61             }    
    62         }
    63     ans = INF;
    64     for (int i = 1;i <= n;++ i)
    65         ans = min(ans, dp[(1 << n) - 1][i] + g[i][0]);
    66 }
    67 
    68 inline void out()
    69 {
    70     printf("%d
    ", ans);
    71 }
    72 
    73 int main()
    74 {
    75     while (scanf("%d", &n))
    76     {
    77         if (!n)break;
    78         //初始化操作 
    79         init(); Floyd();DP(); out();
    80     }
    81     return 0;
    82 }
    View Code
     
     
     
     
  • 相关阅读:
    写给QA/软件测试新人
    互联网产品线上故障管理规范
    爬了世纪佳缘后发现了一个秘密,世纪佳缘找对象靠谱吗?
    网传美团今年应届生年薪 35w+,严重倒挂老员工,为什么互联网大厂校招的薪资一年比一年高?...
    MySQL大表优化方案
    步入AI领域2年连升3级,我只是找对了学习方法而已……
    BZOJ 4008 亚瑟王(概率DP 奥妙重重)
    BZOJ 4318 OSU! (概率DP)
    BZOJ 3812 主旋律 (状压DP+容斥) + NOIP模拟赛 巨神兵(obelisk)(状压DP)
    BZOJ 4145 [AMPPZ2014]The Prices (状压DP)
  • 原文地址:https://www.cnblogs.com/huibixiaoxing/p/7103298.html
Copyright © 2011-2022 走看看