Hie with the Pie

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 3160   Accepted: 1613

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

Source

 
用二进制位表示某一点是否走过,1表示走过,0表示没走过,例如如果有四个点,3的二进制是(0011)2,就表示1点和2点走过,3点和4点没走,用dp(i,j)表示状态为i时,走到j点的最短距离,状态i就是前面所说的二进制标记,那么在i点j为1,2时才有效(通过 i & (1<<(j - 1))来检测在状态i下哪一位是1),根据此状态,能更新dp(7,3)和dp(11,4),即更新二进制位下为0的点,7是(0111)2,11是(1011)2
 
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <cmath>
  5. using namespace std;
  6. const int N = ;
  7. const int INF = 0x7f7f7f7f;
  8. int graph[N][N];
  9. int dp[(<<)+][];
  10. void floyd(int n) {
  11. int i,j,k;
  12. for (k = ; k <= n; k++) {
  13. for (i = ; i <= n; i++) {
  14. for (j = ; j <= n; j++) {
  15. if (graph[i][k] + graph[k][j] < graph[i][j])
  16. graph[i][j] = graph[i][k] + graph[k][j];
  17. }
  18. }
  19. }
  20. }
  21. int main() {
  22. // freopen("in.txt","r",stdin);
  23. int n;
  24. int i,j,k;
  25. while (scanf("%d",&n) && n) {
  26. for (i = ; i <= n; i++) {
  27. for (j = ; j <= n; j++)
  28. scanf("%d",&graph[i][j]);
  29. }
  30. floyd(n);
  31. memset(dp, 0x7f, sizeof(dp));
  32. for (i = ; i <= n; i++) {
  33. dp[<<(i - )][i] = graph[][i];
  34. }
  35. for (i = ; i < (<<n); i++) {
  36. for (j = ; j <= n; j++) {
  37. if ((i & ( << (j - ))) == ) {
  38. for (k = ; k <= n; k++) {
  39. if (i & ( << (k - ))) {
  40. dp[(i|( << (j - )))][j] = min(dp[(i|( << (j - )))][j], dp[i][k] + graph[k][j]);
  41. }
  42. }
  43. }
  44. }
  45. }
  46. int mini = INF;
  47. for(i = ; i <= n; i++) {
  48. if (dp[(<<n) - ][i] + graph[i][] < mini)
  49. mini = dp[(<<n) - ][i] + graph[i][];
  50. }
  51. printf("%d\n",mini);
  52. }
  53. return ;
  54. }

poj3311 Hie with the Pie (状态压缩dp,旅行商)的更多相关文章

  1. 【鸽】poj3311 Hie with the Pie[状压DP+Floyd]

    题解网上一搜一大坨的,不用复述了吧. 只是觉得网上dp方程没多大问题,但是状态的表示含义模糊.不同于正常哈密顿路径求解,状态表示应当改一下. 首先定义一次移动为从一个点经过若干个点到达另一个点,则$f ...

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

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

  3. [poj3311]Hie with the Pie(Floyd+状态压缩DP)

    题意:tsp问题,经过图中所有的点并回到原点的最短距离. 解题关键:floyd+状态压缩dp,注意floyd时k必须在最外层 转移方程:$dp[S][i] = \min (dp[S \wedge (1 ...

  4. POJ 3311 Hie with the Pie(Floyd+状态压缩DP)

    题是看了这位的博客之后理解的,只不过我是又加了点简单的注释. 链接:http://blog.csdn.net/chinaczy/article/details/5890768 我还加了一些注释代码,对 ...

  5. poj 3311 Hie with the Pie(状态压缩dp)

    Description The Pizazz Pizzeria prides itself or more (up to ) orders to be processed before he star ...

  6. 状态压缩DP(大佬写的很好,转来看)

    奉上大佬博客 https://blog.csdn.net/accry/article/details/6607703 动态规划本来就很抽象,状态的设定和状态的转移都不好把握,而状态压缩的动态规划解决的 ...

  7. poj 3311 floyd+dfs或状态压缩dp 两种方法

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

  8. DP大作战—状态压缩dp

    题目描述 阿姆斯特朗回旋加速式阿姆斯特朗炮是一种非常厉害的武器,这种武器可以毁灭自身同行同列两个单位范围内的所有其他单位(其实就是十字型),听起来比红警里面的法国巨炮可是厉害多了.现在,零崎要在地图上 ...

  9. hoj2662 状态压缩dp

    Pieces Assignment My Tags   (Edit)   Source : zhouguyue   Time limit : 1 sec   Memory limit : 64 M S ...

随机推荐

  1. #!/usr/bin/env python与#!/usr/bin/python的区别

    [摘自:http://blog.csdn.net/wh_19910525/article/details/8040494] 一般的python文件的开头都有#!/usr/bin/python.这是什么 ...

  2. Jsp与servlet本质上的区别

    1.jsp经编译后就变成了Servlet.(JSP的本质就是Servlet,JVM只能识别java的类,不能识别JSP的代码,Web容器将JSP的代码编译成JVM能够识别的java类)2.jsp更擅长 ...

  3. php和js如何通过json互相传递数据

    当我们在结合php和javascript实现某些功能时,经常会用到json.json是js的一种数据格式,可以直接被js解析.而php无法直接读取json数据,但是php提供了json_decode函 ...

  4. 阿里云CentOS6上配置iptables

    参考:http://blog.abv.cn/?p=50 阿里云CentOS6默认没有启动iptables 1.检查iptables状态 [root@iZ94jj63a3sZ ~]# service i ...

  5. jquery 中post 、get的同步问题

    jquery 中post .get的同步问题 解决方法1: 在全局设置: $.ajaxSetup({ async : false }); 然后再使用post或get方法 $.get("reg ...

  6. 创建一个List获取数据的lookup

    第一步,在类:syslookup中新建方法 public static client void lookupList(FormStringControl _formStringControl, Lis ...

  7. C语言获得文件一行

    C语言获得一行的数据还是比较麻烦的,这里讲一下几种曾经用过的方法. 第一种,是最笨的方法,就是一个一个字符的读取,也是最容易想到的方法.具体实现如下:void   read_line(char   l ...

  8. 洛谷P3370 【模板】字符串哈希

    P3370 [模板]字符串哈希 143通过 483提交 题目提供者HansBug 标签 难度普及- 提交  讨论  题解 最新讨论 看不出来,这题哪里是哈希了- 题目描述 如题,给定N个字符串(第i个 ...

  9. js+html5 +devexpress属性总结

    //获取某行某列的值 onSelectionChanged: function (selectedItems) { var data = selectedItems.selectedRowsData[ ...

  10. jquery 城市三级联动

    js代码 /*城市三级联动 * @method cityChange * @param allProvince,allCity,allDistrict */ function cityChange(p ...