描述

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. sonar自定义规则

    Sonar并不是简单地把不同的代码检查工具结果(例如 FindBugs,PMD 等)直接显示在 Web 页面上,而是通过不同的插件对这些结果进行再加工处理,通过量化的方式度量代码质量的变化,从而可以方 ...

  2. webstorm上svn的安装使用

    1.首先要下载SlikSvn网址为:https://sliksvn.com/download/  进入该网站可以根据需要下载32位的或者64位的svn.下图为要下载的图标样式 点击下载即可. 2.在w ...

  3. putty登录显示IP

    登陆服务器 cd vi .bashrc 在尾部加入如下代码 if [ "$SSH_CONNECTION" != '' -a "$TERM" != 'linux' ...

  4. Zookpeer集群节点

    Adaptive Communication Environment(自适配通信环境),简称ACE. reference artfile:zookeeper单节点与集群的安装https://blog. ...

  5. Python : 什么是*args和**kwargs

    让生活Web个够 先来看个例子: def foo(*args, **kwargs): print 'args = ', args print 'kwargs = ', kwargs print '-- ...

  6. ACM__队列

    今天学回顾bfs的时候遇到了,遂总结一下 队列是一种特殊的线性表,只允许在队列的前端(front)进行删除操作,在队尾进行插入操作,进行插入操作的端称作队尾,进行删除操作的端称作对头.(来自百度百科) ...

  7. img标签在div里上下居中

    方法一:图片尺寸未知,IE8-不支持 CSS部分: <style> .content{ width:500px; height:500px; border:1px solid black; ...

  8. openvas开放式漏洞评估系统

    OpenVAS是开放式漏洞评估系统,也可以说它是一个包含着相关工具的网络扫描器.其核心部件是一个服务器,包括一套网络漏洞测试程序,可以检测远程系统和应用程序中的安全问题. 用户需要一种自动测试的方法, ...

  9. 使用__future__实现从python2.7到python3.x的过渡

    参考链接:http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/001386820023 ...

  10. (转) 为什么选择.NETCore?

    https://www.cnblogs.com/xiaoliangge/p/8373100.html 为什么选择.NETCore?    为什么选择.NETCore? 学习新的开发框架是一项巨大的投资 ...