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[][],dp[][];
int n;
void floyd()//求俩点的最短距离。
{
int i,j,k;
for(k=; k<=n; k++)
for(i=; i<=n; i++)
for(j=; 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!=)
{
for(i=; i<=n; i++)
for(j=; j<=n; j++)
scanf("%d",&dis[i][j]);
floyd();
for(s=; s<<<n; s++)//枚举所有的状态。
{
for(i=;i<=n; i++)//除去0城市。
{
if(s&<<(i-))//是否到达i个城市,s状态的i位是否为1.
{
if(s==<<(i-))//状态只能从0到达i个城市。
dp[i][s]=dis[][i];//初始化。dp的边界。
else//状态除了0外还可以从别的城市出发,但这个城市一定是状态已经为1的城市,到达i城市。
{
dp[i][s]=inf;
for(k=; k<=n; k++)
if((k!=i)&&(s&<<(k-)))//k一定是s状态里已经到达的为1.
dp[i][s]=min(dp[i][s],dp[k][s^(<<(i-))]+dis[k][i]);
//s^(1<<(i-1)),s的i位一定是1,但要变为0,处理。 }
}
}
}
int ans=inf;
for(i=;i<=n;i++)
{ ans=min(ans,dp[i][(<<n)-]+dis[i][]);
//先不考虑回来,只算到状态全1的情况,看哪个城市最后到达。 }
printf("%d\n",ans);
}
return ;
}
												

poj 3311 Hie with the Pie的更多相关文章

  1. poj 3311 Hie with the Pie dp+状压

    Hie with the Pie Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4671   Accepted: 2471 ...

  2. poj 3311 Hie with the Pie (TSP问题)

    Hie with the Pie Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4491   Accepted: 2376 ...

  3. POJ 3311 Hie with the Pie 最短路+状压DP

    Hie with the Pie Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 11243   Accepted: 5963 ...

  4. POJ 3311 Hie with the Pie(DP状态压缩+最短路径)

    题目链接:http://poj.org/problem?id=3311 题目大意:一个送披萨的,每次送外卖不超过10个地方,给你这些地方之间的时间,求送完外卖回到店里的总时间最小. Sample In ...

  5. POJ 3311 Hie with the Pie(状压DP + Floyd)

    题目链接:http://poj.org/problem?id=3311 Description The Pizazz Pizzeria prides itself in delivering pizz ...

  6. [POJ 3311]Hie with the Pie——谈论TSP难题DP解决方法

    主题连接:  id=3311">http://poj.org/problem?id=3311 题目大意:有n+1个点,给出点0~n的每两个点之间的距离,求这个图上TSP问题的最小解 ...

  7. POJ 3311 Hie with the Pie floyd+状压DP

    链接:http://poj.org/problem?id=3311 题意:有N个地点和一个出发点(N<=10),给出全部地点两两之间的距离,问从出发点出发,走遍全部地点再回到出发点的最短距离是多 ...

  8. POJ 3311 Hie with the Pie:TSP(旅行商)【节点可多次经过】

    题目链接:http://poj.org/problem?id=3311 题意: 你在0号点(pizza店),要往1到n号节点送pizza. 每个节点可以重复经过. 给你一个(n+1)*(n+1)的邻接 ...

  9. POJ 3311 Hie with the Pie (状压DP)

    dp[i][j][k] i代表此层用的状态序号 j上一层用的状态序号 k是层数&1(滚动数组) 标准流程 先预处理出所有合法数据存在status里 然后独立处理第一层 然后根据前一层的max推 ...

随机推荐

  1. POJ3080Blue Jeans

    http://poj.org/problem?id=3080 题意 : 给你几个DNA序列,让你找他们的共同的最长的子串,若是子串长度小于3,就输出no significant commonaliti ...

  2. Linux查看机器型号

    dmidecode | grep “Product Name”

  3. linux入门教程(三) Linux操作系统的安装

    因为笔者一直都是使用CentOS,所以这次安装系统也是基于CentOS的安装.把光盘插入光驱,设置bios光驱启动.进入光盘的欢迎界面. 其中有两个选项,可以直接按回车,也可以在当前界面下输入 lin ...

  4. hadoop 2.2.0的datanode中存储block的多个文件夹的负载均衡问题

    hadoop的分布式文件系统HDFS的存储方式是,将数据分成block,分布式存储在整个hadoop集群的datanode中,每个block默认的大小是64M,这些block文件的具体存储位置是在ha ...

  5. lintcode : 空格替换

    题目: 空格替换 设计一种方法,将一个字符串中的所有空格替换成 %20 .你可以假设该字符串有足够的空间来加入新的字符,且你得到的是“真实的”字符长度. 样例 对于字符串"Mr John S ...

  6. lintcode:Find the Connected Component in the Undirected Graph 找出无向图汇总的相连要素

    题目: 找出无向图汇总的相连要素 请找出无向图中相连要素的个数. 图中的每个节点包含其邻居的 1 个标签和 1 个列表.(一个无向图的相连节点(或节点)是一个子图,其中任意两个顶点通过路径相连,且不与 ...

  7. LR_问题_无法使用LR的Controller,提示缺少license

    问题描述 无法使用LR的Controller,提示缺少license 问题解决 使用开始->所有程序->HP LoadRunner->loadrunner,在打开界面的左上角选择co ...

  8. Java开发--操作MongoDB

    http://www.cnblogs.com/hoojo/archive/2011/06/01/2066426.html介绍到了在MongoDB的控制台完成MongoDB的数据操作,通过前一篇文章我们 ...

  9. Apollo,Python,Delphi与Oracle之间的神话关系

    在希腊历史上Delphi曾被认为是世界的中心,Apollo杀死Python后将其据为己有,在其神庙上刻有Oracle,曰:Γνωθι δεαυτόν (认识你自己自身关怀,Cognosce te ip ...

  10. esriFeatureType与esriGeometryType的区别与联系

    esriFeatureType通常用来表示数据的存储结构,即物理层: esriGeometryType通常用来表示数据的几何形状,即表现层. esriGeometryType枚举类型详解 常量 值 对 ...