描述

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. HBuilder开发APP自动登录时跳过"登录页面"

    刚接触开发公司APP项目,用HBuilder开发工具. manifest.json中的入口页面就是"登录页面",现在获取到自动登录状态是true,但是真机联调时"登录页面 ...

  2. Oracle 学习总结 - 表和索引的性能优化

    表的性能 表的性能取决于创建表之前所应用的数据库特性,数据库->表空间->表,创建数据库时确保为每个用户创建一个默认的永久表空间和临时表空间并使用本地管理,创建表空间设为本地管理并且自动段 ...

  3. C# 中奇妙的函数–String Split 和 Join

    很多时候处理字符串数据,比如从文件中读取或者存入 - 我们可能需要加入分隔符(如CSV文件中的逗号),或使用一个分隔符来合并字符串序列. 很多人都知道使用split()的方法,但使用与其对应的Join ...

  4. mysql 索引,转载

    from:http://blog.csdn.net/zhanglu0223/article/details/8713149 1. 索引建立的原则 用于索引的最好的备选数据列是那些出现在WHERE子句. ...

  5. Turn the Rectangles 1008B

    output standard output There are nn rectangles in a row. You can either turn each rectangle by 9090  ...

  6. ajax用户名存在检测

    一.ajax请求的四个步骤: 1.创建ajax对象 var xmlhttp=new XMLHttpRequest();//IE5,IE6以外的浏览器 var xmlhttp=new ActiveXOb ...

  7. English: How to Pronounce R [ɹ] Consonant

    English: How to Pronounce R [ɹ] Consonant Share Tweet Share Tagged With: Most Popular, Sound How-To ...

  8. [jQ]jQuery显式操作Checkbox,并用数组存储关联值的方案

    ---------------------------------------------------------------------------------------------------- ...

  9. it工程师常用英文自我介绍常用用语

      Good morning ! It is really my honor to have this opportunity for an interview, I hope i can make ...

  10. ANg-线性回归算法

    线性回归算法 linear regression 对于线性回归模型,我们期望对于样本数据集,通过假设函数,得出目标值 代价函数 m在这里指的是训练样本的数量 所以我们的目的就是得出代价函数(平方误差代 ...