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

    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

     
    /*
    dp[i][j]表示在i个状态下最后到达j城市的最小时间
    */
    
    #include<iostream>
    #include<string.h>
    #include<stdio.h>
    #define N (1<<10)+5
    #define M 15
    #define INF 0x3f3f3f3f
    using namespace std;
    int dp[N][M],g[M][M],n;//dp[i][j]表示在第i个状态下最后到达j城市的最小时间,g[i][j]表示从i城市到达j城市的最小时间
    void floyd()//求出从i城市到j城市的最短路径(佛洛依德算法)
    {
        for(int k=0;k<=n;k++)//枚举中间点
            for(int i=0;i<=n;i++)//枚举起点
                for(int j=0;j<=n;j++)//枚举终点
                    g[i][j]=min(g[i][j],g[i][k]+g[k][j]);
    }
    int main()
    {
        //freopen("in.txt", "r", stdin);
        while(scanf("%d",&n)!=EOF&&n)
        {
            //memset(dp,0,sizeof dp);
            //memset(g,INF,sizeof g);
            for(int i=0;i<=n;i++)
                for(int j=0;j<=n;j++)
                    scanf("%d",&g[i][j]);
            floyd();//求出从i到j的最短路径
            int tol=(1<<n);
            for(int i=0;i<tol;i++)//枚举状态
            {
                for(int j=1;j<=n;j++)//枚举你最后要到达的城市
                {
                    if(i==(1<<(j-1)))//i状态这是就是只有j的位置是1,也就是什么城市也没去的状态到达j城市的最小时间,当然就是从0到这个位置的时间了
                        dp[i][j]=g[0][j];
                    else if(i&(1<<(j-1)))
                    {
                        dp[i][j]=INF;
                        for(int k=1;k<=n;k++)
                            if(k!=j&&(i&(1<<(k-1))))//k和j不是一个状态,并且k城市确定已经送到了
                            dp[i][j]=min(dp[i][j],dp[i^(1<<(j-1))][k]+g[k][j]);//dp[i^(i<<(j-1))][k]表示在没到达j状态的条件下,最后到达
                    }
                }
            }
            int cur=INF;
            for(int i=1;i<=n;i++)//枚举你要选择的终点
            {
                cur=min(cur,dp[tol-1][i]+g[i][0]);//到达i点之后还要回到零点
            }
            printf("%d
    ",cur);
        }
        return 0;
    }
  • 相关阅读:
    第四章 虚拟机性能监控与故障处理工具
    C++_异常5-异常规范和栈解退
    C++_异常4-将对象用作异常类型
    C++_异常3-异常机制throw try catch
    C++_异常2-返回错误码
    C++_异常1-调用abort()
    C++_类继承7-类设计回顾
    C++_类继承6-继承和动态内存分配
    C++_类继承5-抽象基类
    C++_类继承4-访问控制protected
  • 原文地址:https://www.cnblogs.com/wuwangchuxin0924/p/5740387.html
Copyright © 2011-2022 走看看