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. Access forbidden! You don't have permission to access the requested object. It is either read-protected or not readable by the server

    好久不没弄 apache和php了,突然遇到这种奇葩的问题,本来想直接在网上找现成的解决思路,结果网上搜索花了不少功夫,也没找到原因. 后来看日志文件:apache\logs\error.log发现了 ...

  2. AJAX跨域解决方案

    从AJAX诞生那天起,XMLHttprequest对象不能跨域请求的问题就一直存在,这似乎是一个很经典的问题了,是由于javascript的同源策略所导致. 解决的办法,大概有如下几种: 1. 使用中 ...

  3. fdisk -c 0 350 1000 300命令

    在Linux中有一个fdisk的分区命令,在对开发板的nand或者emmc分区也会用到这个命令, fdisk -c 这里0 350 1000 300分别代表: 每个扇区大小为0,一共350个柱面,起始 ...

  4. 常用PHP函数类目录

    说明:用来记录我在开发过程中,经常用到的一些常用函数或者常用类. 常用函数 PHP常用函数 (1) 常用类 PHP表单数据校验类

  5. Linux下的文件与目录操作 BY 四喜三顺

      文件操作权限: chmod 三个八进制数字 文件名 其中:三个八进制数字,第一个代表本用户的权限,第二个代表同组的权限,第三个代表其他用户的权限4代表可读2代表可写1代表可执行例如:chmod 7 ...

  6. Fragment笔记整理

    前言 一直在用Fragment,但是没有系统的整理过,Google了一下相关文章,看到了几篇,将几篇还不错的文章重点整理了下,很多是直接Copy的,只为做个笔记,以后翻来看比较方便,建议大家看一下下面 ...

  7. OCR图像识别技术-Asprise OCR

    // csc AspriseDemo.cs /r:AspriseOcr.dll // 注意注册:AspriseOCR.InputLicense("123456", "12 ...

  8. KMS服务器激活Windows和Office2013EnterprisePlus

    KMS服务器激活Windows和Office2013EnterprisePlus 参考了文档 http://wenku.baidu.com/view/0cb2602358fb770bf68a5501. ...

  9. autoit 将输入法修改为英文输入法

    如果能用ControlSend,就不推荐用send,如果非要用send,可以切换输入法为英文再send. $hWnd = WinGetHandle("[ACTIVE]");$hWn ...

  10. 造轮子之数据库对比工具DataBaseComparer

    最近同时在维护好几个项目,有些项目是SqlServer的,另一些是MySql的,DBA推荐了一个线上库和线下库的对比工具,用的时候经常会在对比时,半天都没有进度.索性自己这次造个轮子,做了一个纯对比数 ...