zoukankan      html  css  js  c++  java
  • POJ 3311 Hie with the Pie (状压dp)

    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 jvia 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

    有个人要送快递,从0点出发到达n个点再回到0点,问最短时间
    状压dp入门题
    dp[stats][j]代表当前到达状态为stats(如到达过i点 (stats>>i)&1==1 ) 目的地为j的最短路
    之前先要搞一下floyd求任意两点之间最短路
     1 #include <cstdio>
     2 #include <cstring>
     3 #include <algorithm>
     4 #include <math.h>
     5 using namespace std;
     6 const int maxn = 15;
     7 const int inf = 0x3f3f3f3f;
     8 int a[maxn][maxn];
     9 int dis[maxn][maxn];
    10 int dp[11000][maxn];
    11 int n;
    12 int main()
    13 {
    14     //freopen("de.txt","r",stdin);
    15     while (~scanf("%d",&n)){
    16         if (n==0) break;
    17 
    18         for (int i=0;i<=n;++i)
    19             for (int j=0;j<=n;++j){
    20                 scanf("%d",&a[i][j]);
    21                 dis[i][j] = a[i][j];
    22             }
    23         for (int j=0;j<=n;++j){
    24             for (int i=0;i<=n;++i){
    25                 for(int k=0;k<=n;++k){
    26                     dis[i][j] = min(dis[i][j],dis[i][k]+a[k][j]);
    27                 }
    28             }
    29         }
    30         memset(dp,inf,sizeof dp);
    31         dp[1][0] = 0;
    32 
    33         for (int i = 1;i<(1<<(n+1));++i){
    34             i = i|1;//每个状态是要包括0点的联通的
    35             for (int j=0;j<=n;++j){
    36                 for (int k=0;k<=n;++k){
    37                     if (j!=k)
    38                     dp[(1<<k)|i][k] = min(dp[(1<<k)|i][k],dp[i][j]+dis[j][k]);
    39                 }
    40             }
    41         }
    42         printf("%d
    ",dp[(1<<(n+1))-1][0]);
    43     }
    44     return 0;
    45 }
     
  • 相关阅读:
    LeetCode 到底怎么刷?GitHub 上多位大厂程序员亲测的高效刷题方式
    第 15 篇:接口的单元测试
    【译】GitHub 为什么挂?官方的可行性报告为你解答
    微服务常用的几种部署方式——蓝绿部署,滚动升级,灰度发布/金丝雀发布
    .net生成PDF文件的几种方式
    读取私有字体信息并进行相关判断
    Windows Locale Codes
    总结C#获取当前路径的7种方法
    Devexpress aspxgridview oncustomcallback 无刷新更新数据
    ArcGIS Pro 版本、版本号、发布年代
  • 原文地址:https://www.cnblogs.com/agenthtb/p/7652959.html
Copyright © 2011-2022 走看看