描述

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

输出

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.

样例输入

3
0 1 10 10
1 0 1 2
10 1 0 10
10 2 10 0
0

样例输出

8

题意

从0出发,走n个城市后回到0,所经过的最短路径

题解

dp[i][j]表示状态i最后到点j的最短路径,1表示到达过,0表示还未到达

我们可以从子推出它的父,就是从子状态i经过j点到最后的k点,就变成父状态(i<<k)|i

dp[i][j]+d[j][k]=dp[(i<<k)|i][[k]

上面的d为从j到k的最短路,可以用floyd跑出任意两个点的最短路

代码

 #include<stdio.h>
#include<string.h> int G[][],d[][],dp[<<][];
int main()
{
int n;
while(scanf("%d",&n)!=EOF,n)
{
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
scanf("%d",&G[i][j]),d[i][j]=G[i][j]; for(int k=;k<=n;k++)
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
if(d[i][j]>d[i][k]+G[k][j])
d[i][j]=d[i][k]+G[k][j]; memset(dp,-,sizeof dp);
dp[][]=;
for(int i=;i<(<<(n+));i++)
{
i|=;
for(int j=;j<=n;j++)
{
if(dp[i][j]==-)continue;
for(int k=;k<=n;k++)
{
if(j!=k&&(dp[(<<k)|i][k]==-||dp[(<<k)|i][k]>dp[i][j]+d[j][k]))
dp[(<<k)|i][k]=dp[i][j]+d[j][k];
}
}
}
printf("%d\n",dp[(<<(n+))-][]);
}
return ;
}

TZOJ 1937 Hie with the Pie(floyd+状压dp)的更多相关文章

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

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

  2. POJ3311 Hie with the Pie 【状压dp/TSP问题】

    题目链接:http://poj.org/problem?id=3311 Hie with the Pie Time Limit: 2000MS   Memory Limit: 65536K Total ...

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

    题意: 每个点都可以走多次的TSP问题:有n个点(n<=11),从点1出发,经过其他所有点至少1次,并回到原点1,使得路程最短是多少? 思路: 同HDU 5418 VICTOR AND WORL ...

  4. 【POJ3311】Hie with the Pie(状压DP,最短路)

    题意: 思路:状压DP入门题 #include<cstdio> #include<cstdlib> #include<algorithm> #include< ...

  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 【状压DP】

    Description The Pizazz Pizzeria prides itself in delivering pizzas to its customers as fast as possi ...

  7. Hie with the Pie(状压DP+可以经过多次相同的点要全部走过的最短回路)

    大意:一个人要送n份货,给出一个矩阵,表示任意两个点间的直接路径长度,求从起点0送完这n份货(到达指定的n个地点)再回到起点0的最短时间.经过任意顶点的次数不限. 分析:既然是可以过多个点,那我们可以 ...

  8. POJ3311 Hie with the Pie(状压DP,Tsp)

    本题是经典的Tsp问题的变形,Tsp问题就是要求从起点出发经过每个节点一次再回到起点的距离最小值,本题的区别就是可以经过一个节点不止一次,那么先预处理出任意两点之间的最短距离就行了,因为再多走只会浪费 ...

  9. poj 3311 Hie with the Pie (状压dp) (Tsp问题)

    这道题就是Tsp问题,稍微加了些改变 注意以下问题 (1)每个点可以经过多次,这里就可以用弗洛伊德初始化最短距离 (2)在循环中集合可以用S表示更清晰一些 (3)第一维为状态,第二维为在哪个点,不要写 ...

随机推荐

  1. CSS 点击事件

    :active 伪类向激活(在鼠标点击与释放之间发生的事件)的元素添加特殊的样式. 这个伪类应用于处于激活状态的元素.最常见的例子就是在 HTML 文档中点击一个超链接:在鼠标按钮按下期间,这个链接是 ...

  2. netty为啥要二次开发

    很早之前就看过李林峰写的netty的书,但是感觉没有直接用到还是理解不够深入,现在的公司有两套自己基于Netty开发的系统,感觉才真正理解为啥要这么做 借用别人文章回顾下 https://www.cn ...

  3. 剑指offer例题——二维数组中的查找

    //实现一个函数,将一个字符串的每个空格替换成“%20”.例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy. public class Solutio ...

  4. TFS登录时保存了用户密码,如何用其他账户登录

      来源:http://blog.csdn.net/littlegreenfrog/article/details/5254633 使用TFS2008过程中,常常由于已经保存用户名和密码,却没有重新登 ...

  5. [JS]如何理解JS中的类和对象

    -------------------------------------------------------------------------------------------- 变量:自由的 ...

  6. Hibernate 再接触 基础配置 搭建Log4j环境 Junit日志环境等

    <!-- Drop and re-create the database schema on startup --> <property name="hbm2ddl.aut ...

  7. gparted增加Ubuntu14.04根目录空间(转)

    转自:https://blog.csdn.net/t765833631/article/details/79031063 在win7上装了Ubuntu14.04双系统后,突然发现ubuntu开机会弹出 ...

  8. Linux部署项目

    1 安装jdk 第一步:获取Linux系统中jdk安装包和tomcat安装包(后面要用,所以上传两个) 第二步:使用secureCRT客户端工具连到服务器 第三步:使用命令创建一个目录,作为软件的安装 ...

  9. js判断是否为undefined

    typeof(isadmin)=="undefined"需要使用typeof才能判断

  10. html -引入其他html页面

    其他页面html为:ip.html 主页面代码 <body> <div id="ip"></div> </body> <scr ...