描述

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跑出任意两个点的最短路

代码

  1. #include<stdio.h>
  2. #include<string.h>
  3.  
  4. int G[][],d[][],dp[<<][];
  5. int main()
  6. {
  7. int n;
  8. while(scanf("%d",&n)!=EOF,n)
  9. {
  10. for(int i=;i<=n;i++)
  11. for(int j=;j<=n;j++)
  12. scanf("%d",&G[i][j]),d[i][j]=G[i][j];
  13.  
  14. for(int k=;k<=n;k++)
  15. for(int i=;i<=n;i++)
  16. for(int j=;j<=n;j++)
  17. if(d[i][j]>d[i][k]+G[k][j])
  18. d[i][j]=d[i][k]+G[k][j];
  19.  
  20. memset(dp,-,sizeof dp);
  21. dp[][]=;
  22. for(int i=;i<(<<(n+));i++)
  23. {
  24. i|=;
  25. for(int j=;j<=n;j++)
  26. {
  27. if(dp[i][j]==-)continue;
  28. for(int k=;k<=n;k++)
  29. {
  30. if(j!=k&&(dp[(<<k)|i][k]==-||dp[(<<k)|i][k]>dp[i][j]+d[j][k]))
  31. dp[(<<k)|i][k]=dp[i][j]+d[j][k];
  32. }
  33. }
  34. }
  35. printf("%d\n",dp[(<<(n+))-][]);
  36. }
  37. return ;
  38. }

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. air报错 Error: Error #3000: Illegal path name

    配置增加: <supportedProfiles>extendedDesktop desktop</supportedProfiles> fb: flash:

  2. Zookpeer集群节点

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

  3. 2018SDIBT_国庆个人第四场

    A - A  这题很巧妙啊,前两天刚好做过,而且就在博客里  Little C Loves 3 I time limit per test 1 second memory limit per test ...

  4. 1:python 简介与基础

    什么是python? 1.python是一种面向对象的解释型语言,它继承了传统编译语言的通用性和强大性,同时也借鉴了简单脚本和解释语言的易用性. 2.python 在自动化测试.人工智能.数据分析等方 ...

  5. 1005 继续(3n+1)猜想 (25 分)

    1005 继续(3n+1)猜想 (25)(25 分) - 过期汽水的博客 - CSDN博客https://blog.csdn.net/qq_40167974/article/details/80739 ...

  6. Linux tr命令使用方法

    tr命令主要用于删除文件中控制字符或进行字符转换.本文主要介绍tr命令的基本语法和使用实例. tr基本语法 tr命令格式:tr [ -d ] [ -c ] [ -s ] [ 字符串1 ] [ 字符串2 ...

  7. Python中os模块使用方法

    os模块提供了对系统环境.文件.目录等操作系统级的接口函数.本文主要描述os模块和os.path模块常用函数以及常用实例. os模块函数 os.getcwd() 获取当前工作的目录. os.listd ...

  8. Android-json解析:原生JSONObject+JSONArray的使用【转】

    原文地址:https://blog.csdn.net/sinat_31057219/article/details/71518123 一.JSONObject和JSONArray的数据表示形式 JSO ...

  9. 传输层——UDP报文头介绍

    16位源端口 16位目的端口 16位总长度 16位校验和 数据 源端口:长度为16位,2个字节. 目的端口:长度为16位,2个字节. 总长度:长度为16位,2个字节,表示 UDP包头长度 和 数据长度 ...

  10. LDA线性判别分析(转)

    线性判别分析LDA详解 1 Linear Discriminant Analysis    相较于FLD(Fisher Linear Decriminant),LDA假设:1.样本数据服从正态分布,2 ...