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

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

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

  1. 3
  2. 0 1 10 10
  3. 1 0 1 2
  4. 10 1 0 10
  5. 10 2 10 0
  6. 0

Sample Output

  1. 8

题意

从0出发送汉堡,送完每一个点后回到0点

矩阵 表示各个点间距离。

思路

由于能够各个点多次经过。所以先求下floyd 来更新点点之间最短距离。

把每一步。到各个点的状态和最后一步所在的位置还有所花的距离保存下来。

把最后到的各个地方,再加个回零点的距离。求个最小值。

dp[15][15]   一维表示已经走的步数,二维表示当前最后一步到的点。  map的x表示状态,y表示已经花费的时间。

  1. #include<stdio.h>
  2. #include<map>
  3. #include<algorithm>
  4. using namespace std;
  5. map<int,int>::iterator it;
  6. map<int,int> dp[15][15];//ceng 结尾
  7. int mp[15][15];
  8. void floyd(int n)
  9. {
  10. int i,j,k;
  11. for(k=0;k<=n;k++)
  12. {
  13. for(i=0;i<=n;i++)
  14. {
  15. for(j=0;j<=n;j++)
  16. {
  17. mp[i][j]=min(mp[i][j],mp[i][k]+mp[k][j]);
  18. }
  19. }
  20. }
  21. }
  22. int main()
  23. {
  24. int n,i,j,k,mn,x,y;
  25. while(scanf("%d",&n),n)
  26. {
  27. for(i=0;i<=n;i++)
  28. {
  29. for(j=0;j<=n;j++)
  30. {
  31. scanf("%d",&mp[i][j]);
  32. }
  33. }
  34. floyd(n);
  35. /*for(i=0;i<=n;i++)
  36. {
  37. for(j=0;j<=n;j++)
  38. {
  39. printf("%d ",mp[i][j]);
  40. }
  41. puts("");
  42. }*/
  43.  
  44. //clear
  45. dp[0][0][1]=0;
  46. for(i=1;i<=n;i++)//第几处了
  47. {
  48. for(k=0;k<=n;k++)//终点
  49. {
  50. dp[i][k].clear();
  51. for(j=0;j<=n;j++)//起点
  52. {
  53. for(it=dp[i-1][j].begin();it!=dp[i-1][j].end();it++)//上一层的各个状态
  54. {
  55. x=it->first;
  56. y=it->second;
  57. if((x&(1<<k))==0)//还没走过
  58. {
  59. if(dp[i][k].count(x|(1<<k))==0)
  60. dp[i][k][x|(1<<k)]=y+mp[j][k];
  61. else
  62. dp[i][k][x|(1<<k)]=min(dp[i][k][x|(1<<k)],y+mp[j][k]);
  63. }
  64. }
  65. }
  66. }
  67. }
  68.  
  69. i=n;//最后一层
  70. mn=999999999;
  71. for(j=1;j<=n;j++)
  72. {
  73. for(it=dp[i][j].begin();it!=dp[i][j].end();it++)//上一层的各个状态
  74. {
  75. mn=min(mn,it->second+mp[j][0]);
  76. }
  77. }
  78. printf("%d\n",mn);
  79.  
  80. }
  81. return 0;
  82. }
  83. /*
  84. 3
  85. 0 1 10 10
  86. 1 0 1 2
  87. 10 1 0 10
  88. 10 2 10 0
  89.  
  90. 3
  91. 0 5 6 7
  92. 5 0 8 9
  93. 6 8 0 10
  94. 7 9 10 0
  95. */

poj 3311 Hie with the Pie dp+状压的更多相关文章

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

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

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

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

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

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

  4. POJ 3311 Hie with the Pie 【状压DP】

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

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

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

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

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

  7. poj 3311 Hie with the Pie

    floyd,旅游问题每个点都要到,可重复,最后回来,dp http://poj.org/problem?id=3311 Hie with the Pie Time Limit: 2000MS   Me ...

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

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

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

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

随机推荐

  1. Spring整合Mybatis案例,献给初学的朋友

    今天我们来学习Spring整合Mybatis. 开发环境:Ide:MyEclipse 2017 CI JDK:1.8 首先我们简单的认识下这两个框架 1.Mybatis MyBatis是一个支持普通S ...

  2. PIL The _imaging C module is not installed

    今天在WIN 7 64位用PIL的时候,提示 The _imaging C module is not installed ,原来是需要安装64位的. 刚开始安装的是这个:http://www.pyt ...

  3. C# -- 学习笔记之基础篇

    由于要做一个系统,需要用到搜索引擎开发的很多知识点.对于开发语言的选择,我一般不是擅长什么才选择什么的,而是通过对比之后,考虑开发时间和难易程度来选择.尽管现在的开发经验还不足,也只能凭借自己弱弱的判 ...

  4. Boost汉字匹配 -- 宽字符

      原文链接:http://blog.csdn.net/sptoor/article/details/4930069 思路:汉字匹配,把字符都转换成宽字符,然后再匹配. 需要用到以下和宽字符有关的类: ...

  5. PAT甲级1017. Queueing at Bank

    PAT甲级1017. Queueing at Bank 题意: 假设一家银行有K台开放服务.窗前有一条黄线,将等候区分为两部分.所有的客户都必须在黄线后面排队,直到他/她轮到服务,并有一个可用的窗口. ...

  6. python 用gensim进行文本相似度分析

    http://blog.csdn.net/chencheng126/article/details/50070021 参考于这个博主的博文. 原理 1.文本相似度计算的需求始于搜索引擎. 搜索引擎需要 ...

  7. input输入框限制输入英文,数字,汉字

    <h1>js验证输入框内容</h1><br /><br /> 只能输入英文<input type="text" onkeyup ...

  8. MYSQL SELECT 过程 转

      本文从一个select语句的执行过程出发, 遍历MySQL的多个几子系统. 先放图一张, 按图索骥开始我们的历险. <ignore_js_op>   当客户端连接上MySQL服务端之后 ...

  9. Android中MVC模型(复合模式)

    mvc是model,view,controller的缩写,mvc包括三个部分: 1.模型(model)对象:是应用程序的主体部分,全部的业务逻辑都应该写在该层. 2.视图(view)对象:是应用程序中 ...

  10. Google Chrome 39.0.2171.71 正式发布

    Google Chrome,又称Google浏览器,是一个由Google(谷歌)公司开发的网页浏览器.该浏览器是基于其他开源软件所撰写,包括WebKit,目标是提升稳定性.速度和安全性,并创造出简单且 ...