• poj 3311 Hie with the Pie


    floyd,旅游问题每个点都要到,可重复,最后回来,dp
    Hie with the Pie
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 4013   Accepted: 2132

    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 jwithout 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
    题意:题意:一个人送外卖,他从0点出发,送外卖到n个地方,给出每个点之间的距离,帮他选择一条最短路回到原点。注意他走的节点可以重复。
    分析:
    先用floyd求每俩个点,然后进行dp,dp[i][j],表示最后到达i城市j状态,1表示以到达城市,0未到达。
    是用状态来更新城市,比如011,最后到达第1个城市可以从010改变而来,则010就必须要有值。
    具体见代码解释。先不考虑回来,只算到状态全1的情况,看哪个城市最后到达。
    //dp[i][j],最后到达城市i,状态j,最小的路径。
    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #define inf 1<<27
    using namespace std;
    int dis[20][20],dp[20][1025];
    int n;
    void floyd()//求俩点的最短距离。
    {
        int i,j,k;
        for(k=0; k<=n; k++)
            for(i=0; i<=n; i++)
                for(j=0; j<=n; j++)
                    dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]);
    }
    int main()
    {
        int i,j,s,k;
        while(~scanf("%d",&n)&&n!=0)
        {
            for(i=0; i<=n; i++)
                for(j=0; j<=n; j++)
                    scanf("%d",&dis[i][j]);
            floyd();
            for(s=1; s<1<<n; s++)//枚举所有的状态。
               {
                for(i=1;i<=n; i++)//除去0城市。
                 { 
                     if(s&1<<(i-1))//是否到达i个城市,s状态的i位是否为1.
                      {
                        if(s==1<<(i-1))//状态只能从0到达i个城市。
                            dp[i][s]=dis[0][i];//初始化。dp的边界。
                         else//状态除了0外还可以从别的城市出发,但这个城市一定是状态已经为1的城市,到达i城市。
                         {
                               dp[i][s]=inf;
                            for(k=1; k<=n; k++)
                               if((k!=i)&&(s&1<<(k-1)))//k一定是s状态里已经到达的为1.
                                  dp[i][s]=min(dp[i][s],dp[k][s^(1<<(i-1))]+dis[k][i]);
                                  //s^(1<<(i-1)),s的i位一定是1,但要变为0,处理。
                                 
                         }
                    }
                }
            }
            int ans=inf;
            for(i=1;i<=n;i++)
            {
    
                ans=min(ans,dp[i][(1<<n)-1]+dis[i][0]);
                //先不考虑回来,只算到状态全1的情况,看哪个城市最后到达。
    
            }
            printf("%d
    ",ans);
        }
        return 0;
    }
    
    
  • 相关阅读:
    Node入门
    Java try-catch、throw和throws的几点想法
    SpringMVC的四种HandlerMapping
    枚举
    MyBatis表和实体关联
    MyBatis xml和dao层接口组合使用
    Spring Task 定时器
    (转)Spring定时任务的几种实现
    Struts2+Spring发送邮件
    Struts2文件上传
  • 原文地址:https://www.cnblogs.com/cancangood/p/3878654.html
Copyright © 2020-2023  润新知